Class: Ecoportal::API::Common::Content::ArrayModel

Inherits:
DoubleModel
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ecoportal/api/common/content/array_model.rb

Overview

Note:
  • Its purpose is to handle an Array of basic objects (i.e. Date, String, Number)

Class to handle a plain Array embedded in a Hashed model.

Defined Under Namespace

Classes: TypeMismatchedComparison

Constant Summary

Constants inherited from DoubleModel

DoubleModel::NOT_USED

Constants included from ClassHelpers

ClassHelpers::NOT_USED

Class Attribute Summary collapse

Attributes inherited from DoubleModel

#_key, #_parent, #_read_only

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DoubleModel

#_doc_key, #as_json, #as_update, #consolidate!, #dirty?, #doc, embeds_many, embeds_one, enforce!, #key, #key=, key?, new_uuid, #original_doc, pass_reader, pass_writer, passarray, passboolean, passdate, passforced, passkey, passthrough, #print_pretty, read_only!, read_only?, #read_only?, #replace_doc, #reset!, #resolved_doc_key, #root, #to_json

Methods included from ClassHelpers

#inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #new_class, #resolve_class, #to_constant, #to_time, #uid, #used_param?

Constructor Details

#initialize(doc = [], parent: self, key: nil, read_only: self.class.read_only?) ⇒ ArrayModel

Returns a new instance of ArrayModel.



34
35
36
# File 'lib/ecoportal/api/common/content/array_model.rb', line 34

def initialize(doc = [], parent: self, key: nil, read_only: self.class.read_only?)
  super(doc, parent: parent, key: key, read_only: read_only)
end

Class Attribute Details

.order_mattersObject

Returns the value of attribute order_matters.



20
21
22
# File 'lib/ecoportal/api/common/content/array_model.rb', line 20

def order_matters
  @order_matters
end

.uniqObject

Returns the value of attribute uniq.



20
21
22
# File 'lib/ecoportal/api/common/content/array_model.rb', line 20

def uniq
  @uniq
end

Class Method Details

.same_type?(a, b) ⇒ Boolean

Returns true if both elements have same behaviour.

Parameters:

Returns:

  • (Boolean)

    true if both elements have same behaviour



25
26
27
28
29
# File 'lib/ecoportal/api/common/content/array_model.rb', line 25

def same_type?(a, b) # rubocop:disable Naming/MethodParameterName
  msg = "To use this comparison both objects should be `ArrayModel`"
  raise msg unless a.is_a?(ArrayModel) && b.is_a?(ArrayModel)
  (a.order_matters? == b.order_matters?) && (a.uniq? == b.uniq?)
end

Instance Method Details

#&(other) ⇒ ArrayModel

Intersect

Parameters:

  • other (Object, Array<Object>, ArrayModel)

    the value(s) to be deleted

Returns:

  • (ArrayModel)

    a new object instance with the intersection done



197
198
199
200
201
202
203
204
# File 'lib/ecoportal/api/common/content/array_model.rb', line 197

def &(other)
  dup.tap do |out|
    dup.tap do |delta|
      delta.delete!(*into_a(other))
      out.delete!(*into_a(delta))
    end
  end
end

#+(other) ⇒ Object

Concat to new



182
183
184
# File 'lib/ecoportal/api/common/content/array_model.rb', line 182

def +(other)
  new_from(to_a + into_a(other))
end

#-(other) ⇒ ArrayModel

Subtract

Parameters:

  • value (Object, Array<Object>, ArrayModel)

    the value(s) to be deleted

Returns:

  • (ArrayModel)

    a copy of the object with the elements subtracted



209
210
211
212
213
# File 'lib/ecoportal/api/common/content/array_model.rb', line 209

def -(other)
  dup.tap do |copy|
    copy.delete!(*into_a(other))
  end
end

#<(other) ⇒ Object

Resets the Array by keeping its reference and adds the value(s)

