Class: Dommy::RadioNodeList

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

Overview

RadioNodeList — a NodeList a form's named getter returns when a name matches more than one control. Adds value: the value of the checked radio button in the list (or "" if none), and a setter that checks the radio whose value matches.

Instance Method Summary collapse

Methods inherited from NodeList

#__js_call__, #entries, #for_each, #keys, #values

Methods included from Bridge::Methods

included

Constructor Details

#initialize(*args, &compute) ⇒ RadioNodeList

An optional &compute block makes the list LIVE: it re-evaluates the membership on every DOM-shape read, so a reference held across a mutation (e.g. removing a control from the group) reflects the change — matching the form named getter's live RadioNodeList. Without a block it is a snapshot.



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

def initialize(*args, &compute)
  @compute = compute
  super(*args)
end

Instance Method Details

#[](index) ⇒ Object



113
114
115
116
# File 'lib/dommy/node.rb', line 113

def [](index)
  __refresh__
  super
end

#__js_get__(key) ⇒ Object



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

def __js_get__(key)
  return value if key == "value"

  super
end

#__js_set__(key, v) ⇒ Object



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

def __js_set__(key, v)
  return self.value = v if key == "value"

  super
end

#__refresh__Object

Refresh the backing storage from the live source, if any. Returns self so it can prefix the Array reads below.



98
99
100
101
# File 'lib/dommy/node.rb', line 98

def __refresh__
  replace(@compute.call || []) if @compute
  self
end

#each(&block) ⇒ Object



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

def each(&block)
  __refresh__
  super
end

#item(index) ⇒ Object



108
109
110
111
# File 'lib/dommy/node.rb', line 108

def item(index)
  __refresh__
  super
end

#lengthObject



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

def length
  __refresh__
  super
end

#valueObject



123
124
125
126
127
# File 'lib/dommy/node.rb', line 123

def value
  __refresh__
  radio = find { |el| radio_button?(el) && el.checked }
  radio ? radio.value.to_s : ""
end

#value=(new_value) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/dommy/node.rb', line 129

def value=(new_value)
  __refresh__
  target = find { |el| radio_button?(el) && el.value.to_s == new_value.to_s }
  each { |el| el.checked = false if radio_button?(el) }
  target.checked = true if target
  new_value
end