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.
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
- #value ⇒ 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 |
Instance Method Details
#append_value(string) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/saxy/element.rb', line 17 def append_value(string) if @value || !string.strip.empty? @value ||= "" @value << string end end |
#attribute_name(name) ⇒ Object
40 41 42 |
# File 'lib/saxy/element.rb', line 40 def attribute_name(name) underscore(name) end |
#set_attribute(name, value) ⇒ Object
10 11 12 13 14 15 |
# 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
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/saxy/element.rb', line 28 def to_h return value unless attributes.any? data = attributes.reduce({}) do |memo, (name, value)| memo[name] = value.size == 1 ? value.first : value memo end data["contents"] = value data end |
#value ⇒ Object
24 25 26 |
# File 'lib/saxy/element.rb', line 24 def value @value && @value.strip end |