Parameters:

  • other (Object, Array<Object>, ArrayModel)

    the value(s) to be added



169
170
171
172
# File 'lib/ecoportal/api/common/content/array_model.rb', line 169

def <(other)
  _items.clear
  self << other
end

#<<(value) ⇒ Object

Note:

if the class variable uniq is true, it skips duplicates

Adds an element to the subjacent Array



148
149
150
151
152
153
154
# File 'lib/ecoportal/api/common/content/array_model.rb', line 148

def <<(value)
  _items.concat(into_a(value)).tap do |doc|
    doc.uniq! if uniq?
  end
  on_change
  self
end

#==(other) ⇒ Object

Compares with an Array or another ArrayModel

Parameters:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ecoportal/api/common/content/array_model.rb', line 115

def ==(other)
  return true  if equal?(other)
  return false unless (other.class == self.class) || other.is_a?(Array)

  case other
  when Array
    self == new_from(other)
  when ArrayModel
    raise TypeMismatchedComparison.new(this: self, that: other) unless self.class.same_type?(self, other)

    if order_matters?
      _items == other.to_a
    else
      (_items - other.to_a).empty? && (other.to_a - _items).empty?
    end
  end
end

#[](pos) ⇒ Date, ...

Retrieves the element of a certain position

Parameters:

  • pos (Integer)

    the position of the element

Returns:

  • (Date, String, Number)


99
100
101
# File 'lib/ecoportal/api/common/content/array_model.rb', line 99

def [](pos)
  _items[pos]
end

#[]=(pos, value) ⇒ Date, ...

Sets the element of a certain position

Parameters:

  • pos (Integer)

    the position of the element

  • value (String, Date, Number)

    the element

Returns:

  • (Date, String, Number)


107
108
109
110
111
# File 'lib/ecoportal/api/common/content/array_model.rb', line 107

def []=(pos, value)
  _items[pos] = value
  on_change
  self[pos]
end

#_itemsArray

Returns the array element represented by this object.

Returns:

  • (Array)

    the array element represented by this object



65
66
67
68
# File 'lib/ecoportal/api/common/content/array_model.rb', line 65

def _items
  replace_doc([]) unless doc.is_a?(Array)
  doc.tap {|d| d.uniq! if uniq?}
end

#clear!Object

Clears the Array keeping its reference



175
176
177
178
179
# File 'lib/ecoportal/api/common/content/array_model.rb', line 175

def clear!
  _items.clear
  on_change
  self
end

#concat!(values) ⇒ Object

Note:

same as #push! but for multiple elements

See Also:



163
164
165
# File 'lib/ecoportal/api/common/content/array_model.rb', line 163

def concat!(values)
  self << values
end

#delete!(*values) ⇒ Object

Deletes values from the Array



216
217
218
219
220
221
222
# File 'lib/ecoportal/api/common/content/array_model.rb', line 216

def delete!(*values)
  values.map do |v|
    deletion!(v)
  end.tap do |_r|
    on_change
  end
end

#dupArrayModel

Returns a copy of the current object.

Returns:



87
88
89
# File 'lib/ecoportal/api/common/content/array_model.rb', line 87

def dup
  new_from(to_a)
end

#each(&block) ⇒ Object



58
59
60
61
62
# File 'lib/ecoportal/api/common/content/array_model.rb', line 58

def each(&block)
  return to_enum(:each) unless block

  _items.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/ecoportal/api/common/content/array_model.rb', line 50

def empty?
  count&.zero?
end

#include?(value) ⇒ Boolean

Returns true if value is present, false otherwise.

Returns:

  • (Boolean)

    true if value is present, false otherwise



134
135
136
# File 'lib/ecoportal/api/common/content/array_model.rb', line 134

def include?(value)
  _items.include?(value)
end

#include_all?(*value) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/ecoportal/api/common/content/array_model.rb', line 142

def include_all?(*value)
  value.all? {|v| _items.include?(v)}
