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

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_hObject



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

#valueObject



24
25
26
# File 'lib/saxy/element.rb', line 24

def value
  @value && @value.strip
end