Class: 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 methods to reduce internal code duplication. It should not be instantiated by the user.
Constant Summary collapse
- ASSOCIATION_TYPES =
[:many_to_one, :one_to_many, :many_to_many]
- RECIPROCAL_ASSOCIATIONS =
{:many_to_one=>:one_to_many, :one_to_many=>:many_to_one, :many_to_many=>:many_to_many}
Instance Method Summary collapse
-
#_add_method ⇒ Object
Name symbol for add internal association method.
-
#_dataset_method ⇒ Object
Name symbol for _dataset association method.
-
#_remove_all_method ⇒ Object
Name symbol for _remove_all internal association method.
-
#_remove_method ⇒ Object
Name symbol for remove internal association method.
-
#_setter_method ⇒ Object
Name symbol for setter association method.
-
#add_method ⇒ Object
Name symbol for add_ association method.
-
#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).
-
#association_method ⇒ Object
Name symbol for association method, the same as the name of the association.
-
#dataset_helper_method ⇒ Object
Name symbol for _helper internal association method.
-
#dataset_method ⇒ Object
Name symbol for dataset association method.
-
#dataset_need_primary_key? ⇒ Boolean
Whether the dataset needs a primary key to function.
-
#default_join_table ⇒ Object
Name symbol for default join table.
-
#default_left_key ⇒ Object
Default foreign key name symbol for key in associated table that points to current table’s primary key.
-
#default_right_key ⇒ Object
Default foreign key name symbol for foreign key in current model’s table that points to the given association’s table’s primary key.
-
#eager_graph_lazy_dataset? ⇒ Boolean
Whether to eagerly graph a lazy dataset.
-
#need_associated_primary_key? ⇒ Boolean
Whether the associated object needs a primary key to be added/removed.
-
#reciprocal ⇒ Object
Returns/sets the reciprocal association variable, if one exists.
-
#remove_all_method ⇒ Object
Name symbol for remove_all_ association method.
-
#remove_method ⇒ Object
Name symbol for remove_ association method.
-
#select ⇒ Object
The columns to select when loading the association.
-
#set_reciprocal_to_self? ⇒ Boolean
Whether to set the reciprocal to the current object when loading.
-
#setter_method ⇒ Object
Name symbol for setter association method.
-
#single_associated_object? ⇒ Boolean
Whether the association should return a single object or multiple objects.
Methods inherited from Hash
#&, #case, #sql_expr, #sql_negate, #sql_or, #|, #~
Instance Method Details
#_add_method ⇒ Object
Name symbol for add internal association method
12 13 14 |
# File 'lib/sequel_model/association_reflection.rb', line 12 def _add_method :"_add_#{self[:name].to_s.singularize}" end |
#_dataset_method ⇒ Object
Name symbol for _dataset association method
17 18 19 |
# File 'lib/sequel_model/association_reflection.rb', line 17 def _dataset_method :"_#{self[:name]}_dataset" end |
#_remove_all_method ⇒ Object
Name symbol for _remove_all internal association method
22 23 24 |
# File 'lib/sequel_model/association_reflection.rb', line 22 def _remove_all_method :"_remove_all_#{self[:name]}" end |
#_remove_method ⇒ Object
Name symbol for remove internal association method
27 28 29 |
# File 'lib/sequel_model/association_reflection.rb', line 27 def _remove_method :"_remove_#{self[:name].to_s.singularize}" end |
#_setter_method ⇒ Object
Name symbol for setter association method
32 33 34 |
# File 'lib/sequel_model/association_reflection.rb', line 32 def _setter_method :"_#{self[:name]}=" end |
#add_method ⇒ Object
Name symbol for add_ association method
37 38 39 |
# File 'lib/sequel_model/association_reflection.rb', line 37 def add_method :"add_#{self[:name].to_s.singularize}" end |
#associated_class ⇒ Object
The class associated to the current model class via this association
47 48 49 |
# File 'lib/sequel_model/association_reflection.rb', line 47 def associated_class self[:class] ||= self[:class_name].constantize end |
#associated_primary_key ⇒ Object
The associated class’s primary key (used for caching)
52 53 54 |
# File 'lib/sequel_model/association_reflection.rb', line 52 def associated_primary_key self[:associated_primary_key] ||= associated_class.primary_key end |
#association_method ⇒ Object
Name symbol for association method, the same as the name of the association.
42 43 44 |
# File 'lib/sequel_model/association_reflection.rb', line 42 def association_method self[:name] end |
#dataset_helper_method ⇒ Object
Name symbol for _helper internal association method
62 63 64 |
# File 'lib/sequel_model/association_reflection.rb', line 62 def dataset_helper_method :"_#{self[:name]}_dataset_helper" end |
#dataset_method ⇒ Object
Name symbol for dataset association method
57 58 59 |
# File 'lib/sequel_model/association_reflection.rb', line 57 def dataset_method :"#{self[:name]}_dataset" end |
#dataset_need_primary_key? ⇒ Boolean
Whether the dataset needs a primary key to function
67 68 69 |
# File 'lib/sequel_model/association_reflection.rb', line 67 def dataset_need_primary_key? self[:type] != :many_to_one end |
#default_join_table ⇒ Object
Name symbol for default join table
72 73 74 75 |
# File 'lib/sequel_model/association_reflection.rb', line 72 def default_join_table ([self[:class_name].demodulize, self[:model].name.to_s.demodulize]. \ map{|i| i.pluralize.underscore}.sort.join('_')).to_sym end |
#default_left_key ⇒ Object
Default foreign key name symbol for key in associated table that points to current table’s primary key.
79 80 81 |
# File 'lib/sequel_model/association_reflection.rb', line 79 def default_left_key :"#{self[:model].name.to_s.demodulize.underscore}_id" end |
#default_right_key ⇒ Object
Default foreign key name symbol for foreign key in current model’s table that points to the given association’s table’s primary key.
85 86 87 |
# File 'lib/sequel_model/association_reflection.rb', line 85 def default_right_key :"#{self[:type] == :many_to_one ? self[:name] : self[:name].to_s.singularize}_id" end |
#eager_graph_lazy_dataset? ⇒ Boolean
Whether to eagerly graph a lazy dataset
90 91 92 |
# File 'lib/sequel_model/association_reflection.rb', line 90 def eager_graph_lazy_dataset? self[:type] != :many_to_one or self[:key].nil? end |
#need_associated_primary_key? ⇒ Boolean
Whether the associated object needs a primary key to be added/removed
95 96 97 |
# File 'lib/sequel_model/association_reflection.rb', line 95 def need_associated_primary_key? self[:type] == :many_to_many end |
#reciprocal ⇒ Object
Returns/sets the reciprocal association variable, if one exists
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/sequel_model/association_reflection.rb', line 100 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 |
#remove_all_method ⇒ Object
Name symbol for remove_all_ association method
125 126 127 |
# File 'lib/sequel_model/association_reflection.rb', line 125 def remove_all_method :"remove_all_#{self[:name]}" end |
#remove_method ⇒ Object
Name symbol for remove_ association method
130 131 132 |
# File 'lib/sequel_model/association_reflection.rb', line 130 def remove_method :"remove_#{self[:name].to_s.singularize}" end |
#select ⇒ Object
The columns to select when loading the association
135 136 137 138 |
# File 'lib/sequel_model/association_reflection.rb', line 135 def select return self[:select] if include?(:select) self[:select] = self[:type] == :many_to_many ? associated_class.table_name.* : nil end |
#set_reciprocal_to_self? ⇒ Boolean
Whether to set the reciprocal to the current object when loading
141 142 143 |
# File 'lib/sequel_model/association_reflection.rb', line 141 def set_reciprocal_to_self? self[:type] == :one_to_many end |
#setter_method ⇒ Object
Name symbol for setter association method
146 147 148 |
# File 'lib/sequel_model/association_reflection.rb', line 146 def setter_method :"#{self[:name]}=" end |
#single_associated_object? ⇒ Boolean
Whether the association should return a single object or multiple objects.
151 152 153 |
# File 'lib/sequel_model/association_reflection.rb', line 151 def single_associated_object? self[:type] == :many_to_one end |