class ErpRules::RulesEngine::Context

OpenStruct is part of ruby stdlib This class adds methods to allow hash-like behavior

Public Class Methods

new(hash=nil) click to toggle source
Calls superclass method
# File lib/erp_rules/rules_engine/context.rb, line 11
def initialize(hash=nil)
  if hash
    hash.each do |k,v|
      if v.class == Hash
        result = ErpRules::RulesEngine::Context.new(v)
        hash[k] = result
      elsif v.class == Array
        v.map! do |item|
          #ostruct requires objects passed to it on the constructr
          #to support #each
          if item.is_a? Enumerable
            ErpRules::RulesEngine::Context.new(item)
          else
            item
          end
        end
        #end Array case
      end
    end
  end
  super(hash)
end

Public Instance Methods

[](key) click to toggle source
# File lib/erp_rules/rules_engine/context.rb, line 34
def [](key)
  send(key)
end
[]=(key, *args) click to toggle source

This will set a method on the struct using array syntax. Trying to set the argument in eval led to an error, hence the 'send' call following it.

# File lib/erp_rules/rules_engine/context.rb, line 43
def []=(key, *args)
  arg = args[0]
  eval("#{key} = nil", binding)

  if arg.class == Hash
    send("#{key}=", ErpRules::RulesEngine::Context.new(arg))
  else
    send("#{key}=", arg)
  end
end
method_missing(mid, *args) click to toggle source

need this method in order to mimic []= behavior using the method/attr syntax of OpenStruct

Calls superclass method
# File lib/erp_rules/rules_engine/context.rb, line 56
def method_missing(mid, *args)
  if args[0].class == Hash
    args[0] = ErpRules::RulesEngine::Context.new(args[0])
  end
  super(mid, *args)
end