Class: AIXM::Association::Array
Instance Method Summary collapse
-
#duplicates ⇒ AIXM::Association::Array
Find equal or identical duplicates on a has_many association.
-
#find(object) ⇒ AIXM::Association::Array
Find equal objects on a has_many association.
-
#find_by(klass, attributes = {}) ⇒ AIXM::Association::Array
Find objects of the given class and optionally with the given attribute values on a has_many association.
Instance Method Details
#duplicates ⇒ AIXM::Association::Array
Find equal or identical duplicates on a has_many association.
358 359 360 361 362 363 364 365 366 |
# File 'lib/aixm/association.rb', line 358 def duplicates AIXM::Memoize.method :to_uid do self.class.new( select.with_index do |element, index| index != self.index(element) end ) end end |
#find(object) ⇒ AIXM::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!
333 334 335 336 337 338 339 340 |
# File 'lib/aixm/association.rb', line 333 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::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
.
294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/aixm/association.rb', line 294 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.reduce(true) do |memo, (attribute, value)| memo && element.send(attribute) == value end end end ) end |