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.
Instance Method Summary collapse
- #[](index) ⇒ Object
- #__js_call__(method, args) ⇒ Object
- #__js_get__(key) ⇒ 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.
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
157 158 159 |
# File 'lib/dommy/node.rb', line 157 def empty? @compute.call.empty? end |
#entries ⇒ Object
145 146 147 |
# File 'lib/dommy/node.rb', line 145 def entries @compute.call.each_with_index.map { |v, i| [i, v] } end |
#first ⇒ Object
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 |
#keys ⇒ Object
149 150 151 |
# File 'lib/dommy/node.rb', line 149 def keys (0...length).to_a end |
#last ⇒ Object
122 123 124 |
# File 'lib/dommy/node.rb', line 122 def last @compute.call.last end |
#length ⇒ Object Also known as: size
95 96 97 |
# File 'lib/dommy/node.rb', line 95 def length @compute.call.length end |
#to_a ⇒ Object
131 132 133 |
# File 'lib/dommy/node.rb', line 131 def to_a @compute.call.dup end |
#values ⇒ Object
153 154 155 |
# File 'lib/dommy/node.rb', line 153 def values to_a end |