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

order_matters[RW]
uniq[RW]

Public Class Methods

new(doc = {}, parent: self, key: nil) click to toggle source
# File lib/ecoportal/api/common/content/array_model.rb, line 35
def initialize(doc = {}, parent: self, key: nil)
  super(doc, parent: parent, key: key)
end
same_type?(a, b) click to toggle source

@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 28
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

&(value) click to toggle source

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 181
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
+(value) click to toggle source

Concat to new

# File lib/ecoportal/api/common/content/array_model.rb, line 166
def +(value)
  new_from(self.to_a + into_a(value))
end
-(value) click to toggle source

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 193
def -(value)
  self.dup.tap do |copy|
    copy.delete!(*into_a(value))
  end
end
<(values) click to toggle source

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 153
def <(values)
  _items.clear
  self << values
end
<<(value) click to toggle source

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 131
def <<(value)
  _items.concat(into_a(value)).tap do |doc|
    doc.uniq! if uniq?
  end
  on_change
  self
end
==(a) click to toggle source

Compares with an `Array` or another `ArrayModel` @param a [ArrayModel, Array]

# File lib/ecoportal/api/common/content/array_model.rb, line 98
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
[](pos) click to toggle source

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 82
def [](pos)
  _items[pos]
end
[]=(post, value) click to toggle source

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 90
def []=(post, value)
  _items[pos] = value
  on_change
  self[pos]
end
_items() click to toggle source

@return [Array] the array element represented by this object

# File lib/ecoportal/api/common/content/array_model.rb, line 52
def _items
  replace_doc([]) unless doc.is_a?(Array)
  doc.tap {|d| d.uniq! if uniq?}
end
clear!() click to toggle source

Clears the `Array` keeping its reference

# File lib/ecoportal/api/common/content/array_model.rb, line 159
def clear!
  _items.clear
  on_change
  self
end
concat!(values) click to toggle source

@see << @note same as push! but for multiple elements

# File lib/ecoportal/api/common/content/array_model.rb, line 146
def concat!(values)
  self << values
end
delete!(*values) click to toggle source

Deletes `values` from the `Array`

# File lib/ecoportal/api/common/content/array_model.rb, line 200
def delete!(*values)
  values.map do |v|
    deletion!(v)
  end.tap do |r|
    on_change
  end
end
dup() click to toggle source

@return [ArrayModel] a copy of the current object

# File lib/ecoportal/api/common/content/array_model.rb, line 70
def dup
  new_from(to_a)
end
each(&block) click to toggle source
# File lib/ecoportal/api/common/content/array_model.rb, line 46
def each(&block)
  return to_enum(:each) unless block
  _items.each(&block)
end
empty?() click to toggle source
# File lib/ecoportal/api/common/content/array_model.rb, line 43
def empty?;   count == 0; end
include?(value) click to toggle source

@return [Boolean] `true` if `value` is present, `false` otherwise

# File lib/ecoportal/api/common/content/array_model.rb, line 117
def include?(value)
  _items.include?(value)
end
include_all?(*value) click to toggle source
# File lib/ecoportal/api/common/content/array_model.rb, line 125
def include_all?(*value)
  value.all? {|v| _items.include?(v)}
end
include_any?(*value) click to toggle source
# File lib/ecoportal/api/common/content/array_model.rb, line 121
def include_any?(*value)
  value.any? {|v| _items.include?(v)}
end
index(value) click to toggle source

@return [Integer] the position of the element in the `Array`

# File lib/ecoportal/api/common/content/array_model.rb, line 75
def index(value)
  _items.index(value)
end
insert_one(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED) click to toggle source
# File lib/ecoportal/api/common/content/array_model.rb, line 222
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
        when used_param?(before)
          index(before)
        when used_param?(after)
          if i = index(after)
            i + 1
          end
        else
          length
        end

  pos.tap do |i|
    unless !i
      _items.insert(pos, value)
      on_change
    end
  end
end
length() click to toggle source
# File lib/ecoportal/api/common/content/array_model.rb, line 42
def length;   count;      end
move(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED) click to toggle source

TODO

# File lib/ecoportal/api/common/content/array_model.rb, line 248
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
new_from(value) click to toggle source

@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 65
def new_from(value)
  self.class.new(into_a(value))
end
order_matters?() click to toggle source
# File lib/ecoportal/api/common/content/array_model.rb, line 39
def order_matters?; self.class.order_matters; end
present?() click to toggle source
# File lib/ecoportal/api/common/content/array_model.rb, line 44
def present?; count > 0;  end
push!(value) click to toggle source

@see <<

# File lib/ecoportal/api/common/content/array_model.rb, line 140
def push!(value)
  self << value
end
swap(value1, value2) click to toggle source

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 213
def swap(value1, value2)
  index(value2).tap do |dest|
    if dest && pos = index(value1)
      _items[dest] = value1
      _items[pos]  = value2
    end
  end
end
to_a() click to toggle source

@see #_items @return [Array] a copy of the `Array` elements

# File lib/ecoportal/api/common/content/array_model.rb, line 59
def to_a
  _items.slice(0..-1)
end
uniq?() click to toggle source
# File lib/ecoportal/api/common/content/array_model.rb, line 40
def uniq?;          self.class.uniq;          end
|(value) click to toggle source

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 173
def |(value)
  new = new_from(value) - self
  new_from(to_a + new.to_a)
end

Protected Instance Methods

on_change() click to toggle source
# File lib/ecoportal/api/common/content/array_model.rb, line 260
def on_change
  # to be overriden by child classes
end

Private Instance Methods

deletion!(value) click to toggle source
# File lib/ecoportal/api/common/content/array_model.rb, line 272
def deletion!(value)
  if !uniq?
    if i = _items.index(value)
      _items.slice!(i)
    end
  else
    _items.delete(value)
  end
end
into_a(value) click to toggle source
# File lib/ecoportal/api/common/content/array_model.rb, line 266
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