Class: Sequel::Model::Associations::AssociationReflection
- Inherits:
-
Hash
- Object
- Hash
- Sequel::Model::Associations::AssociationReflection
- Defined in:
- lib/sequel_model/association_reflection.rb
Overview
AssociationReflection is a Hash subclass that keeps information on Sequel::Model associations. It provides a few methods to reduce the amount of internal code duplication. It should not be instantiated by the user.
Constant Summary collapse
- RECIPROCAL_ASSOCIATIONS =
{:many_to_one=>:one_to_many, :one_to_many=>:many_to_one, :many_to_many=>:many_to_many}
Instance Method Summary collapse
-
#associated_class ⇒ Object
The class associated to the current model class via this association.
-
#associated_primary_key ⇒ Object
The associated class’s primary key (used for caching).
-
#reciprocal ⇒ Object
Returns/sets the reciprocal association variable, if one exists.
-
#select ⇒ Object
The columns to select when loading the association.
Instance Method Details
#associated_class ⇒ Object
The class associated to the current model class via this association
11 12 13 |
# File 'lib/sequel_model/association_reflection.rb', line 11 def associated_class self[:class] ||= self[:class_name].constantize end |
#associated_primary_key ⇒ Object
The associated class’s primary key (used for caching)
16 17 18 |
# File 'lib/sequel_model/association_reflection.rb', line 16 def associated_primary_key self[:associated_primary_key] ||= associated_class.primary_key end |
#reciprocal ⇒ Object
Returns/sets the reciprocal association variable, if one exists
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sequel_model/association_reflection.rb', line 21 def reciprocal return self[:reciprocal] if include?(:reciprocal) reciprocal_type = RECIPROCAL_ASSOCIATIONS[self[:type]] if reciprocal_type == :many_to_many left_key = self[:left_key] right_key = self[:right_key] join_table = self[:join_table] associated_class.all_association_reflections.each do |assoc_reflect| if assoc_reflect[:type] == :many_to_many && assoc_reflect[:left_key] == right_key \ && assoc_reflect[:right_key] == left_key && assoc_reflect[:join_table] == join_table return self[:reciprocal] = assoc_reflect[:name] end end else key = self[:key] associated_class.all_association_reflections.each do |assoc_reflect| if assoc_reflect[:type] == reciprocal_type && assoc_reflect[:key] == key return self[:reciprocal] = assoc_reflect[:name] end end end self[:reciprocal] = nil end |
#select ⇒ Object
The columns to select when loading the association
46 47 48 |
# File 'lib/sequel_model/association_reflection.rb', line 46 def select self[:select] ||= associated_class.table_name.* end |