Class: Ansr::NestedOpenStructWithHashAccess

Inherits:
OpenStructWithHashAccess show all
Defined in:
lib/ansr/utils.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from OpenStructWithHashAccess

#[], #merge, #merge!, #to_h

Constructor Details

#initialize(klass, *args) ⇒ NestedOpenStructWithHashAccess

Returns a new instance of NestedOpenStructWithHashAccess.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ansr/utils.rb', line 31

def initialize klass, *args
  @nested_class = klass
  hash = {}

  hashes_and_keys = args.flatten
  lazy_configs = hashes_and_keys.extract_options!

  args.each do |v|
    if v.is_a? Hash
      key = v.first
      value = v[key]

      hash[key] = nested_class.new value
    else
      hash[v] = nested_class.new
    end
  end

  lazy_configs.each do |k,v|
    hash[k] = nested_class.new v
  end

  super hash
  set_default_proc!
end

Instance Attribute Details

#nested_classObject (readonly)

Returns the value of attribute nested_class.



28
29
30
# File 'lib/ansr/utils.rb', line 28

def nested_class
  @nested_class
end

Instance Method Details

#<<(key) ⇒ Object



57
58
59
# File 'lib/ansr/utils.rb', line 57

def << key
  @table[key]
end

#[]=(key, value) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/ansr/utils.rb', line 61

def []=(key, value)
  if value.is_a? Hash
    send "#{key}=", nested_class.new(value)
  else
    send "#{key}=", value
  end
end

#marshal_dumpObject



69
70
71
72
73
74
# File 'lib/ansr/utils.rb', line 69

def marshal_dump
  h = to_h.dup
  h.default = nil

  [nested_class, h]
end

#marshal_load(x) ⇒ Object



76
77
78
79
80
# File 'lib/ansr/utils.rb', line 76

def marshal_load x
  @nested_class = x.first
  super x.last
  set_default_proc!
end