Class: EasySax::SimpleElement
- Inherits:
-
Object
- Object
- EasySax::SimpleElement
- Defined in:
- lib/easy_sax/simple_element.rb
Instance Attribute Summary collapse
-
#attrs ⇒ Object
Returns the value of attribute attrs.
-
#elements ⇒ Object
Returns the value of attribute elements.
-
#name ⇒ Object
Returns the value of attribute name.
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(name, attrs) ⇒ SimpleElement
constructor
A new instance of SimpleElement.
- #text_for(key) ⇒ Object
- #to_h ⇒ Object (also: #inspect, #to_s)
Constructor Details
#initialize(name, attrs) ⇒ SimpleElement
Returns a new instance of SimpleElement.
6 7 8 9 10 |
# File 'lib/easy_sax/simple_element.rb', line 6 def initialize(name, attrs) @name = name @attrs = HashWithIndifferentAccess.new(attrs || {}) @elements = HashWithIndifferentAccess.new end |
Instance Attribute Details
#attrs ⇒ Object
Returns the value of attribute attrs.
4 5 6 |
# File 'lib/easy_sax/simple_element.rb', line 4 def attrs @attrs end |
#elements ⇒ Object
Returns the value of attribute elements.
4 5 6 |
# File 'lib/easy_sax/simple_element.rb', line 4 def elements @elements end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/easy_sax/simple_element.rb', line 4 def name @name end |
#text ⇒ Object
Returns the value of attribute text.
4 5 6 |
# File 'lib/easy_sax/simple_element.rb', line 4 def text @text end |
Instance Method Details
#[](key) ⇒ Object
12 13 14 |
# File 'lib/easy_sax/simple_element.rb', line 12 def [](key) elements[key] end |
#[]=(key, value) ⇒ Object
16 17 18 |
# File 'lib/easy_sax/simple_element.rb', line 16 def []=(key, value) elements[key] = value end |
#text_for(key) ⇒ Object
20 21 22 |
# File 'lib/easy_sax/simple_element.rb', line 20 def text_for(key) elements[key]&.text end |
#to_h ⇒ Object Also known as: inspect, to_s
24 25 26 27 28 29 30 |
# File 'lib/easy_sax/simple_element.rb', line 24 def to_h {}.tap do |hash| hash[:attrs] = attrs unless attrs.empty? hash[:elements] = elements unless elements.empty? hash[:text] = text if text end end |