Class: AIXM::Association::Array

Inherits:
Array
  • Object
show all
Defined in:
lib/aixm/association.rb

Instance Method Summary collapse

Instance Method Details

#duplicatesAIXM::Association::Array

Find equal or identical duplicates on a has_many association.

Examples:

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]

Returns:


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!

Examples:

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

Parameters:

  • object (Object)

    instance of class listed in AIXM::CLASSES

Returns:


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.

Examples:

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

Parameters:

  • 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

  • attributes (Hash) (defaults to: {})

    search attributes by their values

Returns:


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