Class: Saxy::Element
- Inherits:
-
Object
- Object
- Saxy::Element
- Defined in:
- lib/saxy/element.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #append_value(string) ⇒ Object
- #attribute_name(name) ⇒ Object
-
#initialize ⇒ Element
constructor
A new instance of Element.
- #set_attribute(name, value) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ Element
Returns a new instance of Element.
5 6 7 8 |
# File 'lib/saxy/element.rb', line 5 def initialize @attributes = {} @value = nil end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
3 4 5 |
# File 'lib/saxy/element.rb', line 3 def attributes @attributes end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
3 4 5 |
# File 'lib/saxy/element.rb', line 3 def value @value end |
Instance Method Details
#append_value(string) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/saxy/element.rb', line 16 def append_value(string) unless (string = string.strip).empty? @value ||= "" @value << string end end |
#attribute_name(name) ⇒ Object
33 34 35 |
# File 'lib/saxy/element.rb', line 33 def attribute_name(name) underscore(name).to_sym end |
#set_attribute(name, value) ⇒ Object
10 11 12 13 14 |
# File 'lib/saxy/element.rb', line 10 def set_attribute(name, value) name = attribute_name(name) attributes[name] ||= [] attributes[name] << value end |
#to_h ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/saxy/element.rb', line 23 def to_h return value unless attributes.any? data = attributes.reduce({}) do |memo, (name, value)| memo[name.to_sym] = value.size == 1 ? value.first : value memo end data[:contents] = value data end |