Class: Configer::Object

Inherits:
Hash
  • Object
show all
Includes:
HashExtension
Defined in:
lib/configer/object.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HashExtension

#deep_merge, #deep_merge!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/configer/object.rb', line 11

def method_missing( method_name, *args, &block )

  obj_methods = self.__send__(:methods)
  if method_name[-1] == '=' && !obj_methods.include?(method_name)
    self[method_name[0..-2]]= *args
    return self[method_name[0..-2]]

  elsif self[method_name.to_s].nil? && obj_methods.include?(method_name)

    if block_given?
      return self.__send__(method_name,*args,&block)
    else
      return self.__send__(method_name,*args)
    end

  else
    return self[method_name.to_s]

  end

end

Class Method Details

.parse(obj) ⇒ Object

> parse object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/configer/object.rb', line 46

def self.parse(obj)
  case obj

    when Hash
      obj.reduce(self.new){|m,h|
        m.deep_merge!( (h[0].class <= Symbol ? h[0].to_s : h[0] ) => self.parse(h[1]) ) ;m}

    when Array
      obj.map{|o| self.parse(o) }

    else
      obj

  end

end

Instance Method Details

#[](key) ⇒ Object



35
36
37
38
# File 'lib/configer/object.rb', line 35

def [](key)
  key = key.to_s if key.class <= Symbol
  super || super(key.to_sym)
end

#[]=(key, value) ⇒ Object



40
41
42
43
# File 'lib/configer/object.rb', line 40

def []=(key,value)
  key = key.to_s if key.class <= Symbol
  super
end