Class Sequel::Model::Associations::AssociationReflection
In: lib/sequel/model/associations.rb
Parent: Hash

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

Included Modules

Sequel::Inflections

Public Instance methods

Name symbol for the _add internal association method

[Source]

    # File lib/sequel/model/associations.rb, line 22
22:         def _add_method
23: 
24:           "_add_#{singularize(self[:name])}"
25:         end

Name symbol for the _remove_all internal association method

[Source]

    # File lib/sequel/model/associations.rb, line 27
27:         def _remove_all_method
28: 
29:           "_remove_all_#{self[:name]}"
30:         end

Name symbol for the _remove internal association method

[Source]

    # File lib/sequel/model/associations.rb, line 32
32:         def _remove_method
33: 
34:           "_remove_#{singularize(self[:name])}"
35:         end

Name symbol for the _setter association method

[Source]

    # File lib/sequel/model/associations.rb, line 37
37:         def _setter_method
38: 
39:           "_#{self[:name]}="
40:         end

Name symbol for the add association method

[Source]

    # File lib/sequel/model/associations.rb, line 42
42:         def add_method
43: 
44:           "add_#{singularize(self[:name])}"
45:         end

Apply all non-instance specific changes to the given dataset and return it.

[Source]

    # File lib/sequel/model/associations.rb, line 63
63:         def apply_dataset_changes(ds)
64:           ds.extend(AssociationDatasetMethods)
65:           ds.association_reflection = self
66:           self[:extend].each{|m| ds.extend(m)}
67:           ds = ds.select(*select) if select
68:           if c = self[:conditions]
69:             ds = (c.is_a?(Array) && !Sequel.condition_specifier?(c)) ? ds.where(*c) : ds.where(c)
70:           end
71:           ds = ds.order(*self[:order]) if self[:order]
72:           ds = ds.limit(*self[:limit]) if self[:limit]
73:           ds = ds.limit(1) if !returns_array? && self[:key]
74:           ds = ds.eager(*self[:eager]) if self[:eager]
75:           ds = ds.distinct if self[:distinct]
76:           ds
77:         end

The class associated to the current model class via this association

[Source]

    # File lib/sequel/model/associations.rb, line 52
52:         def associated_class
53:           cached_fetch(:class){constantize(self[:class_name])}
54:         end

The dataset associated via this association, with the non-instance specific changes already applied.

[Source]

    # File lib/sequel/model/associations.rb, line 58
58:         def associated_dataset
59:           cached_fetch(:_dataset){apply_dataset_changes(associated_class.dataset.clone)}
60:         end

Name symbol for association method, the same as the name of the association.

[Source]

    # File lib/sequel/model/associations.rb, line 47
47:         def association_method
48:           self[:name]
49:         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.

[Source]

    # File lib/sequel/model/associations.rb, line 82
82:         def can_have_associated_objects?(obj)
83:           true
84:         end

Name symbol for the dataset association method

[Source]

    # File lib/sequel/model/associations.rb, line 87
87:         def dataset_method
88: 
89:           "#{self[:name]}_dataset"
90:         end

Whether the dataset needs a primary key to function, true by default.

[Source]

    # File lib/sequel/model/associations.rb, line 92
92:         def dataset_need_primary_key?
93:           true
94:         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.

[Source]

     # File lib/sequel/model/associations.rb, line 136
136:         def eager_graph_lazy_dataset?
137:           true
138:         end

The eager limit strategy to use for this dataset.

[Source]

     # File lib/sequel/model/associations.rb, line 97
 97:         def eager_limit_strategy
 98:           cached_fetch(:_eager_limit_strategy) do
 99:             if self[:limit]
100:               case s = cached_fetch(:eager_limit_strategy){self[:model].default_eager_limit_strategy || :ruby}
101:               when true
102:                 ds = associated_class.dataset
103:                 if ds.supports_window_functions?
104:                   :window_function
105:                 else
106:                   :ruby
107:                 end
108:               else
109:                 s
110:               end
111:             else
112:               nil
113:             end
114:           end
115:         end

The key to use for the key hash when eager loading

[Source]

     # File lib/sequel/model/associations.rb, line 118
118:         def eager_loader_key
119:           self[:eager_loader_key]
120:         end

Alias of predicate_key, only for backwards compatibility.

[Source]

     # File lib/sequel/model/associations.rb, line 129
129:         def eager_loading_predicate_key
130:           predicate_key
131:         end