end

#include_any?(*value) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/ecoportal/api/common/content/array_model.rb', line 138

def include_any?(*value)
  value.any? {|v| _items.include?(v)}
end

#index(value) ⇒ Integer

Returns the position of the element in the Array.

Returns:

  • (Integer)

    the position of the element in the Array



92
93
94
# File 'lib/ecoportal/api/common/content/array_model.rb', line 92

def index(value)
  _items.index(value)
end

#insert_one(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/ecoportal/api/common/content/array_model.rb', line 238

def insert_one(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
  idx = index(value)
  return idx if idx && uniq?

  pos =
    if used_param?(pos) && pos
      pos
    elsif used_param?(before) && before
      index(before)
    elsif used_param?(after) && after
      if (idx = index(after)) then idx + 1 end
    end

  # use last position as default
  pos ||= length
  pos.tap do |_i|
    _items.insert(pos, value)
    on_change
  end
end

#lengthObject



46
47
48
# File 'lib/ecoportal/api/common/content/array_model.rb', line 46

def length
  count
end

#move(value, pos: NOT_USED, _before: NOT_USED, _after: NOT_USED) ⇒ Object

TODO



260
261
262
263
264
# File 'lib/ecoportal/api/common/content/array_model.rb', line 260

def move(value, pos: NOT_USED, _before: NOT_USED, _after: NOT_USED)
  return    unless (idx = index(value))
  on_change unless idx == pos
  pos
end

#new_from(other) ⇒ ArrayModel

Returns a new object with the current class.

Parameters:

  • other (Object, Array<Object>, ArrayModel)

    the value(s) of the new object

Returns:

  • (ArrayModel)

    a new object with the current class



82
83
84
# File 'lib/ecoportal/api/common/content/array_model.rb', line 82

def new_from(other)
  self.class.new(into_a(other))
end

#order_matters?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/ecoportal/api/common/content/array_model.rb', line 38

def order_matters?
  self.class.order_matters
end

#present?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/ecoportal/api/common/content/array_model.rb', line 54

def present?
  count&.positive?
end

#push!(value) ⇒ Object

See Also:



157
158
159
# File 'lib/ecoportal/api/common/content/array_model.rb', line 157

def push!(value)
  self << value
end

#swap(val_1, val_2) ⇒ Integer

Note:

this will work with first instances when not uniq?

Swaps two values' positions

Parameters:

  • val1 (Object)

    the first value to swap

  • val2 (Object)

    the second value to swap

Returns:

  • (Integer)

    the new of value1, nil if it wasn't moved



229
230
231
232
233
234
235
236
# File 'lib/ecoportal/api/common/content/array_model.rb', line 229

def swap(val_1, val_2)
  index(val_2).tap do |dest|
    if dest && (pos = index(val_1))
      _items[dest] = val_1
      _items[pos]  = val_2
    end
  end
end

#to_aArray

Returns a copy of the Array elements.

Returns:

  • (Array)

    a copy of the Array elements

See Also:



76
77
78
# File 'lib/ecoportal/api/common/content/array_model.rb', line 76

def to_a
  _items.slice(0..-1)
end

#to_s(delimiter: "|") ⇒ Object



70
71
72
# File 'lib/ecoportal/api/common/content/array_model.rb', line 70

def to_s(delimiter: "|")
  map(&:to_s).join(delimiter)
end

#uniq?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/ecoportal/api/common/content/array_model.rb', line 42

def uniq?
  self.class.uniq
end

#|(other) ⇒ ArrayModel

Join

Parameters:

  • other (Object, Array<Object>, ArrayModel)

    the value(s) to be joined

Returns:

  • (ArrayModel)

    a new object instance with the intersection done



189
190
191
192
# File 'lib/ecoportal/api/common/content/array_model.rb', line 189

def |(other)
  oth = new_from(other) - self
  new_from(to_a + oth.to_a)
end