Class: AIXM::Concerns::Association::Array
- Defined in:
- lib/aixm/concerns/association.rb
Instance Method Summary collapse
-
#duplicates ⇒ Array<Array<AIXM::Feature>>
Find equal or identical duplicates on a has_many association.
-
#find(object) ⇒ AIXM::Concerns::Association::Array
Find equal objects on a has_many association.
-
#find_by(klass, attributes = {}) ⇒ AIXM::Concerns::Association::Array
Find objects of the given class and optionally with the given attribute values on a has_many association.
Instance Method Details
#duplicates ⇒ Array<Array<AIXM::Feature>>
Find equal or identical duplicates on a has_many association.
372 373 374 375 376 |
# File 'lib/aixm/concerns/association.rb', line 372 def duplicates AIXM::Concerns::Memoize.method :to_uid do group_by { _1.to_uid.to_s }.select { |_, a| a.count > 1 }.map(&:last) end end |
#find(object) ⇒ AIXM::Concerns::Association::Array
Find equal objects on a has_many association.
This may seem redundant at first, but keep in mind that two instances of AIXM::CLASSES
which implement ‘#to_uid` are considered equal if they are instances of the same class and both their UIDs as calculated by `#to_uid` are equal. Attributes which are not part of the `#to_uid` calculation are irrelevant!
346 347 348 349 350 351 352 353 |
# File 'lib/aixm/concerns/association.rb', line 346 def find(object) klass = object.__class__ self.class.new( select do |element| element.kind_of?(klass) && element == object end ) end |
#find_by(klass, attributes = {}) ⇒ AIXM::Concerns::Association::Array
Find objects of the given class and optionally with the given attribute values on a has_many association.
The class can either be declared by passing the class itself or by passing a shortcut symbol as listed in AIXM::CLASSES
.
309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/aixm/concerns/association.rb', line 309 def find_by(klass, attributes={}) if klass.is_a? Symbol klass = AIXM::CLASSES[klass]&.to_class || fail(ArgumentError, "unknown class shortcut `#{klass}'") end self.class.new( select do |element| if element.kind_of? klass attributes.all? { |a, v| element.send(a) == v } end end ) end |