Class: Command::Results::List
Overview
A List contains ListItems - this class is sorely under-documented. Honestly, much of it’s functionality is either speculative (and it’s quite possible that you ain’t gonna need it) or else required by internal code and probably not needed by client code.
What’s most important to understand is that Lists can nest both ListItems and other Lists, which makes them a bit like a tree, and a bit like a list.
Instance Attribute Summary collapse
Attributes inherited from ListItem
#depth, #options, #order, #parent, #sequence, #value
Instance Method Summary
collapse
Methods inherited from ListItem
#match, #match_on, #next_sibling
Constructor Details
#initialize(name, values = []) ⇒ List
Returns a new instance of List.
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/command-set/result-list.rb', line 118
def initialize(name, values=[])
@name = name
create_order = 0
values = values.map do |item|
if ListItem === item
if item.order.nil?
item.order = (create_order += 1)
else
create_order = item.order
end
else
item = ListItem.new(item)
item.order = (create_order += 1)
end
if item.parent.nil?
item.parent = self
end
item
end
super(values)
@open = true
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
143
144
145
|
# File 'lib/command-set/result-list.rb', line 143
def name
@name
end
|
Instance Method Details
#==(other) ⇒ Object
268
269
270
271
272
|
# File 'lib/command-set/result-list.rb', line 268
def ==(other)
return (List === other &&
name == other.name &&
values == other.values)
end
|
#add(item) ⇒ Object
258
259
260
261
262
263
264
265
266
|
# File 'lib/command-set/result-list.rb', line 258
def add(item)
unless ListItem === item
item = ListItem.new(item)
end
item.parent = self
item.order = values.length
values.push(item)
return item
end
|
#after(item) ⇒ Object
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
# File 'lib/command-set/result-list.rb', line 233
def after(item)
index = nil
values.each_with_index do |value, idx|
if value.equal?(item)
index = idx
break
end
end
if index.nil? or index >= values.length
return nil
else
return values[index + 1]
end
end
|
#close ⇒ Object
248
249
250
251
252
253
254
255
256
|
# File 'lib/command-set/result-list.rb', line 248
def close
return self unless @open
@open = false
values.each do |item|
next unless List === item
item.close
end
return self
end
|
#eql?(other) ⇒ Boolean
274
275
276
|
# File 'lib/command-set/result-list.rb', line 274
def eql?(other)
return self == other
end
|
#filter(path) ⇒ Object
path
should be an array of arguments to match (against list names or item values). The special path element :** can be used to keep opening lists to find whatever. The result will be a List that contains only matching elements. :*
will match any single item at that level.
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/command-set/result-list.rb', line 184
def filter(path)
if path == [:**]
list = List.new(@name, @value)
list.order = @order
return list
end
if path.first == :**
double_stars = filter_into_array(path)
path = path[1..-1]
else
double_stars = nil
end
list = filter_into_array(path)
if (double_stars.nil? || double_stars.empty?) && path.length > 0 && list.empty?
raise NoMatch
end
unless double_stars.nil?
list.each do |item|
unless double_stars.find {|starred| starred.order == item.order }
double_stars << item
end
end
double_stars.sort! {|left, right| left.order <=> right.order}
list = double_stars
end
list = List.new(@name, list)
list.order = @order
return list
end
|
#filter_into_array(path) ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/command-set/result-list.rb', line 164
def filter_into_array(path)
next_path = path.first == :** ? path : path[1..-1]
return values.find_all do |value|
value.match(path[0])
end.map do |item|
begin
item.filter(next_path)
rescue NoMatch => nm
nm
end
end.reject do |value|
NoMatch === value
end
end
|
#first_child ⇒ Object
225
226
227
228
229
230
231
|
# File 'lib/command-set/result-list.rb', line 225
def first_child
if values.empty?
return nil
else
return values[0]
end
end
|
#inspect ⇒ Object
278
279
280
|
# File 'lib/command-set/result-list.rb', line 278
def inspect
"<#{@open ? "L":"l"}(#@order):#{name.to_s} #{values.inspect} #{name.to_s}>"
end
|
#list_end ⇒ Object
150
151
152
|
# File 'lib/command-set/result-list.rb', line 150
def list_end
return ListEnd.new(self)
end
|
#open? ⇒ Boolean
221
222
223
|
# File 'lib/command-set/result-list.rb', line 221
def open?
@open
end
|
#to_s ⇒ Object
146
147
148
|
# File 'lib/command-set/result-list.rb', line 146
def to_s
@name.to_s
end
|
#tree_order_next ⇒ Object
154
155
156
157
158
159
160
161
|
# File 'lib/command-set/result-list.rb', line 154
def tree_order_next
deeper = self.first_child
if deeper.nil?
return self.list_end
else
return deeper
end
end
|