Class: MoStash

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/mostash/mostash.rb

Instance Method Summary collapse

Constructor Details

#initialize(init = {}) ⇒ MoStash

Returns a new instance of MoStash.



2
3
4
5
# File 'lib/mostash/mostash.rb', line 2

def initialize(init={})
  super({})
  __init__ init
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/mostash/mostash.rb', line 7

def method_missing(method_name, *args)
  #dbg "#{method_name} was sent #{args.inspect}"
  if __is_setter__( method_name )
    super method_name, __adjusted_value__( args.first )
  else
    super
  end
end

Instance Method Details

#[](key) ⇒ Object



20
21
22
# File 'lib/mostash/mostash.rb', line 20

def [](key)
  self.send "#{key.to_s}"
end

#[]=(key, value) ⇒ Object



16
17
18
# File 'lib/mostash/mostash.rb', line 16

def []=(key, value)
  self.send "#{key.to_s}=", value
end

#merge(new_hash) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/mostash/mostash.rb', line 24

def merge(new_hash)
  new_mo = @table.merge( new_hash ) do |key, oldval, newval|
    if oldval.class == MoStash
      oldval.merge newval
    else
      newval
    end
  end
  MoStash.new( new_mo )
end

#merge!(new_hash) ⇒ Object

TODO: HACK!!!!!



36
37
38
39
# File 'lib/mostash/mostash.rb', line 36

def merge!(new_hash)
  @table = self.merge( new_hash ).instance_variable_get( '@table' )
  self
end

#to_hashObject



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

def to_hash
  hash = {}
  @table.each_pair do |key, value|
    hash[key] = if value.class == MoStash
                  value.to_hash
                else
                  value
                end
  end
  hash
end