Class: OBIX::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, &block) ⇒ Builder

Initialize a new builder.

parent - An Objects::Object instance or derivative thereof describing the parent

of the object that will be built.


10
11
12
13
14
15
# File 'lib/obix/builder.rb', line 10

def initialize parent = nil, &block
  @parent  = parent
  @objects = []

  instance_eval &block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, attributes = {}, &block) ⇒ Object

Respond to methods matching OBIX tags.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/obix/builder.rb', line 22

def method_missing method, attributes = {}, &block
  klass = Objects.find method

  object = klass.new &block

  attributes.each do |key, value|
    object.send "#{key}=", value
  end

  object.parent = @parent

  if @parent
    @objects.push object
  else
    if @objects.empty?
      @objects.push object
    else
      raise ArgumentError, "Object already has a root"
    end
  end
end

Instance Attribute Details

#objectsObject (readonly)

Returns the value of attribute objects.



4
5
6
# File 'lib/obix/builder.rb', line 4

def objects
  @objects
end

Instance Method Details

#objectObject



17
18
19
# File 'lib/obix/builder.rb', line 17

def object
  objects.first
end