By default associations do not need to select a key in an associated table to eagerly load.

[Source]

     # File lib/sequel/model/associations.rb, line 124
124:         def eager_loading_use_associated_key?
125:           false
126:         end

The limit and offset for this association (returned as a two element array).

[Source]

     # File lib/sequel/model/associations.rb, line 141
141:         def limit_and_offset
142:           if (v = self[:limit]).is_a?(Array)
143:             v
144:           else
145:             [v, nil]
146:           end
147:         end

Whether the associated object needs a primary key to be added/removed, false by default.

[Source]

     # File lib/sequel/model/associations.rb, line 151
151:         def need_associated_primary_key?
152:           false
153:         end

The keys to use for loading of the regular dataset, as an array.

[Source]

     # File lib/sequel/model/associations.rb, line 156
156:         def predicate_keys
157:           cached_fetch(:predicate_keys){Array(predicate_key)}
158:         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.

[Source]

     # File lib/sequel/model/associations.rb, line 163
163:         def qualify(table, col)
164:           transform(col) do |k|
165:             case k
166:             when Symbol, SQL::Identifier
167:               SQL::QualifiedIdentifier.new(table, k)
168:             else
169:               Sequel::Qualifier.new(self[:model].dataset, table).transform(k)
170:             end
171:           end
172:         end

Qualify col with the associated model‘s table name.

[Source]

     # File lib/sequel/model/associations.rb, line 175
175:         def qualify_assoc(col)
176:           qualify(associated_class.table_name, col)
177:         end

Qualify col with the current model‘s table name.

[Source]

     # File lib/sequel/model/associations.rb, line 180
180:         def qualify_cur(col)
181:           qualify(self[:model].table_name, col)
182:         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.

[Source]

     # File lib/sequel/model/associations.rb, line 190
190:         def reciprocal
191:           cached_fetch(:reciprocal) do
192:             possible_recips = []
193: 
194:             associated_class.all_association_reflections.each do |assoc_reflect|
195:               if reciprocal_association?(assoc_reflect)
196:                 possible_recips << assoc_reflect
197:               end
198:             end
199: 
200:             if possible_recips.length == 1
201:               cached_set(:reciprocal_type, possible_recips.first[:type]) if reciprocal_type.is_a?(Array)
202:               possible_recips.first[:name]
203:             end
204:           end
205:         end

Whether the reciprocal of this association returns an array of objects instead of a single object, true by default.

[Source]

     # File lib/sequel/model/associations.rb, line 209
209:         def reciprocal_array?
210:           true
211:         end

Name symbol for the remove_all_ association method

[Source]

     # File lib/sequel/model/associations.rb, line 214
214:         def remove_all_method
215: 
216:           "remove_all_#{self[:name]}"
217:         end

Whether associated objects need to be removed from the association before being destroyed in order to preserve referential integrity.

[Source]

     # File lib/sequel/model/associations.rb, line 220
220:         def remove_before_destroy?
221:           true
222:         end

Name symbol for the remove_ association method

[Source]

     # File lib/sequel/model/associations.rb, line 225
225:         def remove_method
226: 
227:           "remove_#{singularize(self[:name])}"
228:         end

Whether to check that an object to be disassociated is already associated to this object, false by default.

[Source]

     # File lib/sequel/model/associations.rb, line 230
230:         def remove_should_check_existing?
231:           false
232:         end

Whether this association returns an array of objects instead of a single object, true by default.

[Source]

     # File lib/sequel/model/associations.rb, line 236
236:         def returns_array?
237:           true
238:         end

The columns to select when loading the association.

[Source]

     # File lib/sequel/model/associations.rb, line 241
241:         def select
242:           self[:select]
243:         end

Whether to set the reciprocal association to self when loading associated records, false by default.

[Source]

     # File lib/sequel/model/associations.rb, line 247
247:         def set_reciprocal_to_self?
248:           false
249:         end

Name symbol for the setter association method

[Source]

     # File lib/sequel/model/associations.rb, line 252
252:         def setter_method
253: 
254:           "#{self[:name]}="
255:         end

The range used for slicing when using the :ruby eager limit strategy.

[Source]

     # File lib/sequel/model/associations.rb, line 257
257:         def slice_range
258:           limit, offset = limit_and_offset
259:           if limit || offset
260:             (offset||0)..(limit ? (offset||0)+limit-1 : -1)
261:           end
262:         end

[Validate]