Class: HashTML::Node
- Inherits:
-
Object
- Object
- HashTML::Node
- Defined in:
- lib/hashtml.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#children ⇒ Object
Returns the value of attribute children.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(node = nil) ⇒ Node
constructor
A new instance of Node.
- #method_missing(method, *args) ⇒ Object
- #to_h ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(node = nil) ⇒ Node
Returns a new instance of Node.
98 99 100 101 102 103 |
# File 'lib/hashtml.rb', line 98 def initialize(node=nil) return unless node @name = node.name @attributes = node.respond_to?(:attributes) ? get_html_node_attributes(node) : {} @children = get_html_node_children(node) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/hashtml.rb', line 116 def method_missing(method, *args) method = method.to_s attributes, new_value, _nil = args attributes ||= {} if method.end_with?("?") key = method[0..-2] _check_for_presence(key, attributes) elsif method.end_with?("=") key = method[0..-2] new_value, attributes = attributes, {} if new_value.nil? _change_value(key, attributes, new_value) else _get_value(method, attributes) end end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
96 97 98 |
# File 'lib/hashtml.rb', line 96 def attributes @attributes end |
#children ⇒ Object
Returns the value of attribute children.
96 97 98 |
# File 'lib/hashtml.rb', line 96 def children @children end |
#name ⇒ Object
Returns the value of attribute name.
96 97 98 |
# File 'lib/hashtml.rb', line 96 def name @name end |
Instance Method Details
#to_h ⇒ Object
105 106 107 |
# File 'lib/hashtml.rb', line 105 def to_h { @name => { children: @children.map { |child| child.to_h }, attributes: @attributes } } end |
#to_html ⇒ Object
109 110 111 112 113 114 |
# File 'lib/hashtml.rb', line 109 def to_html space = (@attributes.any? ? ' ' : '') children_html = @children.map { |child| child.to_html }.join attribute_list = @attributes.map { |k, v| "#{k}=\"#{v}\"" }.join(' ') "<#{@name}#{space}#{attribute_list}>#{children_html}</#{@name}>" end |