Class: Evertils::Helper::Xml

Inherits:
Base
  • Object
show all
Defined in:
lib/evertils/helpers/xml.rb

Constant Summary

Constants inherited from Base

Base::MAX_SEARCH_SIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ Xml

Returns a new instance of Xml.

Raises:

  • (ArgumentError)

Since:

  • 0.3.15



8
9
10
11
12
# File 'lib/evertils/helpers/xml.rb', line 8

def initialize(doc)
  raise ArgumentError, "doc param required" unless doc

  @doc = doc.first
end

Instance Attribute Details

#docObject

Returns the value of attribute doc.



4
5
6
# File 'lib/evertils/helpers/xml.rb', line 4

def doc
  @doc
end

Instance Method Details

#a(link, content) ⇒ Object

Since:

  • 0.3.15



16
17
18
19
20
21
22
23
# File 'lib/evertils/helpers/xml.rb', line 16

def a(link, content)
  conf = {
    href: link,
    content: content
  }

  create(:a, conf)
end

#brObject

Since:

  • 0.3.15



37
38
39
# File 'lib/evertils/helpers/xml.rb', line 37

def br
  create(:br)
end

#create(element, conf = {}) ⇒ Object

Since:

  • 0.3.15



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/evertils/helpers/xml.rb', line 67

def create(element, conf = {})
  el = Nokogiri::XML::Node.new(element.to_s, @doc)

  return el if conf.empty?

  conf.each_pair do |k, v|
    if el.respond_to? "#{k}="
      el.send("#{k}=", v)
    elsif el.respond_to? k
      el.send(k, v)
    else
      el[k] = v
    end
  end

  el
end

#div(*children) ⇒ Object

Since:

  • 0.3.15



51
52
53
54
55
# File 'lib/evertils/helpers/xml.rb', line 51

def div(*children)
  el = create(:div)
  children.each { |child| el.add_child(child) }
  el
end

#li(*children) ⇒ Object

Since:

  • 0.3.15



43
44
45
46
47
# File 'lib/evertils/helpers/xml.rb', line 43

def li(*children)
  el = create(:li)
  children.each { |child| el.add_child(child) }
  el
end

#span(content) ⇒ Object

Since:

  • 0.3.18



27
28
29
30
31
32
33
# File 'lib/evertils/helpers/xml.rb', line 27

def span(content)
  conf = {
    content: content
  }

  create(:span, conf)
end

#ul(*children) ⇒ Object

Since:

  • 0.3.18



59
60
61
62
63
# File 'lib/evertils/helpers/xml.rb', line 59

def ul(*children)
  el = create(:ul)
  children.each { |child| el.add_child(child) }
  el
end