Class: Ecoportal::API::Common::Content::ArrayModel
- Inherits:
-
DoubleModel
- Object
- BaseModel
- DoubleModel
- Ecoportal::API::Common::Content::ArrayModel
- Includes:
- Enumerable
- Defined in:
- lib/ecoportal/api/common/content/array_model.rb
Overview
- 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 included from DoubleModel::Diffable
DoubleModel::Diffable::DIFF_CLASS
Constants included from DoubleModel::Base
Class Attribute Summary collapse
-
.order_matters ⇒ Object
Returns the value of attribute order_matters.
-
.uniq ⇒ Object
Returns the value of attribute uniq.
Attributes included from DoubleModel::Parented
Class Method Summary collapse
-
.same_type?(a, b) ⇒ Boolean
trueif both elements have same behaviour.
Instance Method Summary collapse
-
#&(other) ⇒ ArrayModel
Intersect.
-
#+(other) ⇒ Object
Concat to new.
-
#-(other) ⇒ ArrayModel
Subtract.
-
#<(other) ⇒ Object
Resets the
Arrayby keeping its reference and adds the value(s). -
#<<(value) ⇒ Object
Adds an element to the subjacent
Array. -
#==(other) ⇒ Object
Compares with an
Arrayor anotherArrayModel. -
#[](pos) ⇒ Date, ...
Retrieves the element of a certain position.
-
#[]=(pos, value) ⇒ Date, ...
Sets the element of a certain position.
-
#_items ⇒ Array
The array element represented by this object.
-
#added_items ⇒ Array<Date>, ...
(also: #added)
Array
, Array , Array<Number] the elements that have been added. -
#clear! ⇒ Object
Clears the
Arraykeeping its reference. - #concat!(values) ⇒ Object
-
#delete!(*values) ⇒ Object
Deletes
valuesfrom theArray. -
#deleted_items ⇒ Array<Date>, ...
(also: #deleted)
Array
, Array , Array<Number] the elements that have been deleted. -
#dup ⇒ ArrayModel
A copy of the current object.
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#include?(value) ⇒ Boolean
trueifvalueis present,falseotherwise. - #include_all?(*value) ⇒ Boolean
- #include_any?(*value) ⇒ Boolean
-
#index(value) ⇒ Integer
The position of the element in the
Array. -
#initialize(doc = [], parent: self, key: nil, read_only: self.class.read_only?) ⇒ ArrayModel
constructor
A new instance of ArrayModel.
- #insert_one(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object
- #length ⇒ Object
-
#move(value, pos: NOT_USED, _before: NOT_USED, _after: NOT_USED) ⇒ Object
TODO.
-
#new_from(other) ⇒ ArrayModel
A new object with the current class.
- #order_matters? ⇒ Boolean
- #present? ⇒ Boolean
- #push!(*values) ⇒ Object (also: #push)
-
#swap(val_1, val_2) ⇒ Integer
Swaps two values' positions.
-
#to_a ⇒ Array
A copy of the
Arrayelements. - #to_s(delimiter: "|") ⇒ Object
- #uniq? ⇒ Boolean
-
#|(other) ⇒ ArrayModel
Join.
Methods inherited from DoubleModel
Methods included from DoubleModel::Diffable
Methods included from Includer
Constructor Details
#initialize(doc = [], parent: self, key: nil, read_only: self.class.read_only?) ⇒ ArrayModel
Returns a new instance of ArrayModel.
35 36 37 38 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 35 def initialize(doc = [], parent: self, key: nil, read_only: self.class.read_only?) doc = [] unless doc.is_a?(Array) super end |
Class Attribute Details
.order_matters ⇒ Object
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 |
.uniq ⇒ Object
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.
25 26 27 28 29 30 |
# 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
215 216 217 218 219 220 221 222 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 215 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
200 201 202 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 200 def +(other) new_from(to_a + into_a(other)) end |
#-(other) ⇒ ArrayModel
Subtract
227 228 229 230 231 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 227 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)
187 188 189 190 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 187 def <(other) _items.clear self << other end |
#<<(value) ⇒ Object
if the class variable uniq is true, it skips duplicates
Adds an element to the subjacent Array
164 165 166 167 168 169 170 171 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 164 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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 117 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
101 102 103 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 101 def [](pos) _items[pos] end |
#[]=(pos, value) ⇒ Date, ...
Sets the element of a certain position
109 110 111 112 113 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 109 def []=(pos, value) _items[pos] = value on_change self[pos] end |
#_items ⇒ Array
Returns the array element represented by this object.
67 68 69 70 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 67 def _items replace_doc([]) unless doc.is_a?(Array) doc.tap {|d| d.uniq! if uniq?} end |
#added_items ⇒ Array<Date>, ... Also known as: added
Returns Array
150 151 152 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 150 def added_items (doc || []) - (original_doc || []) end |
#clear! ⇒ Object
Clears the Array keeping its reference
193 194 195 196 197 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 193 def clear! _items.clear on_change self end |
#concat!(values) ⇒ Object
same as #push! but for multiple elements
181 182 183 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 181 def concat!(values) self << values end |
#delete!(*values) ⇒ Object
Deletes values from the Array
234 235 236 237 238 239 240 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 234 def delete!(*values) values.map do |v| deletion!(v) end.tap do |_r| on_change end end |
#deleted_items ⇒ Array<Date>, ... Also known as: deleted
Returns Array
157 158 159 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 157 def deleted_items (original_doc || []) - (doc || []) end |
#dup ⇒ ArrayModel
Returns a copy of the current object.
89 90 91 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 89 def dup new_from(to_a) end |
#each(&block) ⇒ Object
60 61 62 63 64 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 60 def each(&block) return to_enum(:each) unless block _items.each(&block) end |
#empty? ⇒ Boolean
52 53 54 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 52 def empty? count&.zero? end |
#include?(value) ⇒ Boolean
Returns true if value is present, false otherwise.
136 137 138 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 136 def include?(value) _items.include?(value) end |
#include_all?(*value) ⇒ Boolean
144 145 146 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 144 def include_all?(*value) value.all? {|v| _items.include?(v)} end |
#include_any?(*value) ⇒ Boolean
140 141 142 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 140 def include_any?(*value) value.any? {|v| _items.include?(v)} end |
#index(value) ⇒ Integer
Returns the position of the element in the Array.
94 95 96 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 94 def index(value) _items.index(value) end |
#insert_one(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 256 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 |
#length ⇒ Object
48 49 50 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 48 def length count end |
#move(value, pos: NOT_USED, _before: NOT_USED, _after: NOT_USED) ⇒ Object
TODO
278 279 280 281 282 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 278 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.
84 85 86 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 84 def new_from(other) self.class.new(into_a(other)) end |
#order_matters? ⇒ Boolean
40 41 42 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 40 def order_matters? self.class.order_matters end |
#present? ⇒ Boolean
56 57 58 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 56 def present? count&.positive? end |
#push!(*values) ⇒ Object Also known as: push
174 175 176 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 174 def push!(*values) self << values end |
#swap(val_1, val_2) ⇒ Integer
this will work with first instances when not uniq?
Swaps two values' positions
247 248 249 250 251 252 253 254 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 247 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_a ⇒ Array
Returns a copy of the Array elements.
78 79 80 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 78 def to_a _items.slice(0..-1) end |
#to_s(delimiter: "|") ⇒ Object
72 73 74 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 72 def to_s(delimiter: "|") map(&:to_s).join(delimiter) end |
#uniq? ⇒ Boolean
44 45 46 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 44 def uniq? self.class.uniq end |
#|(other) ⇒ ArrayModel
Join
207 208 209 210 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 207 def |(other) oth = new_from(other) - self new_from(to_a + oth.to_a) end |