Class: RareMap::Relation
- Inherits:
-
Object
- Object
- RareMap::Relation
- 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.
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
-
#foreign_key ⇒ String
readonly
The foreign key of this Relation based on.
-
#table ⇒ String
readonly
The table of this Relation refers to.
-
#through ⇒ String?
readonly
The table of this Relation goes through.
-
#type ⇒ Symbol
readonly
The type of relation,
:has_oneor:has_manyor:has_many_through.
Instance Method Summary collapse
-
#initialize(type, foreign_key, table, through = nil) ⇒ Relation
constructor
Creates a Relation.
Constructor Details
#initialize(type, foreign_key, table, through = nil) ⇒ Relation
Creates a Relation.
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_key ⇒ String (readonly)
Returns 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 |
#table ⇒ String (readonly)
Returns 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 |
#through ⇒ String? (readonly)
Returns 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 |
#type ⇒ Symbol (readonly)
Returns 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 |