Sha256: 1307d18ca62e5578b30625ae6aff2d2711a36a8d9da8ed9a813df7fd3ac2aa38

Contents?: true

Size: 1.07 KB

Versions: 5

Compression:

Stored size: 1.07 KB

Contents

require 'securerandom'
require 'date'
require 'ostruct'

module Parsable
  class Context

    attr_accessor :variables

    def initialize args={}

      @variables = args.fetch(:variables, {
        :random   => OpenStruct.new(:hex => SecureRandom.hex, :integer => Time.now.to_i),
        :date     => OpenStruct.new(:today => Date.today.to_s, :year => Date.today.year.to_s),
        :time     => OpenStruct.new(:now => Time.now.to_s),
        :custom   => OpenStruct.new
      }) # object_key => OpenStruct.new(:attribute => value)
         # {{object.attribute}} returns value
    end

    def custom_store attribute, value
      _store :custom, attribute, value
    end

    def system_store object_key, attribute, value
      _store object_key, attribute, value
    end

    def read object_key, attribute
      _object(object_key).send(attribute.to_sym)
    end

    private

    def _object object_key
      variables[object_key.to_sym] ||= OpenStruct.new
    end

    def _store object_key, attribute, value
      _object(object_key).send("#{attribute}=".to_sym, value)
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
parsable-0.1.4 lib/parsable/context.rb
parsable-0.1.3 lib/parsable/context.rb
parsable-0.1.2 lib/parsable/context.rb
parsable-0.1.1 lib/parsable/context.rb
parsable-0.1.0 lib/parsable/context.rb