Class: ClassyStruct::ClassyStructClass

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = hil) ⇒ ClassyStructClass

Returns a new instance of ClassyStructClass.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/classy_struct.rb', line 7

def initialize(hash=hil)
  if hash
    hash.each_pair do |k,v|
      if v.is_a?(Hash)
        v = self.class.node_class(k).new(v)
      end

      send("#{k}=", v)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/classy_struct.rb', line 28

def method_missing(name, *args)
  base = (name.to_s =~ /=$/) ? name.to_s[0..-2] : name

  self.class.class_eval "    def \#{base}\n      @\#{base}\n    end\n\n    def \#{base}=(val)\n      @\#{base} = val\n    end\n  EOF\n\n  send(name, *args)\nend\n"

Class Method Details

.node_class(name) ⇒ Object



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

def self.node_class(name)
  @__node_classes ||= {}
  @__node_classes[name.to_sym] ||= ClassyStruct.new
end

Instance Method Details

#new_child(key) ⇒ Object



24
25
26
# File 'lib/classy_struct.rb', line 24

def new_child(key)
  self.send("#{key}=", self.class.node_class(key).new)
end