Class: Yerba::Sequence

Inherits:
Object
  • 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, #source

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
# 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, plain: true)
    else
      document.insert(@selector, item.to_s, style: detect_quote_style)
    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_styleObject



326
327
328
# File 'lib/yerba/sequence.rb', line 326

def collection_style
  @collection_style || document&.get_collection_style(@selector)
end

#collection_style=(style) ⇒ Object



330
331
332
333
334
# File 'lib/yerba/sequence.rb', line 330

def collection_style=(style)
  document&.set_collection_style(@selector, style)

  @collection_style = style
end

#concat(items) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/yerba/sequence.rb', line 74

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

#deleteObject

Deletes the entire sequence node from the YAML document.

Removes the sequence at its selector path from the parent document. Has no effect if the sequence is not connected to a document.

Returns the document, or nil if the sequence is not connected to a document.

document = Yerba::Document.parse(<<~YAML)
items:
  - name: Ruby
  - name: Rust
YAML

document["items"].delete
document.to_s # => "{}\n"


287
288
289
# File 'lib/yerba/sequence.rb', line 287

def delete
  document&.delete(@selector)
end

#delete_at(index) ⇒ Object



244
245
246
247
248
# File 'lib/yerba/sequence.rb', line 244

def delete_at(index)
  document&.remove_at(@selector, index)

  self
end

#delete_ifObject



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/yerba/sequence.rb', line 250

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

#eachObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/yerba/sequence.rb', line 92

def each(&)
  return enum_for(:each) unless block_given?

  if connected?
    items = document.get_all("#{@selector}[]")

    if items.length == length
      items.each(&)

      return self
    end
  end

  length.times { |index| yield self[index] }

  self
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



110
111
112
113
114
# File 'lib/yerba/sequence.rb', line 110

def find_by(selector = nil, value = nil, **criteria)
  index = index_of(selector, value, **criteria)

  self[index] if index
end

#firstObject



230
231
232
# File 'lib/yerba/sequence.rb', line 230

def first
  self[0]
end

#index_of(selector = nil, value = nil, **criteria) ⇒ Object



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
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/yerba/sequence.rb', line 135

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



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
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/yerba/sequence.rb', line 173

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

#inspectObject



314
315
316
317
318
319
320
321
322
323
324
# File 'lib/yerba/sequence.rb', line 314

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

#lastObject



234
235
236
# File 'lib/yerba/sequence.rb', line 234

def last
  self[length - 1] # rubocop:disable Style/NegativeArrayIndex
end

#lengthObject Also known as: size



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/yerba/sequence.rb', line 213

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/yerba/sequence.rb', line 120

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



238
239
240
241
242
# File 'lib/yerba/sequence.rb', line 238

def remove(value)
  document&.remove(@selector, value.to_s)

  self
end

#sequence_indentObject



336
337
338
# File 'lib/yerba/sequence.rb', line 336

def sequence_indent
  @sequence_indent || document&.get_sequence_indent(@selector)
end

#sequence_indent=(style) ⇒ Object



340
341
342
343
344
# File 'lib/yerba/sequence.rb', line 340

def sequence_indent=(style)
  document&.set_sequence_indent(@selector, style)

  @sequence_indent = style
end

#sort(by: nil, case_sensitive: false) ⇒ Object



266
267
268
269
270
# File 'lib/yerba/sequence.rb', line 266

def sort(by: nil, case_sensitive: false)
  document&.sort(@selector, by: by, case_sensitive: case_sensitive)

  self
end

#to_aObject Also known as: to_ary



295
296
297
# File 'lib/yerba/sequence.rb', line 295

def to_a
  @data || items
end

#to_sObject



310
311
312
# File 'lib/yerba/sequence.rb', line 310

def to_s
  source || super
end

#to_yamlObject



300
301
302
303
304
305
306
307
308
# File 'lib/yerba/sequence.rb', line 300

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

#valueObject



291
292
293
# File 'lib/yerba/sequence.rb', line 291

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



116
117
118
# File 'lib/yerba/sequence.rb', line 116

def where(selector = nil, value = nil, **criteria)
  QueryResult.new(self, indices_of(selector, value, **criteria))
end