Class: AdfBuilder::Nodes::Node
- Inherits:
-
Object
- Object
- AdfBuilder::Nodes::Node
- Includes:
- Validations
- Defined in:
- lib/adf_builder/nodes/node.rb
Direct Known Subclasses
Address, Amount, Balance, ColorCombination, Condition, Contact, Customer, Email, Finance, FinanceMethod, GenericNode, Id, ImageTag, Name, Odometer, Option, Phone, Price, Prospect, Provider, Root, Timeframe, Vehicle, Vendor, Weighting
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#tag_name ⇒ Object
readonly
Returns the value of attribute tag_name.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #add_child(node) ⇒ Object
-
#initialize ⇒ Node
constructor
A new instance of Node.
- #method_missing(method_name, *args, &block) ⇒ Object
- #remove_children(tag_name) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #to_xml ⇒ Object
Methods included from Validations
Constructor Details
#initialize ⇒ Node
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
8 9 10 |
# File 'lib/adf_builder/nodes/node.rb', line 8 def attributes @attributes end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
8 9 10 |
# File 'lib/adf_builder/nodes/node.rb', line 8 def children @children end |
#tag_name ⇒ Object (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 |
#value ⇒ Object (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
54 55 56 |
# File 'lib/adf_builder/nodes/node.rb', line 54 def respond_to_missing?(method_name, include_private = false) true end |
#to_xml ⇒ Object
25 26 27 |
# File 'lib/adf_builder/nodes/node.rb', line 25 def to_xml Serializer.to_xml(self) end |