class Ecoportal::API::Common::Content::ArrayModel
Class to handle a plain Array embedded in a Hashed model. @note
- Its purpose is to handle an Array of basic objects (i.e. `Date`, `String`, `Number`)
Attributes
Public Class Methods
Ecoportal::API::Common::Content::DoubleModel::new
# File lib/ecoportal/api/common/content/array_model.rb, line 36 def initialize(doc = [], parent: self, key: nil) super(doc, parent: parent, key: key) end
@param a [ArrayModel] @param b [ArrayModel] @return [Boolean] `true` if both elements have same behaviour
# File lib/ecoportal/api/common/content/array_model.rb, line 29 def same_type?(a, b) raise "To use this comparison both objects should be `ArrayModel`" unless a.is_a?(ArrayModel) && b.is_a?(ArrayModel) (a.order_matters? == b.order_matters?) && (a.uniq? == b.uniq?) end
Public Instance Methods
Intersect @param value [Object, Array<Object>, ArrayModel] the value(s) to be deleted @return [ArrayModel] a new object instance with the intersection done
# File lib/ecoportal/api/common/content/array_model.rb, line 182 def &(value) self.dup.tap do |out| self.dup.tap do |delta| delta.delete!(*into_a(value)) out.delete!(*into_a(delta)) end end end
Concat to new
# File lib/ecoportal/api/common/content/array_model.rb, line 167 def +(value) new_from(self.to_a + into_a(value)) end
Subtract @param value [Object, Array<Object>, ArrayModel] the value(s) to be deleted @return [ArrayModel] a copy of the object with the elements subtracted
# File lib/ecoportal/api/common/content/array_model.rb, line 194 def -(value) self.dup.tap do |copy| copy.delete!(*into_a(value)) end end
Resets the `Array` by keeping its reference and adds the value(s) @param value [Object, Array<Object>, ArrayModel] the value(s) to be added @param values [Array]
# File lib/ecoportal/api/common/content/array_model.rb, line 154 def <(values) _items.clear self << values end
Adds an element to the subjacent `Array` @note if the class variable `uniq` is `true`, it skips duplicates
# File lib/ecoportal/api/common/content/array_model.rb, line 132 def <<(value) _items.concat(into_a(value)).tap do |doc| doc.uniq! if uniq? end on_change self end
Compares with an `Array` or another `ArrayModel` @param a [ArrayModel, Array]
# File lib/ecoportal/api/common/content/array_model.rb, line 99 def ==(a) return true if self.equal?(a) return false unless (a.class == self.class) || a.is_a?(Array) case a when Array self == new_from(a) when ArrayModel return true if raise TypeMismatchedComparison.new(this: self, that: a) unless self.class.same_type?(self, a) if self.order_matters? _items == a.to_a else (_items - a.to_a).empty? && (a.to_a - _items).empty? end end end
Retrieves the element of a certain position @param pos [Integer] the position of the element @return [Date, String, Number]
# File lib/ecoportal/api/common/content/array_model.rb, line 83 def [](pos) _items[pos] end
Sets the element of a certain position @param pos [Integer] the position of the element @param value [String, Date, Number] the element @return [Date, String, Number]
# File lib/ecoportal/api/common/content/array_model.rb, line 91 def []=(post, value) _items[pos] = value on_change self[pos] end
@return [Array] the array element represented by this object
# File lib/ecoportal/api/common/content/array_model.rb, line 53 def _items replace_doc([]) unless doc.is_a?(Array) doc.tap {|d| d.uniq! if uniq?} end
Clears the `Array` keeping its reference
# File lib/ecoportal/api/common/content/array_model.rb, line 160 def clear! _items.clear on_change self end
Deletes `values` from the `Array`
# File lib/ecoportal/api/common/content/array_model.rb, line 201 def delete!(*values) values.map do |v| deletion!(v) end.tap do |r| on_change end end
@return [ArrayModel] a copy of the current object
# File lib/ecoportal/api/common/content/array_model.rb, line 71 def dup new_from(to_a) end
# File lib/ecoportal/api/common/content/array_model.rb, line 47 def each(&block) return to_enum(:each) unless block _items.each(&block) end
# File lib/ecoportal/api/common/content/array_model.rb, line 44 def empty?; count == 0; end
@return [Boolean] `true` if `value` is present, `false` otherwise
# File lib/ecoportal/api/common/content/array_model.rb, line 118 def include?(value) _items.include?(value) end
# File lib/ecoportal/api/common/content/array_model.rb, line 126 def include_all?(*value) value.all? {|v| _items.include?(v)} end
# File lib/ecoportal/api/common/content/array_model.rb, line 122 def include_any?(*value) value.any? {|v| _items.include?(v)} end
@return [Integer] the position of the element in the `Array`
# File lib/ecoportal/api/common/content/array_model.rb, line 76 def index(value) _items.index(value) end
# File lib/ecoportal/api/common/content/array_model.rb, line 223 def insert_one(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED) i = index(value) return i if (i && uniq?) pos = case when used_param?(pos) pos || length when used_param?(before) before ? index(before) : length when used_param?(after) if after if i = index(after) then i + 1 end else length end else length end pos.tap do |i| unless !i _items.insert(pos, value) on_change end end end
# File lib/ecoportal/api/common/content/array_model.rb, line 43 def length; count; end
TODO
# File lib/ecoportal/api/common/content/array_model.rb, line 250 def move(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED) if i = index(value) unless i == pos on_change end pos end end
@param value [Object, Array<Object>, ArrayModel] the value(s) of the new object @return [ArrayModel] a new object with the current class
# File lib/ecoportal/api/common/content/array_model.rb, line 66 def new_from(value) self.class.new(into_a(value)) end
# File lib/ecoportal/api/common/content/array_model.rb, line 40 def order_matters?; self.class.order_matters; end
# File lib/ecoportal/api/common/content/array_model.rb, line 45 def present?; count > 0; end
@see <<
# File lib/ecoportal/api/common/content/array_model.rb, line 141 def push!(value) self << value end
Swaps two values' positions @note this will work with first instances when not `uniq?` @param val1 [Object] the first value to swap @param val2 [Object] the second value to swap @return [Integer] the new of `value1`, `nil` if it wasn't moved
# File lib/ecoportal/api/common/content/array_model.rb, line 214 def swap(value1, value2) index(value2).tap do |dest| if dest && pos = index(value1) _items[dest] = value1 _items[pos] = value2 end end end
@see #_items @return [Array] a copy of the `Array` elements
# File lib/ecoportal/api/common/content/array_model.rb, line 60 def to_a _items.slice(0..-1) end
# File lib/ecoportal/api/common/content/array_model.rb, line 41 def uniq?; self.class.uniq; end
Join @param value [Object, Array<Object>, ArrayModel] the value(s) to be joined @return [ArrayModel] a new object instance with the intersection done
# File lib/ecoportal/api/common/content/array_model.rb, line 174 def |(value) new = new_from(value) - self new_from(to_a + new.to_a) end
Protected Instance Methods
# File lib/ecoportal/api/common/content/array_model.rb, line 262 def on_change # to be overriden by child classes end
Private Instance Methods
# File lib/ecoportal/api/common/content/array_model.rb, line 274 def deletion!(value) if !uniq? if i = _items.index(value) _items.slice!(i) end else _items.delete(value) end end
# File lib/ecoportal/api/common/content/array_model.rb, line 268 def into_a(value) raise "Can't convert to 'Array' a 'Hash', as is a key_value pair Enumerable" if value.is_a?(Hash) return value.to_a.slice(0..-1) if value.is_a?(Enumerable) [].push(value).compact end