Class: Sequel::Model::Associations::AssociationReflection
- Includes:
- Inflections
- Defined in:
- lib/sequel/model/associations.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.
Direct Known Subclasses
ManyToManyAssociationReflection, ManyToOneAssociationReflection, OneToManyAssociationReflection
Constant Summary
Constants included from Inflections
Inflections::CAMELIZE_CONVERT_REGEXP, Inflections::CAMELIZE_MODULE_REGEXP, Inflections::DASH, Inflections::DEMODULIZE_CONVERT_REGEXP, Inflections::EMPTY_STRING, Inflections::SLASH, Inflections::UNDERSCORE, Inflections::UNDERSCORE_CONVERT_REGEXP1, Inflections::UNDERSCORE_CONVERT_REGEXP2, Inflections::UNDERSCORE_CONVERT_REPLACE, Inflections::UNDERSCORE_MODULE_REGEXP, Inflections::VALID_CONSTANT_NAME_REGEXP
Instance Method Summary collapse
-
#_add_method ⇒ Object
Name symbol for the _add internal association method.
-
#_dataset_method ⇒ Object
Name symbol for the _dataset association method.
-
#_remove_all_method ⇒ Object
Name symbol for the _remove_all internal association method.
-
#_remove_method ⇒ Object
Name symbol for the _remove internal association method.
-
#_setter_method ⇒ Object
Name symbol for the _setter association method.
-
#add_method ⇒ Object
Name symbol for the add association method.
-
#associated_class ⇒ Object
The class associated to the current model class via this association.
-
#association_method ⇒ Object
Name symbol for association method, the same as the name of the association.
-
#can_have_associated_objects?(obj) ⇒ Boolean
Whether this association can have associated objects, given the current object.
-
#dataset_helper_method ⇒ Object
Name symbol for the _helper internal association method.
-
#dataset_method ⇒ Object
Name symbol for the dataset association method.
-
#dataset_need_primary_key? ⇒ Boolean
Whether the dataset needs a primary key to function, true by default.
-
#eager_graph_lazy_dataset? ⇒ Boolean
Whether to eagerly graph a lazy dataset, true by default.
-
#eager_limit_strategy ⇒ Object
The eager limit strategy to use for this dataset.
-
#eager_loading_use_associated_key? ⇒ Boolean
By default associations do not need to select a key in an associated table to eagerly load.
-
#limit_and_offset ⇒ Object
The limit and offset for this association (returned as a two element array).
-
#need_associated_primary_key? ⇒ Boolean
Whether the associated object needs a primary key to be added/removed, false by default.
-
#qualify(table, col) ⇒ Object
Qualify
col
with the given table name. -
#qualify_assoc(col) ⇒ Object
Qualify col with the associated model’s table name.
-
#qualify_cur(col) ⇒ Object
Qualify col with the current model’s table name.
-
#reciprocal ⇒ Object
Returns the reciprocal association variable, if one exists.
-
#reciprocal_array? ⇒ Boolean
Whether the reciprocal of this association returns an array of objects instead of a single object, true by default.
-
#remove_all_method ⇒ Object
Name symbol for the remove_all_ association method.
-
#remove_before_destroy? ⇒ Boolean
Whether associated objects need to be removed from the association before being destroyed in order to preserve referential integrity.
-
#remove_method ⇒ Object
Name symbol for the remove_ association method.
-
#remove_should_check_existing? ⇒ Boolean
Whether to check that an object to be disassociated is already associated to this object, false by default.
-
#returns_array? ⇒ Boolean
Whether this association returns an array of objects instead of a single object, true by default.
-
#select ⇒ Object
The columns to select when loading the association.
-
#set_reciprocal_to_self? ⇒ Boolean
Whether to set the reciprocal association to self when loading associated records, false by default.
-
#setter_method ⇒ Object
Name symbol for the setter association method.
Methods included from Inflections
clear, irregular, plural, singular, uncountable
Methods inherited from Hash
#&, #case, #sql_expr, #sql_negate, #sql_or, #|, #~
Instance Method Details
#_add_method ⇒ Object
Name symbol for the _add internal association method
21 22 23 |
# File 'lib/sequel/model/associations.rb', line 21 def _add_method :"_add_#{singularize(self[:name])}" end |
#_dataset_method ⇒ Object
Name symbol for the _dataset association method
26 27 28 |
# File 'lib/sequel/model/associations.rb', line 26 def _dataset_method :"_#{self[:name]}_dataset" end |
#_remove_all_method ⇒ Object
Name symbol for the _remove_all internal association method
31 32 33 |
# File 'lib/sequel/model/associations.rb', line 31 def _remove_all_method :"_remove_all_#{self[:name]}" end |
#_remove_method ⇒ Object
Name symbol for the _remove internal association method
36 37 38 |
# File 'lib/sequel/model/associations.rb', line 36 def _remove_method :"_remove_#{singularize(self[:name])}" end |
#_setter_method ⇒ Object
Name symbol for the _setter association method
41 42 43 |
# File 'lib/sequel/model/associations.rb', line 41 def _setter_method :"_#{self[:name]}=" end |
#add_method ⇒ Object
Name symbol for the add association method
46 47 48 |
# File 'lib/sequel/model/associations.rb', line 46 def add_method :"add_#{singularize(self[:name])}" end |
#associated_class ⇒ Object
The class associated to the current model class via this association
56 57 58 |
# File 'lib/sequel/model/associations.rb', line 56 def associated_class self[:class] ||= constantize(self[:class_name]) end |
#association_method ⇒ Object
Name symbol for association method, the same as the name of the association.
51 52 53 |
# File 'lib/sequel/model/associations.rb', line 51 def association_method self[:name] end |
#can_have_associated_objects?(obj) ⇒ Boolean
Whether this association can have associated objects, given the current object. Should be false if obj cannot have associated objects because the necessary key columns are NULL.
63 64 65 |
# File 'lib/sequel/model/associations.rb', line 63 def can_have_associated_objects?(obj) true end |
#dataset_helper_method ⇒ Object
Name symbol for the _helper internal association method
73 74 75 |
# File 'lib/sequel/model/associations.rb', line 73 def dataset_helper_method :"_#{self[:name]}_dataset_helper" end |
#dataset_method ⇒ Object
Name symbol for the dataset association method
68 69 70 |
# File 'lib/sequel/model/associations.rb', line 68 def dataset_method :"#{self[:name]}_dataset" end |
#dataset_need_primary_key? ⇒ Boolean
Whether the dataset needs a primary key to function, true by default.
78 79 80 |
# File 'lib/sequel/model/associations.rb', line 78 def dataset_need_primary_key? true end |
#eager_graph_lazy_dataset? ⇒ Boolean
Whether to eagerly graph a lazy dataset, true by default. If this is false, the association won’t respect the :eager_graph option when loading the association for a single record.
112 113 114 |
# File 'lib/sequel/model/associations.rb', line 112 def eager_graph_lazy_dataset? true end |
#eager_limit_strategy ⇒ Object
The eager limit strategy to use for this dataset.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/sequel/model/associations.rb', line 83 def eager_limit_strategy fetch(:_eager_limit_strategy) do self[:_eager_limit_strategy] = if self[:limit] case s = self.fetch(:eager_limit_strategy){self[:model].default_eager_limit_strategy || :ruby} when true ds = associated_class.dataset if ds.supports_window_functions? :window_function else :ruby end else s end else nil end end end |
#eager_loading_use_associated_key? ⇒ Boolean
By default associations do not need to select a key in an associated table to eagerly load.
105 106 107 |
# File 'lib/sequel/model/associations.rb', line 105 def eager_loading_use_associated_key? false end |
#limit_and_offset ⇒ Object
The limit and offset for this association (returned as a two element array).
117 118 119 120 121 122 123 |
# File 'lib/sequel/model/associations.rb', line 117 def limit_and_offset if (v = self[:limit]).is_a?(Array) v else [v, nil] end end |
#need_associated_primary_key? ⇒ Boolean
Whether the associated object needs a primary key to be added/removed, false by default.
127 128 129 |
# File 'lib/sequel/model/associations.rb', line 127 def need_associated_primary_key? false end |
#qualify(table, col) ⇒ Object
Qualify col
with the given table name. If col
is an array of columns, return an array of qualified columns.
133 134 135 |
# File 'lib/sequel/model/associations.rb', line 133 def qualify(table, col) transform(col){|k| SQL::QualifiedIdentifier.new(table, k)} end |
#qualify_assoc(col) ⇒ Object
Qualify col with the associated model’s table name.
138 139 140 |
# File 'lib/sequel/model/associations.rb', line 138 def qualify_assoc(col) qualify(associated_class.table_name, col) end |
#qualify_cur(col) ⇒ Object
Qualify col with the current model’s table name.
143 144 145 |
# File 'lib/sequel/model/associations.rb', line 143 def qualify_cur(col) qualify(self[:model].table_name, col) end |
#reciprocal ⇒ Object
Returns the reciprocal association variable, if one exists. The reciprocal association is the association in the associated class that is the opposite of the current association. For example, Album.many_to_one :artist and Artist.one_to_many :albums are reciprocal associations. This information is to populate reciprocal associations. For example, when you do this_artist.add_album(album) it sets album.artist to this_artist.
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/sequel/model/associations.rb', line 153 def reciprocal return self[:reciprocal] if include?(:reciprocal) r_types = Array(reciprocal_type) keys = self[:keys] associated_class.all_association_reflections.each do |assoc_reflect| if r_types.include?(assoc_reflect[:type]) && assoc_reflect[:keys] == keys && assoc_reflect.associated_class == self[:model] self[:reciprocal_type] = assoc_reflect[:type] return self[:reciprocal] = assoc_reflect[:name] end end self[:reciprocal] = nil end |
#reciprocal_array? ⇒ Boolean
Whether the reciprocal of this association returns an array of objects instead of a single object, true by default.
168 169 170 |
# File 'lib/sequel/model/associations.rb', line 168 def reciprocal_array? true end |
#remove_all_method ⇒ Object
Name symbol for the remove_all_ association method
173 174 175 |
# File 'lib/sequel/model/associations.rb', line 173 def remove_all_method :"remove_all_#{self[:name]}" end |
#remove_before_destroy? ⇒ Boolean
Whether associated objects need to be removed from the association before being destroyed in order to preserve referential integrity.
179 180 181 |
# File 'lib/sequel/model/associations.rb', line 179 def remove_before_destroy? true end |
#remove_method ⇒ Object
Name symbol for the remove_ association method
184 185 186 |
# File 'lib/sequel/model/associations.rb', line 184 def remove_method :"remove_#{singularize(self[:name])}" end |
#remove_should_check_existing? ⇒ Boolean
Whether to check that an object to be disassociated is already associated to this object, false by default.
189 190 191 |
# File 'lib/sequel/model/associations.rb', line 189 def remove_should_check_existing? false end |
#returns_array? ⇒ Boolean
Whether this association returns an array of objects instead of a single object, true by default.
195 196 197 |
# File 'lib/sequel/model/associations.rb', line 195 def returns_array? true end |
#select ⇒ Object
The columns to select when loading the association.
200 201 202 |
# File 'lib/sequel/model/associations.rb', line 200 def select self[:select] end |
#set_reciprocal_to_self? ⇒ Boolean
Whether to set the reciprocal association to self when loading associated records, false by default.
206 207 208 |
# File 'lib/sequel/model/associations.rb', line 206 def set_reciprocal_to_self? false end |
#setter_method ⇒ Object
Name symbol for the setter association method
211 212 213 |
# File 'lib/sequel/model/associations.rb', line 211 def setter_method :"#{self[:name]}=" end |