Class: Dommy::LiveNodeList
- Inherits:
-
Object
- Object
- Dommy::LiveNodeList
- 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.
Constant Summary collapse
- JS_METHOD_NAMES =
Methods routed through js_call (keep in sync with its when-arms).
%w[item forEach entries keys values].freeze
Instance Method Summary collapse
- #[](index) ⇒ Object
- #__js_call__(method, args) ⇒ Object
- #__js_get__(key) ⇒ Object
- #__js_method_names__ ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #entries ⇒ Object
- #first ⇒ Object
- #for_each(&block) ⇒ Object (also: #forEach)
-
#initialize(&block) ⇒ LiveNodeList
constructor
A new instance of LiveNodeList.
- #item(index) ⇒ Object
- #keys ⇒ Object
- #last ⇒ Object
- #length ⇒ Object (also: #size)
- #to_a ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(&block) ⇒ LiveNodeList
Returns a new instance of LiveNodeList.
103 104 105 |
# File 'lib/dommy/node.rb', line 103 def initialize(&block) @compute = block end |
Instance Method Details
#[](index) ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/dommy/node.rb', line 121 def [](index) case index when Integer item(index) else nil end end |
#__js_call__(method, args) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/dommy/node.rb', line 184 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
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/dommy/node.rb', line 173 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 |
#__js_method_names__ ⇒ Object
99 100 101 |
# File 'lib/dommy/node.rb', line 99 def __js_method_names__ JS_METHOD_NAMES end |
#each(&block) ⇒ Object
138 139 140 141 |
# File 'lib/dommy/node.rb', line 138 def each(&block) @compute.call.each(&block) self end |
#empty? ⇒ Boolean
169 170 171 |
# File 'lib/dommy/node.rb', line 169 def empty? @compute.call.empty? end |
#entries ⇒ Object
157 158 159 |
# File 'lib/dommy/node.rb', line 157 def entries @compute.call.each_with_index.map { |v, i| [i, v] } end |
#first ⇒ Object
130 131 132 |
# File 'lib/dommy/node.rb', line 130 def first @compute.call.first end |
#for_each(&block) ⇒ Object Also known as: forEach
147 148 149 150 151 152 153 |
# File 'lib/dommy/node.rb', line 147 def for_each(&block) @compute.call.each_with_index do |value, index| block.call(value, index, self) end nil end |
#item(index) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/dommy/node.rb', line 113 def item(index) i = index.to_i arr = @compute.call return nil if i < 0 || i >= arr.length arr[i] end |
#keys ⇒ Object
161 162 163 |
# File 'lib/dommy/node.rb', line 161 def keys (0...length).to_a end |
#last ⇒ Object
134 135 136 |
# File 'lib/dommy/node.rb', line 134 def last @compute.call.last end |
#length ⇒ Object Also known as: size
107 108 109 |
# File 'lib/dommy/node.rb', line 107 def length @compute.call.length end |
#to_a ⇒ Object
143 144 145 |
# File 'lib/dommy/node.rb', line 143 def to_a @compute.call.dup end |
#values ⇒ Object
165 166 167 |
# File 'lib/dommy/node.rb', line 165 def values to_a end |