Class: Klassy::Klassic

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Klassic

Returns a new instance of Klassic.



8
9
10
# File 'lib/klassy/klassic.rb', line 8

def initialize(options = {})
  @parent = options[:parent]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/klassy/klassic.rb', line 19

def method_missing(name, *args, &block)
  # find or create the class
  klass = find_or_create_class(name)

  # create the object
  attribs = args.find{|x| x.class == Hash }
  obj = create_object(klass, name, attribs)

  # if there's a parent, make this object one of it's children
  add_nested_object(name, obj) if parent

  # if block_given? then we have more objects to create.
  Klassic.klassify({parent: obj}, &block) if block_given?

  obj
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/klassy/klassic.rb', line 6

def parent
  @parent
end

Class Method Details

.klassify(options = {}, &block) ⇒ Object



12
13
14
15
# File 'lib/klassy/klassic.rb', line 12

def self.klassify(options = {}, &block)
  klassic = new(options)
  klassic.instance_eval(&block)
end