Class: Yerba::Sequence
- Inherits:
-
Object
- Object
- Yerba::Sequence
- Includes:
- Enumerable
- Defined in:
- lib/yerba/sequence.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #[](index) ⇒ Object
- #concat(items) ⇒ Object
- #delete ⇒ Object
- #delete_at(index) ⇒ Object
- #delete_if ⇒ Object
- #each ⇒ Object
- #find_by(selector = nil, value = nil, **criteria) ⇒ Object
- #first ⇒ Object
- #index_of(selector = nil, value = nil, **criteria) ⇒ Object
- #indices_of(selector = nil, value = nil, **criteria) ⇒ Object
-
#initialize(document_or_array = nil, selector = nil, location = nil, key = nil) ⇒ Sequence
constructor
A new instance of Sequence.
- #inspect ⇒ Object
- #last ⇒ Object
- #length ⇒ Object (also: #size)
- #pluck(*fields) ⇒ Object
- #remove(value) ⇒ Object
- #sort(by: nil, case_sensitive: false) ⇒ Object
- #to_a ⇒ Object (also: #to_ary)
- #to_yaml ⇒ Object
- #value ⇒ Object
- #where(selector = nil, value = nil, **criteria) ⇒ Object
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
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/yerba/sequence.rb', line 7 def key @key end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
7 8 9 |
# File 'lib/yerba/sequence.rb', line 7 def location @location end |
#selector ⇒ Object (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 |
#concat(items) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/yerba/sequence.rb', line 58 def concat(items) if @document 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
234 235 236 |
# File 'lib/yerba/sequence.rb', line 234 def delete @document&.delete(@selector) end |
#delete_at(index) ⇒ Object
206 207 208 209 210 |
# File 'lib/yerba/sequence.rb', line 206 def delete_at(index) @document&.remove_at(@selector, index) self end |
#delete_if ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/yerba/sequence.rb', line 212 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 |
#each ⇒ Object
76 77 78 79 80 |
# File 'lib/yerba/sequence.rb', line 76 def each return enum_for(:each) unless block_given? length.times { |index| yield self[index] } end |
#find_by(selector = nil, value = nil, **criteria) ⇒ Object
82 83 84 85 86 |
# File 'lib/yerba/sequence.rb', line 82 def find_by(selector = nil, value = nil, **criteria) index = index_of(selector, value, **criteria) self[index] if index end |
#first ⇒ Object
192 193 194 |
# File 'lib/yerba/sequence.rb', line 192 def first self[0] end |
#index_of(selector = nil, value = nil, **criteria) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/yerba/sequence.rb', line 107 def index_of(selector = nil, value = nil, **criteria) if selector && value.nil? && criteria.empty? values = @document&.get("#{@selector}[]") return values.index(selector) if values.is_a?(Array) return nil end criteria[selector] = value if selector && value pairs = (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 values = @document&.get("#{@selector}[].#{field_string}") next unless values.is_a?(Array) matching = values.each_with_index.filter_map { |actual, index| index if actual == expected } end indices = indices ? indices & matching : matching end indices&.first end |
#indices_of(selector = nil, value = nil, **criteria) ⇒ Object
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 172 173 |
# File 'lib/yerba/sequence.rb', line 140 def indices_of(selector = nil, value = nil, **criteria) if selector && value.nil? && criteria.empty? values = @document&.get("#{@selector}[]") if values.is_a?(Array) return values.each_with_index.filter_map { |actual, index| index if actual == selector } end return [] end criteria[selector] = value if selector && value pairs = (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 values = @document&.get("#{@selector}[].#{field_string}") next unless values.is_a?(Array) matching = values.each_with_index.filter_map { |actual, index| index if actual == expected } end indices = indices ? indices & matching : matching end indices || [] end |
#inspect ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/yerba/sequence.rb', line 257 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
196 197 198 |
# File 'lib/yerba/sequence.rb', line 196 def last self[length - 1] # rubocop:disable Style/NegativeArrayIndex end |
#length ⇒ Object Also known as: size
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/yerba/sequence.rb', line 175 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 |
#pluck(*fields) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/yerba/sequence.rb', line 92 def pluck(*fields) return [] unless @document all_values = @document.get_value(@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
200 201 202 203 204 |
# File 'lib/yerba/sequence.rb', line 200 def remove(value) @document&.remove(@selector, value.to_s) self end |
#sort(by: nil, case_sensitive: false) ⇒ Object
228 229 230 231 232 |
# File 'lib/yerba/sequence.rb', line 228 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
242 243 244 |
# File 'lib/yerba/sequence.rb', line 242 def to_a @data || items end |
#to_yaml ⇒ Object
247 248 249 250 251 252 253 254 255 |
# File 'lib/yerba/sequence.rb', line 247 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
238 239 240 |
# File 'lib/yerba/sequence.rb', line 238 def value items end |
#where(selector = nil, value = nil, **criteria) ⇒ Object
88 89 90 |
# File 'lib/yerba/sequence.rb', line 88 def where(selector = nil, value = nil, **criteria) QueryResult.new(self, indices_of(selector, value, **criteria)) end |