Class: EasySax::SimpleElement

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_sax/simple_element.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attrsObject

Returns the value of attribute attrs.



4
5
6
# File 'lib/easy_sax/simple_element.rb', line 4

def attrs
  @attrs
end

#elementsObject

Returns the value of attribute elements.



4
5
6
# File 'lib/easy_sax/simple_element.rb', line 4

def elements
  @elements
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/easy_sax/simple_element.rb', line 4

def name
  @name
end

#textObject

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_hObject 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