Class: Dommy::LiveNodeList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dommy/node.rb

Overview

‘LiveNodeList` — like NodeList, but re-evaluates its source on every access. Returned by APIs whose spec says “live” — e.g. `Node.childNodes`. The constructor takes a block that yields the current array of nodes; `length`, `item`, iteration all call it.

Inherits Array so ‘list` / `list.each` still work for callers that don’t know about the live semantics, but those work off a snapshot taken at the moment of the call. The DOM-shape methods (‘length`, `item`, `for_each`) re-query on every call.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ LiveNodeList

Returns a new instance of LiveNodeList.



91
92
93
# File 'lib/dommy/node.rb', line 91

def initialize(&block)
  @compute = block
end

Instance Method Details

#[](index) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/dommy/node.rb', line 109

def [](index)
  case index
  when Integer
    item(index)
  else
    nil
  end
end

#__js_call__(method, args) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/dommy/node.rb', line 172

def __js_call__(method, args)
  case method
  when "item"
    item(args[0])
  when "forEach"
    for_each(&args[0])
  when "entries"
    entries
  when "keys"
    keys
  when "values"
    values
  end
end

#__js_get__(key) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/dommy/node.rb', line 161

def __js_get__(key)
  case key
  when "length"
    length
  else
    if key.is_a?(Integer) || key.to_s.match?(/\A\d+\z/)
      item(key.to_i)
    end
  end
end

#each(&block) ⇒ Object



126
127
128
129
# File 'lib/dommy/node.rb', line 126

def each(&block)
  @compute.call.each(&block)
  self
end

#empty?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/dommy/node.rb', line 157

def empty?
  @compute.call.empty?
end

#entriesObject



145
146
147
# File 'lib/dommy/node.rb', line 145

def entries
  @compute.call.each_with_index.map { |v, i| [i, v] }
end

#firstObject



118
119
120
# File 'lib/dommy/node.rb', line 118

def first
  @compute.call.first
end

#for_each(&block) ⇒ Object Also known as: forEach



135
136
137
138
139
140
141
# File 'lib/dommy/node.rb', line 135

def for_each(&block)
  @compute.call.each_with_index do |value, index|
    block.call(value, index, self)
  end

  nil
end

#item(index) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/dommy/node.rb', line 101

def item(index)
  i = index.to_i
  arr = @compute.call
  return nil if i < 0 || i >= arr.length

  arr[i]
end

#keysObject



149
150
151
# File 'lib/dommy/node.rb', line 149

def keys
  (0...length).to_a
end

#lastObject



122
123
124
# File 'lib/dommy/node.rb', line 122

def last
  @compute.call.last
end

#lengthObject Also known as: size



95
96
97
# File 'lib/dommy/node.rb', line 95

def length
  @compute.call.length
end

#to_aObject



131
132
133
# File 'lib/dommy/node.rb', line 131

def to_a
  @compute.call.dup
end

#valuesObject



153
154
155
# File 'lib/dommy/node.rb', line 153

def values
  to_a
end