Class: Yerba::Sequence
- Inherits:
-
Object
show all
- Includes:
- Enumerable, Node
- Defined in:
- lib/yerba/sequence.rb
Instance Attribute Summary
Attributes included from Node
#key, #location, #selector
Instance Method Summary
collapse
Methods included from Node
#connected?, #document, #file_path, included, #line
Constructor Details
#initialize(array = nil) ⇒ Sequence
Returns a new instance of Sequence.
8
9
10
11
12
|
# File 'lib/yerba/sequence.rb', line 8
def initialize(array = nil)
init_node(nil, nil, nil, nil, nil, nil)
@data = array.is_a?(Array) ? array : []
end
|
Instance Method Details
#<<(item) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/yerba/sequence.rb', line 55
def <<(item)
if connected?
case item
when Map
document.insert_object(@selector, item.to_hash)
when Hash
document.insert_object(@selector, item)
when Scalar
document.insert(@selector, item.to_yaml)
else
formatted = format_for_insert(item.to_s)
document.insert(@selector, formatted)
end
else
@data << item
end
self
end
|
#[](index) ⇒ Object
14
15
16
17
18
19
20
21
|
# File 'lib/yerba/sequence.rb', line 14
def [](index)
if connected?
new_path = "#{@selector}[#{index}]"
document[new_path]
else
@data[index]
end
end
|
#collection_style ⇒ Object
296
297
298
|
# File 'lib/yerba/sequence.rb', line 296
def collection_style
@collection_style || document&.get_collection_style(@selector)
end
|
#collection_style=(style) ⇒ Object
300
301
302
303
304
|
# File 'lib/yerba/sequence.rb', line 300
def collection_style=(style)
document&.set_collection_style(@selector, style)
@collection_style = style
end
|
#concat(items) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/yerba/sequence.rb', line 75
def concat(items)
if connected?
hashes = items.map do |item|
case item
when Map then item.to_hash
when Hash then item
else { value: item.to_s }
end
end
document.insert_objects(@selector, hashes)
else
@data.concat(items)
end
self
end
|
#delete ⇒ Object
261
262
263
|
# File 'lib/yerba/sequence.rb', line 261
def delete
document&.delete(@selector)
end
|
#delete_at(index) ⇒ Object
233
234
235
236
237
|
# File 'lib/yerba/sequence.rb', line 233
def delete_at(index)
document&.remove_at(@selector, index)
self
end
|
#delete_if ⇒ Object
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
# File 'lib/yerba/sequence.rb', line 239
def delete_if
return enum_for(:delete_if) unless block_given?
indices_to_remove = []
length.times do |index|
indices_to_remove << index if yield self[index]
end
indices_to_remove.reverse_each do |index|
document&.remove_at(@selector, index)
end
self
end
|
#dig(*keys) ⇒ Object
38
39
40
41
42
43
44
|
# File 'lib/yerba/sequence.rb', line 38
def dig(*keys)
if connected?
keys.reduce(self) { |node, key| node.nil? ? nil : node[key] }
else
@data.dig(*keys)
end
end
|
#each ⇒ Object
93
94
95
96
97
|
# File 'lib/yerba/sequence.rb', line 93
def each
return enum_for(:each) unless block_given?
length.times { |index| yield self[index] }
end
|
#fetch(index) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/yerba/sequence.rb', line 23
def fetch(index)
if connected?
new_path = "#{@selector}[#{index}]"
result = document[new_path]
if result.nil?
raise IndexError, "index #{index} outside of sequence bounds: 0...#{length}"
end
result
else
@data.fetch(index)
end
end
|
#find_by(selector = nil, value = nil, **criteria) ⇒ Object
99
100
101
102
103
|
# File 'lib/yerba/sequence.rb', line 99
def find_by(selector = nil, value = nil, **criteria)
index = index_of(selector, value, **criteria)
self[index] if index
end
|
#first ⇒ Object
219
220
221
|
# File 'lib/yerba/sequence.rb', line 219
def first
self[0]
end
|
#index_of(selector = nil, value = nil, **criteria) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/yerba/sequence.rb', line 124
def index_of(selector = nil, value = nil, **criteria)
if selector && value.nil? && criteria.empty?
all_values = document&.value_at(@selector)
return nil unless all_values.is_a?(Array)
return all_values.index(selector.to_s)
end
criteria[selector] = value if selector && value
pairs = expand_nested_criteria(criteria)
indices = nil
pairs.each do |field, expected|
field_string = field.to_s
if field_string.include?("[]")
matching = nested_indices_for(field_string, expected)
else
all_values = document&.value_at(@selector)
next unless all_values.is_a?(Array)
matching = all_values.each_with_index.filter_map do |item, index|
next unless item.is_a?(Hash)
actual = dig_into(item, field_string)
index if actual.to_s == expected.to_s
end
end
indices = indices ? indices & matching : matching
end
indices&.first
end
|
#indices_of(selector = nil, value = nil, **criteria) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/yerba/sequence.rb', line 162
def indices_of(selector = nil, value = nil, **criteria)
if selector && value.nil? && criteria.empty?
all_values = document&.value_at(@selector)
if all_values.is_a?(Array)
return all_values.each_with_index.filter_map { |actual, index| index if actual.to_s == selector.to_s }
end
return []
end
criteria[selector] = value if selector && value
pairs = expand_nested_criteria(criteria)
indices = nil
pairs.each do |field, expected|
field_string = field.to_s
if field_string.include?("[]")
matching = nested_indices_for(field_string, expected)
else
all_values = document&.value_at(@selector)
next unless all_values.is_a?(Array)
matching = all_values.each_with_index.filter_map do |item, index|
next unless item.is_a?(Hash)
actual = dig_into(item, field_string)
index if actual.to_s == expected.to_s
end
end
indices = indices ? indices & matching : matching
end
indices || []
end
|
#inspect ⇒ Object
284
285
286
287
288
289
290
291
292
293
294
|
# File 'lib/yerba/sequence.rb', line 284
def inspect
list = items
preview = list.first(5).map(&:inspect).join(", ")
suffix = list.length > 5 ? ", ... (#{list.length} items)" : ""
if @selector
"#<Yerba::Sequence selector=#{@selector.inspect} [#{preview}#{suffix}]>"
else
"#<Yerba::Sequence [#{preview}#{suffix}]>"
end
end
|
#last ⇒ Object
223
224
225
|
# File 'lib/yerba/sequence.rb', line 223
def last
self[length - 1] end
|
#length ⇒ Object
Also known as:
size
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/yerba/sequence.rb', line 202
def length
if connected?
scalar_items = document.value_at("#{@selector}[]")
if scalar_items.is_a?(Array) && !scalar_items.empty?
scalar_items.length
else
data = document.value_at(@selector)
data.is_a?(Array) ? data.length : 0
end
else
@data.length
end
end
|
#pluck(*fields) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/yerba/sequence.rb', line 109
def pluck(*fields)
return [] unless connected?
all_values = document.value_at(@selector)
return [] unless all_values.is_a?(Array)
if fields.length == 1
all_values.map { |item| item.is_a?(Hash) ? item[fields.first.to_s] : item }
else
all_values.map { |item| fields.map(&:to_s).map { |field| item.is_a?(Hash) ? item[field] : nil } }
end
end
|
#remove(value) ⇒ Object
227
228
229
230
231
|
# File 'lib/yerba/sequence.rb', line 227
def remove(value)
document&.remove(@selector, value.to_s)
self
end
|
#sequence_indent ⇒ Object
306
307
308
|
# File 'lib/yerba/sequence.rb', line 306
def sequence_indent
@sequence_indent || document&.get_sequence_indent(@selector)
end
|
#sequence_indent=(style) ⇒ Object
310
311
312
313
314
|
# File 'lib/yerba/sequence.rb', line 310
def sequence_indent=(style)
document&.set_sequence_indent(@selector, style)
@sequence_indent = style
end
|
#sort(by: nil, case_sensitive: false) ⇒ Object
255
256
257
258
259
|
# File 'lib/yerba/sequence.rb', line 255
def sort(by: nil, case_sensitive: false)
document&.sort(@selector, by: by, case_sensitive: case_sensitive)
self
end
|
#to_a ⇒ Object
Also known as:
to_ary
269
270
271
|
# File 'lib/yerba/sequence.rb', line 269
def to_a
@data || items
end
|
#to_yaml ⇒ Object
274
275
276
277
278
279
280
281
282
|
# File 'lib/yerba/sequence.rb', line 274
def to_yaml
items.map do |item|
case item
when Scalar then "- #{item.to_yaml}"
when Map then "- #{item.to_yaml.gsub("\n", "\n ")}"
else "- #{item}"
end
end.join("\n")
end
|
#value ⇒ Object
265
266
267
|
# File 'lib/yerba/sequence.rb', line 265
def value
items
end
|
#value_at(index) ⇒ Object
46
47
48
49
50
51
52
53
|
# File 'lib/yerba/sequence.rb', line 46
def value_at(index)
if connected?
new_path = "#{@selector}[#{index}]"
document.value_at(new_path)
else
@data[index]
end
end
|
#where(selector = nil, value = nil, **criteria) ⇒ Object
105
106
107
|
# File 'lib/yerba/sequence.rb', line 105
def where(selector = nil, value = nil, **criteria)
QueryResult.new(self, indices_of(selector, value, **criteria))
end
|