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.
Methods
Public Instance
- _add_method
- _remove_all_method
- _remove_method
- _setter_method
- add_method
- apply_dataset_changes
- associated_class
- associated_dataset
- association_method
- can_have_associated_objects?
- dataset_method
- dataset_need_primary_key?
- eager_graph_lazy_dataset?
- eager_limit_strategy
- eager_loader_key
- eager_loading_predicate_key
- eager_loading_use_associated_key?
- filter_by_associations_add_conditions?
- filter_by_associations_conditions_expression
- limit_and_offset
- need_associated_primary_key?
- predicate_keys
- qualify
- qualify_assoc
- qualify_cur
- reciprocal
- reciprocal_array?
- remove_all_method
- remove_before_destroy?
- remove_method
- remove_should_check_existing?
- returns_array?
- select
- set_reciprocal_to_self?
- setter_method
- slice_range
Included modules
Public Instance methods
Name symbol for the _add internal association method
# File lib/sequel/model/associations.rb, line 22 def _add_method :"_add_#{singularize(self[:name])}" end
Name symbol for the _remove_all internal association method
# File lib/sequel/model/associations.rb, line 27 def _remove_all_method :"_remove_all_#{self[:name]}" end
Name symbol for the _remove internal association method
# File lib/sequel/model/associations.rb, line 32 def _remove_method :"_remove_#{singularize(self[:name])}" end
Name symbol for the _setter association method
# File lib/sequel/model/associations.rb, line 37 def _setter_method :"_#{self[:name]}=" end
Name symbol for the add association method
# File lib/sequel/model/associations.rb, line 42 def add_method :"add_#{singularize(self[:name])}" end
Apply all non-instance specific changes to the given dataset and return it.
# 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 !returns_array? && self[:key] ds = ds.eager(*self[:eager]) if self[:eager] ds = ds.distinct if self[:distinct] ds end
The class associated to the current model class via this association
# File lib/sequel/model/associations.rb, line 52 def associated_class cached_fetch(:class){constantize(self[:class_name])} end
The dataset associated via this association, with the non-instance specific changes already applied.
# File lib/sequel/model/associations.rb, line 58 def associated_dataset cached_fetch(:_dataset){apply_dataset_changes(associated_class.dataset.clone)} end
Name symbol for association method, the same as the name of the association.
# File lib/sequel/model/associations.rb, line 47 def association_method self[:name] end
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.
# File lib/sequel/model/associations.rb, line 82 def can_have_associated_objects?(obj) true end
Name symbol for the dataset association method
# File lib/sequel/model/associations.rb, line 87 def dataset_method :"#{self[:name]}_dataset" end
Whether the dataset needs a primary key to function, true by default.
# File lib/sequel/model/associations.rb, line 92 def dataset_need_primary_key? true end
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.
# File lib/sequel/model/associations.rb, line 136 def eager_graph_lazy_dataset? true end
The eager limit strategy to use for this dataset.
# File lib/sequel/model/associations.rb, line 97 def eager_limit_strategy cached_fetch(:_eager_limit_strategy) do if self[:limit] case s = cached_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
The key to use for the key hash when eager loading
# File lib/sequel/model/associations.rb, line 118 def eager_loader_key self[:eager_loader_key] end
Alias of predicate_key, only for backwards compatibility.
# File lib/sequel/model/associations.rb, line 129 def eager_loading_predicate_key predicate_key end
By default associations do not need to select a key in an associated table to eagerly load.
# File lib/sequel/model/associations.rb, line 124 def eager_loading_use_associated_key? false end
Whether additional conditions should be added when using the filter by associations support.
# File lib/sequel/model/associations.rb, line 142 def filter_by_associations_add_conditions? self[:conditions] || self[:eager_block] end
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.
# File lib/sequel/model/associations.rb, line 150 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
The limit and offset for this association (returned as a two element array).
# File lib/sequel/model/associations.rb, line 156 def limit_and_offset if (v = self[:limit]).is_a?(Array) v else [v, nil] end end
Whether the associated object needs a primary key to be added/removed, false by default.
# File lib/sequel/model/associations.rb, line 166 def need_associated_primary_key? false end
The keys to use for loading of the regular dataset, as an array.
# File lib/sequel/model/associations.rb, line 171 def predicate_keys cached_fetch(:predicate_keys){Array(predicate_key)} end
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.
# File lib/sequel/model/associations.rb, line 178 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 col with the associated model’s table name.
# File lib/sequel/model/associations.rb, line 190 def qualify_assoc(col) qualify(associated_class.table_name, col) end
Qualify col with the current model’s table name.
# File lib/sequel/model/associations.rb, line 195 def qualify_cur(col) qualify(self[:model].table_name, col) end
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.
# File lib/sequel/model/associations.rb, line 205 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
Whether the reciprocal of this association returns an array of objects instead of a single object, true by default.
# File lib/sequel/model/associations.rb, line 224 def reciprocal_array? true end
Name symbol for the remove_all_ association method
# File lib/sequel/model/associations.rb, line 229 def remove_all_method :"remove_all_#{self[:name]}" end
Whether associated objects need to be removed from the association before being destroyed in order to preserve referential integrity.
# File lib/sequel/model/associations.rb, line 235 def remove_before_destroy? true end
Name symbol for the remove_ association method
# File lib/sequel/model/associations.rb, line 240 def remove_method :"remove_#{singularize(self[:name])}" end
Whether to check that an object to be disassociated is already associated to this object, false by default.
# File lib/sequel/model/associations.rb, line 245 def remove_should_check_existing? false end
Whether this association returns an array of objects instead of a single object, true by default.
# File lib/sequel/model/associations.rb, line 251 def returns_array? true end
The columns to select when loading the association.
# File lib/sequel/model/associations.rb, line 256 def select self[:select] end
Whether to set the reciprocal association to self when loading associated records, false by default.
# File lib/sequel/model/associations.rb, line 262 def set_reciprocal_to_self? false end
Name symbol for the setter association method
# File lib/sequel/model/associations.rb, line 267 def setter_method :"#{self[:name]}=" end
The range used for slicing when using the :ruby eager limit strategy.
# File lib/sequel/model/associations.rb, line 272 def slice_range limit, offset = limit_and_offset if limit || offset (offset||0)..(limit ? (offset||0)+limit-1 : -1) end end