Class: Saxy::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/saxy/element.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeElement

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

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/saxy/element.rb', line 3

def attributes
  @attributes
end

#valueObject (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_hObject



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