Module: Stannum::Entities::Associations::ClassMethods

Defined in:
lib/stannum/entities/associations.rb

Overview

Class methods to extend the class when including Associations.

Instance Method Summary collapse

Instance Method Details

#association(arity, assoc_name, **options) ⇒ Symbol #association(arity, assoc_type, **options) ⇒ Symbol Also known as: define_association

Defines an association on the entity.

When an association is defined, each of the following steps is executed:

  • Adds the association to ::Associations and the .associations class method.

  • Adds the association to #association and the associated methods, such as #assign_associations, #[] and #[]=.

  • Defines reader and writer methods.

Overloads:

  • #association(arity, assoc_name, **options) ⇒ Symbol

    Defines an association with the given name. The class of the associated object is determined automatically based on the association name, or can be specified with the :class_name keyword.

    Parameters:

    • arity (:one, :many)

      :one if the association has one item, or :many if the association can have multiple items.

    • assoc_name (String, Symbol)

      the name of the association.

    • options (Hash)

      additional options for the association.

    Options Hash (**options):

    • :class_name (String)

      the name of the associated class.

    • :foreign_key (true, Hash)

      the foreign key options for the association. Can be true, or a Hash containing :name and/or :type keys.

    Returns:

    • (Symbol)

      the association name as a symbol.

  • #association(arity, assoc_type, **options) ⇒ Symbol

    Defines an association with the given class. The name of the association is determined automatically based on the association class.

    Parameters:

    • arity (:one, :many)

      :one if the association has one item, or :many if the association can have multiple items.

    • assoc_type (String, Symbol, Class)

      the type of the associated

    • options (Hash)

      additional options for the association.

    Options Hash (**options):

    • :foreign_key (true, Hash)

      the foreign key options for the association. Can be true, or a Hash containing :name and/or :type keys.

    Returns:

    • (Symbol)

      the association name as a symbol.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/stannum/entities/associations.rb', line 56

def association(arity, class_or_name, **options) # rubocop:disable Metrics/MethodLength
  assoc_class                     =
    resolve_association_class(arity)
  assoc_name, assoc_type, options =
    resolve_parameters(arity, class_or_name, options)

  association = associations.define(
    definition_class: assoc_class,
    name:             assoc_name,
    type:             assoc_type,
    options:          parse_options(assoc_name, **options)
  )
  define_foreign_key(association) if association.foreign_key?

  association.name.intern
end

#associationsStannum::Schema

Returns The associations Schema object for the Entity.

Returns:



75
76
77
# File 'lib/stannum/entities/associations.rb', line 75

def associations
  self::Associations
end

#default_foreign_key_typeClass

Returns the default type for foreign key attributes.

Returns:

  • (Class)

    the default type for foreign key attributes.



80
81
82
# File 'lib/stannum/entities/associations.rb', line 80

def default_foreign_key_type
  (defined?(primary_key_type) && primary_key_type) || Integer
end