Class: Ecoportal::API::Common::Content::ArrayModel
- Inherits:
-
DoubleModel
- Object
- Common::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 inherited from DoubleModel
Constants included from ClassHelpers
Class Attribute Summary collapse
-
.order_matters ⇒ Object
Returns the value of attribute order_matters.
-
.uniq ⇒ Object
Returns the value of attribute uniq.
Attributes inherited from DoubleModel
Class Method Summary collapse
-
.same_type?(a, b) ⇒ Boolean
trueif both elements have same behaviour.
Instance Method Summary collapse
-
#&(value) ⇒ ArrayModel
Intersect.
-
#+(value) ⇒ Object
Concat to new.
-
#-(value) ⇒ ArrayModel
Subtract.
-
#<(values) ⇒ Object
Resets the
Arrayby keeping its reference and adds the value(s). -
#<<(value) ⇒ Object
Adds an element to the subjacent
Array. -
#==(a) ⇒ Object
Compares with an
Arrayor anotherArrayModel. -
#[](pos) ⇒ Date, ...
Retrieves the element of a certain position.
-
#[]=(post, value) ⇒ Date, ...
Sets the element of a certain position.
-
#_items ⇒ Array
The array element represented by this object.
-
#clear! ⇒ Object
Clears the
Arraykeeping its reference. - #concat!(values) ⇒ Object
-
#delete!(*values) ⇒ Object
Deletes
valuesfrom theArray. -
#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) ⇒ 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(value) ⇒ ArrayModel
A new object with the current class.
- #order_matters? ⇒ Boolean
- #present? ⇒ Boolean
- #push!(value) ⇒ Object
-
#swap(value1, value2) ⇒ Integer
Swaps two values' positions.
-
#to_a ⇒ Array
A copy of the
Arrayelements. - #to_s(delimiter: "|") ⇒ Object
- #uniq? ⇒ Boolean
-
#|(value) ⇒ ArrayModel
Join.
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, #replace_doc, #reset!, #root, #to_json
Methods included from ClassHelpers
#inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #new_class, #resolve_class, #to_constant, #to_time, #used_param?
Constructor Details
#initialize(doc = [], parent: self, key: nil) ⇒ ArrayModel
Returns a new instance of ArrayModel.
36 37 38 |
# 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 |
Class Attribute Details
.order_matters ⇒ Object
Returns the value of attribute order_matters.
24 25 26 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 24 def order_matters @order_matters end |
.uniq ⇒ Object
Returns the value of attribute uniq.
24 25 26 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 24 def uniq @uniq end |
Class Method Details
.same_type?(a, b) ⇒ Boolean
Returns true if both elements have same behaviour.
29 30 31 32 |
# 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 |
Instance Method Details
#&(value) ⇒ ArrayModel
Intersect
186 187 188 189 190 191 192 193 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 186 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) ⇒ Object
Concat to new
171 172 173 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 171 def +(value) new_from(self.to_a + into_a(value)) end |
#-(value) ⇒ ArrayModel
Subtract
198 199 200 201 202 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 198 def -(value) self.dup.tap do |copy| copy.delete!(*into_a(value)) end end |
#<(values) ⇒ Object
Resets the Array by keeping its reference and adds the value(s)
158 159 160 161 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 158 def <(values) _items.clear self << values end |
#<<(value) ⇒ Object
if the class variable uniq is true, it skips duplicates
Adds an element to the subjacent Array
136 137 138 139 140 141 142 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 136 def <<(value) _items.concat(into_a(value)).tap do |doc| doc.uniq! if uniq? end on_change self end |
#==(a) ⇒ Object
Compares with an Array or another ArrayModel
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 103 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) ⇒ Date, ...
Retrieves the element of a certain position
87 88 89 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 87 def [](pos) _items[pos] end |
#[]=(post, value) ⇒ Date, ...
Sets the element of a certain position
95 96 97 98 99 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 95 def []=(post, value) _items[pos] = value on_change self[pos] end |
#_items ⇒ Array
Returns the array element represented by this object.
53 54 55 56 |
# 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 |
#clear! ⇒ Object
Clears the Array keeping its reference
164 165 166 167 168 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 164 def clear! _items.clear on_change self end |
#concat!(values) ⇒ Object
same as #push! but for multiple elements
151 152 153 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 151 def concat!(values) self << values end |
#delete!(*values) ⇒ Object
Deletes values from the Array
205 206 207 208 209 210 211 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 205 def delete!(*values) values.map do |v| deletion!(v) end.tap do |r| on_change end end |
#dup ⇒ ArrayModel
Returns a copy of the current object.
75 76 77 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 75 def dup new_from(to_a) end |
#each(&block) ⇒ Object
47 48 49 50 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 47 def each(&block) return to_enum(:each) unless block _items.each(&block) end |
#empty? ⇒ Boolean
44 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 44 def empty?; count == 0; end |
#include?(value) ⇒ Boolean
Returns true if value is present, false otherwise.
122 123 124 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 122 def include?(value) _items.include?(value) end |
#include_all?(*value) ⇒ Boolean
130 131 132 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 130 def include_all?(*value) value.all? {|v| _items.include?(v)} end |
#include_any?(*value) ⇒ Boolean
126 127 128 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 126 def include_any?(*value) value.any? {|v| _items.include?(v)} end |
#index(value) ⇒ Integer
Returns the position of the element in the Array.
80 81 82 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 80 def index(value) _items.index(value) end |
#insert_one(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 227 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 pos when used_param?(before) && before index(before) when used_param?(after) && after if i = index(after) then i + 1 end end pos ||= length pos.tap do |i| _items.insert(pos, value) on_change end end |
#length ⇒ Object
43 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 43 def length; count; end |
#move(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object
TODO
247 248 249 250 251 252 253 254 255 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 247 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) ⇒ ArrayModel
Returns a new object with the current class.
70 71 72 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 70 def new_from(value) self.class.new(into_a(value)) end |
#order_matters? ⇒ Boolean
40 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 40 def order_matters?; self.class.order_matters; end |
#present? ⇒ Boolean
45 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 45 def present?; count > 0; end |
#push!(value) ⇒ Object
145 146 147 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 145 def push!(value) self << value end |
#swap(value1, value2) ⇒ Integer
this will work with first instances when not uniq?
Swaps two values' positions
218 219 220 221 222 223 224 225 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 218 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 ⇒ Array
Returns a copy of the Array elements.
64 65 66 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 64 def to_a _items.slice(0..-1) end |
#to_s(delimiter: "|") ⇒ Object
58 59 60 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 58 def to_s(delimiter: "|") map(&:to_s).join(delimiter) end |
#uniq? ⇒ Boolean
41 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 41 def uniq?; self.class.uniq; end |
#|(value) ⇒ ArrayModel
Join
178 179 180 181 |
# File 'lib/ecoportal/api/common/content/array_model.rb', line 178 def |(value) new = new_from(value) - self new_from(to_a + new.to_a) end |