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
- #delete ⇒ Object
- #delete_at(index) ⇒ Object
- #delete_if ⇒ Object
- #each ⇒ Object
- #first ⇒ 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)
- #remove(value) ⇒ Object
- #sort(by: nil, case_sensitive: false) ⇒ Object
- #to_a ⇒ Object (also: #to_ary)
- #to_yaml ⇒ Object
- #value ⇒ 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 |
#delete ⇒ Object
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_if ⇒ Object
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 |
#each ⇒ Object
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 |
#first ⇒ Object
80 81 82 |
# File 'lib/yerba/sequence.rb', line 80 def first self[0] end |
#inspect ⇒ Object
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 |
#last ⇒ Object
84 85 86 |
# File 'lib/yerba/sequence.rb', line 84 def last self[length - 1] # rubocop:disable Style/NegativeArrayIndex end |
#length ⇒ Object 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_a ⇒ Object Also known as: to_ary
130 131 132 |
# File 'lib/yerba/sequence.rb', line 130 def to_a @data || items end |
#to_yaml ⇒ Object
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 |
#value ⇒ Object
126 127 128 |
# File 'lib/yerba/sequence.rb', line 126 def value items end |