Class: Roda::Component::DOM

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/component/dom.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html) ⇒ DOM



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/roda/component/dom.rb', line 8

def initialize html
  @raw_html = html

  if server?
    @dom = raw_html.is_a?(String) ? Component::HTML(raw_html.dup): raw_html
  else
    @dom = raw_html.is_a?(String) ? Element[raw_html.dup] : raw_html
  end

  # super @dom
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

This allows you to use all the nokogiri or opal jquery methods if a global one isn’t set



104
105
106
107
108
109
110
111
# File 'lib/roda/component/dom.rb', line 104

def method_missing method, *args, &block
  # respond_to?(symbol, include_all=false)
  if dom.respond_to? method, true
    dom.send method, *args, &block
  else
    super
  end
end

Instance Attribute Details

#domObject

Returns the value of attribute dom.



6
7
8
# File 'lib/roda/component/dom.rb', line 6

def dom
  @dom
end

#raw_htmlObject

Returns the value of attribute raw_html.



6
7
8
# File 'lib/roda/component/dom.rb', line 6

def raw_html
  @raw_html
end

Instance Method Details

#append(obj) ⇒ Object



70
71
72
73
# File 'lib/roda/component/dom.rb', line 70

def append obj
  obj = obj.dom if obj.is_a? Roda::Component::DOM
  super obj
end

#data(key = false, value = false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/roda/component/dom.rb', line 41

def data key = false, value = false
  d = Hash[node.xpath("@*[starts-with(name(), 'data-')]").map{|a| [a.name, a.value]}]

  if !key
    d
  elsif key && !value
    d[key]
  else
    node["data-#{key}"] = value
  end
end

#find(string, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/roda/component/dom.rb', line 20

def find string, &block
  if client?
    node = DOM.new dom.find(string)
  elsif server?
    if block
      node = DOM.new dom.css(string)
    else
      node = DOM.new dom.at(string)
    end
  end

  if block
    node.each do |n|
      block.call DOM.new n
    end
  end

  node
end

#html(content = false) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/roda/component/dom.rb', line 86

def html content = false
  if !content
    if server?
      node.inner_html
    else
      node ? node.html : dom.html
    end
  else
    self.html = content
  end
end

#html=(content) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/roda/component/dom.rb', line 58

def html= content
  if server?
    node.inner_html = content
  else
    content = content.dom if content.is_a? Roda::Component::DOM
    node.html content
  end

  node
end

#nodeObject



98
99
100
# File 'lib/roda/component/dom.rb', line 98

def node
  @node || dom
end

#prepend(obj) ⇒ Object



75
76
77
78
# File 'lib/roda/component/dom.rb', line 75

def prepend obj
  obj = obj.dom if obj.is_a? Roda::Component::DOM
  super obj
end

#replace_with(obj) ⇒ Object



80
81
82
83
# File 'lib/roda/component/dom.rb', line 80

def replace_with obj
  obj = obj.dom if obj.is_a? Roda::Component::DOM
  super obj
end

#val(value) ⇒ Object



53
54
55
# File 'lib/roda/component/dom.rb', line 53

def val value
  node.content = value
end