Class: MappableObjectAttributes::DataAttributesMap

Inherits:
BasicObject
Includes:
Kernel
Defined in:
lib/mappable_object_attributes/data_attributes_map.rb

Instance Method Summary collapse

Constructor Details

#initializeDataAttributesMap

Returns a new instance of DataAttributesMap.


7
8
9
# File 'lib/mappable_object_attributes/data_attributes_map.rb', line 7

def initialize
  @mash = ::Hashie::Mash.new      
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

send all setter methods directly to set_map_att_foo


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mappable_object_attributes/data_attributes_map.rb', line 17

def method_missing(method_name, *arguments, &block)
  # if it's all word characters and a :setter, 
  #  then pass to the mash as a data attribute
  if method_name.to_s =~ /(\w+)=/
    set_map_att_foo($1, *arguments)
  elsif @mash.respond_to?(method_name)
    @mash.send(method_name, *arguments, &block)
  else
    super
  end
end

Instance Method Details

#map_keysObject


12
13
14
# File 'lib/mappable_object_attributes/data_attributes_map.rb', line 12

def map_keys
  @mash.keys
end

#map_nested_att(att_name, nodename) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mappable_object_attributes/data_attributes_map.rb', line 40

def map_nested_att(att_name, nodename)
  path_arr = Array(nodename)
  find_nested_node_foo = ->(msh){
    val = path_arr.inject(msh) do |target_node, att|
      break if target_node.nil?
      target_node.fetch(att, nil)
    end
    val
  }

  set_map_att_foo(att_name, find_nested_node_foo)
end

#map_simple_atts(*args) ⇒ Object


36
37
38
# File 'lib/mappable_object_attributes/data_attributes_map.rb', line 36

def map_simple_atts(*args)
  args.each{|k| set_map_att_foo(k, k) }
end