Class: RareMap::Relation

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/rare_map/relation.rb

Overview

RareMap::Relation defines one of has_one, has_many and has_many_through relations of a database table.

Author:

  • Wei-Ming Wu

Constant Summary collapse

HAS_ONE =

The :has_one association.

:has_one
BELONGS_TO =

The :belongs_to association.

:belongs_to
HAS_MANY =

The :has_many association.

:has_many
HAS_MANY_THROUGH =

The :has_many_through association.

:has_many_through
RELATIONS =

All three kinds of relations

[HAS_ONE, BELONGS_TO, HAS_MANY, HAS_MANY_THROUGH]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, foreign_key, table, through = nil) ⇒ Relation

Creates a Relation.

Parameters:

  • type (Symbol)

    the type, :has_one or :has_many or :has_many_through

  • foreign_key (String)

    the foreign key of this Relation based on

  • table (String)

    the table of this Relation refers to

  • through (String, nil) (defaults to: nil)

    the table of this Relation goes through

Raises:



36
37
38
39
# File 'lib/rare_map/relation.rb', line 36

def initialize(type, foreign_key, table, through = nil)
  raise RelationNotDefinedError, 'Relation type not defined.' unless RELATIONS.include? type
  @type, @foreign_key, @table, @through = type, foreign_key, table, through
end

Instance Attribute Details

#foreign_keyString (readonly)

Returns the foreign key of this Relation based on.

Returns:

  • (String)

    the foreign key of this Relation based on



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rare_map/relation.rb', line 15

class Relation
  # The :has_one association.
  HAS_ONE = :has_one
  # The :belongs_to association.
  BELONGS_TO = :belongs_to
  # The :has_many association.
  HAS_MANY = :has_many
  # The :has_many_through association.
  HAS_MANY_THROUGH = :has_many_through
  # All three kinds of relations
  RELATIONS = [HAS_ONE, BELONGS_TO, HAS_MANY, HAS_MANY_THROUGH]
  include Errors
  attr_reader :type, :foreign_key, :table, :through
  
  # Creates a Relation.
  #
  # @param type [Symbol] the type, `:has_one` or `:has_many` or `:has_many_through`
  # @param foreign_key [String] the foreign key of this Relation based on
  # @param table [String] the table of this Relation refers to
  # @param through [String, nil] the table of this Relation goes through
  # @return [Relation] the Relation object
  def initialize(type, foreign_key, table, through = nil)
    raise RelationNotDefinedError, 'Relation type not defined.' unless RELATIONS.include? type
    @type, @foreign_key, @table, @through = type, foreign_key, table, through
  end
end

#tableString (readonly)

Returns the table of this Relation refers to.

Returns:

  • (String)

    the table of this Relation refers to



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rare_map/relation.rb', line 15

class Relation
  # The :has_one association.
  HAS_ONE = :has_one
  # The :belongs_to association.
  BELONGS_TO = :belongs_to
  # The :has_many association.
  HAS_MANY = :has_many
  # The :has_many_through association.
  HAS_MANY_THROUGH = :has_many_through
  # All three kinds of relations
  RELATIONS = [HAS_ONE, BELONGS_TO, HAS_MANY, HAS_MANY_THROUGH]
  include Errors
  attr_reader :type, :foreign_key, :table, :through
  
  # Creates a Relation.
  #
  # @param type [Symbol] the type, `:has_one` or `:has_many` or `:has_many_through`
  # @param foreign_key [String] the foreign key of this Relation based on
  # @param table [String] the table of this Relation refers to
  # @param through [String, nil] the table of this Relation goes through
  # @return [Relation] the Relation object
  def initialize(type, foreign_key, table, through = nil)
    raise RelationNotDefinedError, 'Relation type not defined.' unless RELATIONS.include? type
    @type, @foreign_key, @table, @through = type, foreign_key, table, through
  end
end

#throughString? (readonly)

Returns the table of this Relation goes through.

Returns:

  • (String, nil)

    the table of this Relation goes through



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rare_map/relation.rb', line 15

class Relation
  # The :has_one association.
  HAS_ONE = :has_one
  # The :belongs_to association.
  BELONGS_TO = :belongs_to
  # The :has_many association.
  HAS_MANY = :has_many
  # The :has_many_through association.
  HAS_MANY_THROUGH = :has_many_through
  # All three kinds of relations
  RELATIONS = [HAS_ONE, BELONGS_TO, HAS_MANY, HAS_MANY_THROUGH]
  include Errors
  attr_reader :type, :foreign_key, :table, :through
  
  # Creates a Relation.
  #
  # @param type [Symbol] the type, `:has_one` or `:has_many` or `:has_many_through`
  # @param foreign_key [String] the foreign key of this Relation based on
  # @param table [String] the table of this Relation refers to
  # @param through [String, nil] the table of this Relation goes through
  # @return [Relation] the Relation object
  def initialize(type, foreign_key, table, through = nil)
    raise RelationNotDefinedError, 'Relation type not defined.' unless RELATIONS.include? type
    @type, @foreign_key, @table, @through = type, foreign_key, table, through
  end
end

#typeSymbol (readonly)

Returns the type of relation, :has_one or :has_many or :has_many_through.

Returns:

  • (Symbol)

    the type of relation, :has_one or :has_many or :has_many_through



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rare_map/relation.rb', line 15

class Relation
  # The :has_one association.
  HAS_ONE = :has_one
  # The :belongs_to association.
  BELONGS_TO = :belongs_to
  # The :has_many association.
  HAS_MANY = :has_many
  # The :has_many_through association.
  HAS_MANY_THROUGH = :has_many_through
  # All three kinds of relations
  RELATIONS = [HAS_ONE, BELONGS_TO, HAS_MANY, HAS_MANY_THROUGH]
  include Errors
  attr_reader :type, :foreign_key, :table, :through
  
  # Creates a Relation.
  #
  # @param type [Symbol] the type, `:has_one` or `:has_many` or `:has_many_through`
  # @param foreign_key [String] the foreign key of this Relation based on
  # @param table [String] the table of this Relation refers to
  # @param through [String, nil] the table of this Relation goes through
  # @return [Relation] the Relation object
  def initialize(type, foreign_key, table, through = nil)
    raise RelationNotDefinedError, 'Relation type not defined.' unless RELATIONS.include? type
    @type, @foreign_key, @table, @through = type, foreign_key, table, through
  end
end