Class: Yerba::Sequence

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/yerba/sequence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document_or_array = nil, selector = nil, location = nil, key = nil) ⇒ Sequence

Returns a new instance of Sequence.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/yerba/sequence.rb', line 9

def initialize(document_or_array = nil, selector = nil, location = nil, key = nil)
  if document_or_array.is_a?(Document)
    @document = document_or_array
    @selector = selector
    @location = location
    @key = key
    @data = nil
  elsif document_or_array.is_a?(Array)
    @document = nil
    @selector = nil
    @location = nil
    @data = document_or_array
  else
    @document = nil
    @selector = nil
    @location = nil
    @data = []
  end
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/yerba/sequence.rb', line 7

def key
  @key
end

#locationObject (readonly)

Returns the value of attribute location.



7
8
9
# File 'lib/yerba/sequence.rb', line 7

def location
  @location
end

#selectorObject (readonly)

Returns the value of attribute selector.



7
8
9
# File 'lib/yerba/sequence.rb', line 7

def selector
  @selector
end

Instance Method Details

#<<(item) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/yerba/sequence.rb', line 38

def <<(item)
  if @document
    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



29
30
31
32
33
34
35
36
# File 'lib/yerba/sequence.rb', line 29

def [](index)
  if @document
    new_path = "#{@selector}[#{index}]"
    @document[new_path]
  else
    @data[index]
  end
end

#deleteObject



122
123
124
# File 'lib/yerba/sequence.rb', line 122

def delete
  @document&.delete(@selector)
end

#delete_at(index) ⇒ Object



94
95
96
97
98
# File 'lib/yerba/sequence.rb', line 94

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

  self
end

#delete_ifObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/yerba/sequence.rb', line 100

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

#eachObject



58
59
60
61
62
# File 'lib/yerba/sequence.rb', line 58

def each
  return enum_for(:each) unless block_given?

  length.times { |i| yield self[i] }
end

#firstObject



80
81
82
# File 'lib/yerba/sequence.rb', line 80

def first
  self[0]
end

#inspectObject



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/yerba/sequence.rb', line 145

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



84
85
86
# File 'lib/yerba/sequence.rb', line 84

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

#lengthObject Also known as: size



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/yerba/sequence.rb', line 64

def length
  if @document
    scalar_items = @document.get("#{@selector}[]")

    if scalar_items.is_a?(Array) && !scalar_items.empty?
      scalar_items.length
    else
      data = @document.get_value(@selector)
      data.is_a?(Array) ? data.length : 0
    end
  else
    @data.length
  end
end

#remove(value) ⇒ Object



88
89
90
91
92
# File 'lib/yerba/sequence.rb', line 88

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

  self
end

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



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

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



130
131
132
# File 'lib/yerba/sequence.rb', line 130

def to_a
  @data || items
end

#to_yamlObject



135
136
137
138
139
140
141
142
143
# File 'lib/yerba/sequence.rb', line 135

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



126
127
128
# File 'lib/yerba/sequence.rb', line 126

def value
  items
end