lib/ecoportal/api/common/content/array_model.rb in ecoportal-api-v2-1.1.8 vs lib/ecoportal/api/common/content/array_model.rb in ecoportal-api-v2-2.0.0

- old
+ new

@@ -6,12 +6,12 @@ # @note # - Its purpose is to handle an Array of basic objects (i.e. `Date`, `String`, `Number`) class ArrayModel < Content::DoubleModel class TypeMismatchedComparison < StandardError def initialize(this: nil, that: msg = "Trying to compare objects with different behavior.") - msg += " From object with 'order_matters: #{this.order_matters?}' and 'uniq: #{this.uniq?}'." if this - msg += " To object where 'order_matters: #{that.order_matters?}' and 'uniq: #{that.uniq?}'." if that + msg << " From object with 'order_matters: #{this.order_matters?}' and 'uniq: #{this.uniq?}'." if this + msg << " To object where 'order_matters: #{that.order_matters?}' and 'uniq: #{that.uniq?}'." if that super(msg) end end include Enumerable @@ -20,11 +20,11 @@ attr_accessor :order_matters, :uniq # @param a [ArrayModel] # @param b [ArrayModel] # @return [Boolean] `true` if both elements have same behaviour - def same_type?(a, b) + 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 end @@ -55,10 +55,11 @@ count&.positive? end def each(&block) return to_enum(:each) unless block + _items.each(&block) end # @return [Array] the array element represented by this object def _items @@ -271,15 +272,17 @@ private 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 def deletion!(value) return _items.delete(value) if uniq? return unless (idx = _items.index(value)) + _items.slice!(idx) end end end end