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, Plugins::PgArrayAssociations::ManyToPgArrayAssociationReflection, Plugins::PgArrayAssociations::PgArrayToManyAssociationReflection
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.
-
#_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.
-
#apply_dataset_changes(ds) ⇒ Object
Apply all non-instance specific changes to the given dataset and return it.
-
#apply_distinct_on_eager_limit_strategy(ds) ⇒ Object
Use DISTINCT ON and ORDER BY clauses to limit the results to the first record with matching keys.
-
#apply_eager_graph_limit_strategy(strategy, ds) ⇒ Object
Apply the eager graph limit strategy to the dataset to graph into the current dataset, or return the dataset unmodified if no SQL limit strategy is needed.
-
#apply_eager_limit_strategy(ds) ⇒ Object
Apply an eager limit strategy to the dataset, or return the dataset unmodified if it doesn’t need an eager limit strategy.
-
#apply_ruby_eager_limit_strategy(rows) ⇒ Object
If the ruby eager limit strategy is being used, slice the array using the slice range to return the object(s) at the correct offset/limit.
-
#apply_window_function_eager_limit_strategy(ds) ⇒ Object
Use a window function to limit the results of the eager loading dataset.
-
#assign_singular? ⇒ Boolean
Whether the associations cache should use an array when storing the associated records during eager loading.
-
#associated_class ⇒ Object
The class associated to the current model class via this association.
-
#associated_dataset ⇒ Object
The dataset associated via this association, with the non-instance specific changes already applied.
-
#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.
-
#cloneable?(ref) ⇒ Boolean
Whether you are able to clone from the given association type to the current association type, true by default only if the types match.
-
#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.
-
#delete_row_number_column(ds) ⇒ Object
Return the symbol used for the row number column if the window function eager limit strategy is being used, or nil otherwise.
-
#eager_graph_lazy_dataset? ⇒ Boolean
Whether to eagerly graph a lazy dataset, true by default.
-
#eager_graph_limit_strategy(strategy) ⇒ Object
The eager_graph limit strategy to use for this dataset.
-
#eager_limit_strategy ⇒ Object
The eager limit strategy to use for this dataset.
-
#eager_loader_key ⇒ Object
The key to use for the key hash when eager loading.
-
#eager_loading_predicate_key ⇒ Object
Alias of predicate_key, only for backwards compatibility.
-
#eager_loading_use_associated_key? ⇒ Boolean
By default associations do not need to select a key in an associated table to eagerly load.
-
#filter_by_associations_add_conditions? ⇒ Boolean
Whether additional conditions should be added when using the filter by associations support.
-
#filter_by_associations_conditions_expression(obj) ⇒ Object
The expression to use for the additional conditions to be added for the filter by association support, when the association itself is filtered.
-
#handle_silent_modification_failure? ⇒ Boolean
Whether to handle silent modification failure when adding/removing associated records, false by default.
-
#initialize_association_cache(objects) ⇒ Object
Initialize the associations cache for the current association for the given objects.
-
#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.
-
#predicate_keys ⇒ Object
The keys to use for loading of the regular dataset, as an array.
-
#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.
-
#slice_range ⇒ Object
The range used for slicing when using the :ruby eager limit strategy.
Methods included from Inflections
clear, irregular, plural, singular, uncountable
Methods inherited from Hash
#&, #case, #hstore, #pg_json, #sql_expr, #sql_negate, #sql_or, #|, #~
Instance Method Details
#_add_method ⇒ Object
Name symbol for the _add internal association method
22 23 24 |
# File 'lib/sequel/model/associations.rb', line 22 def _add_method :"_add_#{singularize(self[:name])}" end |
#_remove_all_method ⇒ Object
Name symbol for the _remove_all internal association method
27 28 29 |
# File 'lib/sequel/model/associations.rb', line 27 def _remove_all_method :"_remove_all_#{self[:name]}" end |
#_remove_method ⇒ Object
Name symbol for the _remove internal association method
32 33 34 |
# File 'lib/sequel/model/associations.rb', line 32 def _remove_method :"_remove_#{singularize(self[:name])}" end |
#_setter_method ⇒ Object
Name symbol for the _setter association method
37 38 39 |
# File 'lib/sequel/model/associations.rb', line 37 def _setter_method :"_#{self[:name]}=" end |
#add_method ⇒ Object
Name symbol for the add association method
42 43 44 |
# File 'lib/sequel/model/associations.rb', line 42 def add_method :"add_#{singularize(self[:name])}" end |
#apply_dataset_changes(ds) ⇒ Object
Apply all non-instance specific changes to the given dataset and return it.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/sequel/model/associations.rb', line 63 def apply_dataset_changes(ds) ds.extend(AssociationDatasetMethods) ds.association_reflection = self self[:extend].each{|m| ds.extend(m)} ds = ds.select(*select) if select if c = self[:conditions] ds = (c.is_a?(Array) && !Sequel.condition_specifier?(c)) ? ds.where(*c) : ds.where(c) end ds = ds.order(*self[:order]) if self[:order] ds = ds.limit(*self[:limit]) if self[:limit] ds = ds.limit(1) if limit_to_single_row? ds = ds.eager(*self[:eager]) if self[:eager] ds = ds.distinct if self[:distinct] ds end |
#apply_distinct_on_eager_limit_strategy(ds) ⇒ Object
Use DISTINCT ON and ORDER BY clauses to limit the results to the first record with matching keys.
106 107 108 109 |
# File 'lib/sequel/model/associations.rb', line 106 def apply_distinct_on_eager_limit_strategy(ds) keys = predicate_key ds.distinct(*keys).order_prepend(*keys) end |
#apply_eager_graph_limit_strategy(strategy, ds) ⇒ Object
Apply the eager graph limit strategy to the dataset to graph into the current dataset, or return the dataset unmodified if no SQL limit strategy is needed.
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/sequel/model/associations.rb', line 81 def apply_eager_graph_limit_strategy(strategy, ds) case strategy when :distinct_on apply_distinct_on_eager_limit_strategy(ds.order_prepend(*self[:order])) when :window_function apply_window_function_eager_limit_strategy(ds.order_prepend(*self[:order])).select(*ds.columns) else ds end end |
#apply_eager_limit_strategy(ds) ⇒ Object
Apply an eager limit strategy to the dataset, or return the dataset unmodified if it doesn’t need an eager limit strategy.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/sequel/model/associations.rb', line 94 def apply_eager_limit_strategy(ds) case eager_limit_strategy when :distinct_on apply_distinct_on_eager_limit_strategy(ds) when :window_function apply_window_function_eager_limit_strategy(ds) else ds end end |
#apply_ruby_eager_limit_strategy(rows) ⇒ Object
If the ruby eager limit strategy is being used, slice the array using the slice range to return the object(s) at the correct offset/limit.
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/sequel/model/associations.rb', line 132 def apply_ruby_eager_limit_strategy(rows) if eager_limit_strategy == :ruby name = self[:name] if returns_array? range = slice_range rows.each{|o| o.associations[name] = o.associations[name][range] || []} elsif slice_range offset = slice_range.begin rows.each{|o| o.associations[name] = o.associations[name][offset]} end end end |
#apply_window_function_eager_limit_strategy(ds) ⇒ Object
Use a window function to limit the results of the eager loading dataset.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/sequel/model/associations.rb', line 112 def apply_window_function_eager_limit_strategy(ds) rn = ds.row_number_column limit, offset = limit_and_offset ds = ds.unordered.select_append{|o| o.row_number{}.over(:partition=>predicate_key, :order=>ds.opts[:order]).as(rn)}.from_self ds = if !returns_array? ds.where(rn => offset ? offset+1 : 1) elsif offset offset += 1 if limit ds.where(rn => (offset...(offset+limit))) else ds.where{SQL::Identifier.new(rn) >= offset} end else ds.where{SQL::Identifier.new(rn) <= limit} end end |
#assign_singular? ⇒ Boolean
Whether the associations cache should use an array when storing the associated records during eager loading.
147 148 149 |
# File 'lib/sequel/model/associations.rb', line 147 def assign_singular? !returns_array? end |
#associated_class ⇒ Object
The class associated to the current model class via this association
52 53 54 |
# File 'lib/sequel/model/associations.rb', line 52 def associated_class cached_fetch(:class){constantize(self[:class_name])} end |
#associated_dataset ⇒ Object
The dataset associated via this association, with the non-instance specific changes already applied.
58 59 60 |
# File 'lib/sequel/model/associations.rb', line 58 def associated_dataset cached_fetch(:_dataset){apply_dataset_changes(associated_class.dataset.clone)} end |
#association_method ⇒ Object
Name symbol for association method, the same as the name of the association.
47 48 49 |
# File 'lib/sequel/model/associations.rb', line 47 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.
154 155 156 |
# File 'lib/sequel/model/associations.rb', line 154 def can_have_associated_objects?(obj) true end |
#cloneable?(ref) ⇒ Boolean
Whether you are able to clone from the given association type to the current association type, true by default only if the types match.
160 161 162 |
# File 'lib/sequel/model/associations.rb', line 160 def cloneable?(ref) ref[:type] == self[:type] end |
#dataset_method ⇒ Object
Name symbol for the dataset association method
165 166 167 |
# File 'lib/sequel/model/associations.rb', line 165 def dataset_method :"#{self[:name]}_dataset" end |
#dataset_need_primary_key? ⇒ Boolean
Whether the dataset needs a primary key to function, true by default.
170 171 172 |
# File 'lib/sequel/model/associations.rb', line 170 def dataset_need_primary_key? true end |
#delete_row_number_column(ds) ⇒ Object
Return the symbol used for the row number column if the window function eager limit strategy is being used, or nil otherwise.
176 177 178 179 180 |
# File 'lib/sequel/model/associations.rb', line 176 def delete_row_number_column(ds) if eager_limit_strategy == :window_function ds.row_number_column end 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.
232 233 234 |
# File 'lib/sequel/model/associations.rb', line 232 def eager_graph_lazy_dataset? true end |
#eager_graph_limit_strategy(strategy) ⇒ Object
The eager_graph limit strategy to use for this dataset
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/sequel/model/associations.rb', line 183 def eager_graph_limit_strategy(strategy) if self[:limit] || !returns_array? strategy = strategy[self[:name]] if strategy.is_a?(Hash) case strategy when true true_eager_limit_strategy when Symbol strategy else if returns_array? || offset :ruby end end end end |
#eager_limit_strategy ⇒ Object
The eager limit strategy to use for this dataset.
200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/sequel/model/associations.rb', line 200 def eager_limit_strategy cached_fetch(:_eager_limit_strategy) do if self[:limit] || !returns_array? case s = cached_fetch(:eager_limit_strategy){default_eager_limit_strategy} when true true_eager_limit_strategy else s end end end end |
#eager_loader_key ⇒ Object
The key to use for the key hash when eager loading
214 215 216 |
# File 'lib/sequel/model/associations.rb', line 214 def eager_loader_key self[:eager_loader_key] end |
#eager_loading_predicate_key ⇒ Object
Alias of predicate_key, only for backwards compatibility.
225 226 227 |
# File 'lib/sequel/model/associations.rb', line 225 def eager_loading_predicate_key predicate_key end |
#eager_loading_use_associated_key? ⇒ Boolean
By default associations do not need to select a key in an associated table to eagerly load.
220 221 222 |
# File 'lib/sequel/model/associations.rb', line 220 def eager_loading_use_associated_key? false end |
#filter_by_associations_add_conditions? ⇒ Boolean
Whether additional conditions should be added when using the filter by associations support.
238 239 240 |
# File 'lib/sequel/model/associations.rb', line 238 def filter_by_associations_add_conditions? self[:conditions] || self[:eager_block] || self[:limit] end |
#filter_by_associations_conditions_expression(obj) ⇒ Object
The expression to use for the additional conditions to be added for the filter by association support, when the association itself is filtered. Works by using a subquery to test that the objects passed also meet the association filter criteria.
246 247 248 249 |
# File 'lib/sequel/model/associations.rb', line 246 def filter_by_associations_conditions_expression(obj) ds = filter_by_associations_conditions_dataset.where(filter_by_associations_conditions_subquery_conditions(obj)) {filter_by_associations_conditions_key=>ds} end |
#handle_silent_modification_failure? ⇒ Boolean
Whether to handle silent modification failure when adding/removing associated records, false by default.
253 254 255 |
# File 'lib/sequel/model/associations.rb', line 253 def handle_silent_modification_failure? false end |
#initialize_association_cache(objects) ⇒ Object
Initialize the associations cache for the current association for the given objects.
258 259 260 261 262 263 264 265 |
# File 'lib/sequel/model/associations.rb', line 258 def initialize_association_cache(objects) name = self[:name] if assign_singular? objects.each{|object| object.associations[name] = nil} else objects.each{|object| object.associations[name] = []} end end |
#limit_and_offset ⇒ Object
The limit and offset for this association (returned as a two element array).
268 269 270 271 272 273 274 |
# File 'lib/sequel/model/associations.rb', line 268 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.
278 279 280 |
# File 'lib/sequel/model/associations.rb', line 278 def need_associated_primary_key? false end |
#predicate_keys ⇒ Object
The keys to use for loading of the regular dataset, as an array.
283 284 285 |
# File 'lib/sequel/model/associations.rb', line 283 def predicate_keys cached_fetch(:predicate_keys){Array(predicate_key)} 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. Only qualifies Symbols and SQL::Identifier values, other values are not modified.
290 291 292 293 294 295 296 297 298 299 |
# File 'lib/sequel/model/associations.rb', line 290 def qualify(table, col) transform(col) do |k| case k when Symbol, SQL::Identifier SQL::QualifiedIdentifier.new(table, k) else Sequel::Qualifier.new(self[:model].dataset, table).transform(k) end end end |
#qualify_assoc(col) ⇒ Object
Qualify col with the associated model’s table name.
302 303 304 |
# File 'lib/sequel/model/associations.rb', line 302 def qualify_assoc(col) qualify(associated_class.table_name, col) end |
#qualify_cur(col) ⇒ Object
Qualify col with the current model’s table name.
307 308 309 |
# File 'lib/sequel/model/associations.rb', line 307 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.
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/sequel/model/associations.rb', line 317 def reciprocal cached_fetch(:reciprocal) do possible_recips = [] associated_class.all_association_reflections.each do |assoc_reflect| if reciprocal_association?(assoc_reflect) possible_recips << assoc_reflect end end if possible_recips.length == 1 cached_set(:reciprocal_type, possible_recips.first[:type]) if reciprocal_type.is_a?(Array) possible_recips.first[:name] end end end |
#reciprocal_array? ⇒ Boolean
Whether the reciprocal of this association returns an array of objects instead of a single object, true by default.
336 337 338 |
# File 'lib/sequel/model/associations.rb', line 336 def reciprocal_array? true end |
#remove_all_method ⇒ Object
Name symbol for the remove_all_ association method
341 342 343 |
# File 'lib/sequel/model/associations.rb', line 341 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.
347 348 349 |
# File 'lib/sequel/model/associations.rb', line 347 def remove_before_destroy? true end |
#remove_method ⇒ Object
Name symbol for the remove_ association method
352 353 354 |
# File 'lib/sequel/model/associations.rb', line 352 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.
357 358 359 |
# File 'lib/sequel/model/associations.rb', line 357 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.
363 364 365 |
# File 'lib/sequel/model/associations.rb', line 363 def returns_array? true end |
#select ⇒ Object
The columns to select when loading the association.
368 369 370 |
# File 'lib/sequel/model/associations.rb', line 368 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.
374 375 376 |
# File 'lib/sequel/model/associations.rb', line 374 def set_reciprocal_to_self? false end |
#setter_method ⇒ Object
Name symbol for the setter association method
379 380 381 |
# File 'lib/sequel/model/associations.rb', line 379 def setter_method :"#{self[:name]}=" end |
#slice_range ⇒ Object
The range used for slicing when using the :ruby eager limit strategy.
384 385 386 387 388 389 |
# File 'lib/sequel/model/associations.rb', line 384 def slice_range limit, offset = limit_and_offset if limit || offset (offset||0)..(limit ? (offset||0)+limit-1 : -1) end end |