Class: Dryer::Routes::HashObject

Inherits:
Services::SimpleService
  • Object
show all
Defined in:
lib/dryer/routes/hash_object.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ HashObject

Returns a new instance of HashObject.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dryer/routes/hash_object.rb', line 6

def initialize(hash)
  hash.each do |k,v|
    key = k.is_a?(Numeric) ? "_#{k}" : k

    if v.is_a?(Proc)
      self.send(:define_singleton_method, key, proc do |*args, **kwargs, &block|
        v.arity != 0 ? v.call(*args, **kwargs, &block) : v.call
      end)
    else
      self.instance_variable_set(
        "@#{key}", v.is_a?(Hash) ? HashObject.new(v) : v
      )

      self.send(
        :define_singleton_method, key, proc{self.instance_variable_get("@#{key}")}
      )
    end
  end
end