class AIXM::Association::Array

Public Instance Methods

duplicates() click to toggle source

Find equal or identical duplicates on a has_many association.

@example

class Blog
  include AIXM::Association
  has_many :posts
end
class Post
  include AIXM::Association
  belongs_to :blog
end
blog, post = Blog.new, Post.new
blog.add_posts([post, post])
blog.posts.duplicates   # => [post]

@return [AIXM::Association::Array]

    # File lib/aixm/association.rb
358 def duplicates
359   AIXM::Memoize.method :to_uid do
360     self.class.new(
361       select.with_index do |element, index|
362         index != self.index(element)
363       end
364     )
365   end
366 end
find(object) click to toggle source

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!

@example

class Blog
  include AIXM::Association
  has_many :items, accept: %i(post picture)
end
class Post
  include AIXM::Association
  belongs_to :blog, as: :item
  attr_accessor :title
end
blog, post = Blog.new, Post.new
blog.add_item(post)
blog.items.find(post) == [post]   # => true

@param object [Object] instance of class listed in AIXM::CLASSES @return [AIXM::Association::Array]

    # File lib/aixm/association.rb
333 def find(object)
334   klass = object.__class__
335   self.class.new(
336     select do |element|
337       element.kind_of?(klass) && element == object
338     end
339   )
340 end
find_by(klass, attributes={}) click to toggle source

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.

@example

class Blog
  include AIXM::Association
  has_many :items, accept: %i(post picture)
end
class Post
  include AIXM::Association
  belongs_to :blog, as: :item
  attr_accessor :title
end
class Picture
  include AIXM::Association
  belongs_to :blog, as: :item
end
blog, post, picture = Blog.new, Post.new, Picture.new
post.title = "title"
blog.add_item(post)
blog.add_item(picture)
blog.items.find_by(:post) == [post]                    # => true
blog.items.find_by(Post) == [post]                     # => true
blog.items.find_by(:post, title: "title") == [post]    # => true
blog.items.find_by(Object) == [post, picture]          # => true

@param klass [Class, Symbol] class (e.g. AIXM::Feature::Airport,

AIXM::Feature::NavigationalAid::VOR) or shortcut symbol (e.g.
:airport or :vor) as listed in AIXM::CLASSES

@param attributes [Hash] search attributes by their values @return [AIXM::Association::Array]

    # File lib/aixm/association.rb
294 def find_by(klass, attributes={})
295   if klass.is_a? Symbol
296     klass = AIXM::CLASSES[klass]&.to_class || fail(ArgumentError, "unknown class shortcut `#{klass}'")
297   end
298   self.class.new(
299     select do |element|
300       if element.kind_of? klass
301         attributes.reduce(true) do |memo, (attribute, value)|
302           memo && element.send(attribute) == value
303         end
304       end
305     end
306   )
307 end