Class: Dommy::LiveNodeList

Inherits:
Object
  • Object
show all
Includes:
Bridge::Methods, 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[i] / 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

Methods included from Bridge::Methods

included

Constructor Details

#initialize(&block) ⇒ LiveNodeList

Returns a new instance of LiveNodeList.



168
169
170
# File 'lib/dommy/node.rb', line 168

def initialize(&block)
  @compute = block
end

Instance Method Details

#[](index) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/dommy/node.rb', line 186

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

#__js_call__(method, args) ⇒ Object



261
262
263
264
265
266
# File 'lib/dommy/node.rb', line 261

def __js_call__(method, args)
  case method
  when "item"
    item(args[0])
  end
end

#__js_get__(key) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/dommy/node.rb', line 238

def __js_get__(key)
  case key
  when "length"
    length
  else
    # Indexed getter: out-of-range yields JS `undefined` (item() returns null).
    if key.is_a?(Integer) || key.to_s.match?(/\A-?\d+\z/)
      token = item(key.to_i)
      token.nil? ? Bridge::UNDEFINED : token
    else
      Bridge::ABSENT # unknown non-index property
    end
  end
end

#each(&block) ⇒ Object



203
204
205
206
# File 'lib/dommy/node.rb', line 203

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

#empty?Boolean

Returns:

  • (Boolean)


234
235
236
# File 'lib/dommy/node.rb', line 234

def empty?
  @compute.call.empty?
end

#entriesObject



222
223
224
# File 'lib/dommy/node.rb', line 222

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

#firstObject



195
196
197
# File 'lib/dommy/node.rb', line 195

def first
  @compute.call.first
end

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



212
213
214
215
216
217
218
# File 'lib/dommy/node.rb', line 212

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

  nil
end

#item(index) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/dommy/node.rb', line 178

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

  arr[i]
end

#keysObject



226
227
228
# File 'lib/dommy/node.rb', line 226

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

#lastObject



199
200
201
# File 'lib/dommy/node.rb', line 199

def last
  @compute.call.last
end

#lengthObject Also known as: size



172
173
174
# File 'lib/dommy/node.rb', line 172

def length
  @compute.call.length
end

#to_aObject



208
209
210
# File 'lib/dommy/node.rb', line 208

def to_a
  @compute.call.dup
end

#valuesObject



230
231
232
# File 'lib/dommy/node.rb', line 230

def values
  to_a
end