Class: AdfBuilder::Nodes::Node

Inherits:
Object
  • Object
show all
Includes:
Validations
Defined in:
lib/adf_builder/nodes/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validations

included, #validate!

Constructor Details

#initializeNode

Returns a new instance of Node.



10
11
12
13
14
15
# File 'lib/adf_builder/nodes/node.rb', line 10

def initialize
  @children = []
  @attributes = {}
  @value = nil
  @tag_name = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/adf_builder/nodes/node.rb', line 29

def method_missing(method_name, *args, &block)
  # Support for dynamic/custom tags
  # usage: custom_tag "value", attr: "val"
  # usage: custom_tag { ... }

  tag_name = method_name
  attributes = args.last.is_a?(Hash) ? args.last : {}
  value = args.first unless args.first == attributes

  # If it's a block, it's a structural node
  if block_given?
    node = GenericNode.new(tag_name, attributes)
    node.instance_eval(&block)
    add_child(node)
  # If it has a value, it's a leaf node/element
  elsif value
    node = GenericNode.new(tag_name, attributes, value)
    add_child(node)
  else
    # Just a tag with attributes? e.g. <flag active="true"/>
    node = GenericNode.new(tag_name, attributes)
    add_child(node)
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/adf_builder/nodes/node.rb', line 8

def attributes
  @attributes
end

#childrenObject (readonly)

Returns the value of attribute children.



8
9
10
# File 'lib/adf_builder/nodes/node.rb', line 8

def children
  @children
end

#tag_nameObject (readonly)

Returns the value of attribute tag_name.



8
9
10
# File 'lib/adf_builder/nodes/node.rb', line 8

def tag_name
  @tag_name
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/adf_builder/nodes/node.rb', line 8

def value
  @value
end

Instance Method Details

#add_child(node) ⇒ Object



17
18
19
# File 'lib/adf_builder/nodes/node.rb', line 17

def add_child(node)
  @children << node
end

#remove_children(tag_name) ⇒ Object



21
22
23
# File 'lib/adf_builder/nodes/node.rb', line 21

def remove_children(tag_name)
  @children.reject! { |c| c.tag_name == tag_name }
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/adf_builder/nodes/node.rb', line 54

def respond_to_missing?(method_name, include_private = false)
  true
end

#to_xmlObject



25
26
27
# File 'lib/adf_builder/nodes/node.rb', line 25

def to_xml
  Serializer.to_xml(self)
end