Class: ActiveRecordDataLoader::Dsl::PolymorphicAssociation
- Inherits:
-
Object
- Object
- ActiveRecordDataLoader::Dsl::PolymorphicAssociation
- Defined in:
- lib/active_record_data_loader/dsl/polymorphic_association.rb
Instance Attribute Summary collapse
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#models ⇒ Object
readonly
Returns the value of attribute models.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#queries ⇒ Object
readonly
Returns the value of attribute queries.
Instance Method Summary collapse
-
#initialize(model_class, name) ⇒ PolymorphicAssociation
constructor
A new instance of PolymorphicAssociation.
- #model(klass, weight: 1, eligible_set: nil) ⇒ Object
- #weighted_models ⇒ Object
Constructor Details
#initialize(model_class, name) ⇒ PolymorphicAssociation
Returns a new instance of PolymorphicAssociation.
8 9 10 11 12 13 |
# File 'lib/active_record_data_loader/dsl/polymorphic_association.rb', line 8 def initialize(model_class, name) @model_class = model_class @name = name @models = {} @queries = {} end |
Instance Attribute Details
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
6 7 8 |
# File 'lib/active_record_data_loader/dsl/polymorphic_association.rb', line 6 def model_class @model_class end |
#models ⇒ Object (readonly)
Returns the value of attribute models.
6 7 8 |
# File 'lib/active_record_data_loader/dsl/polymorphic_association.rb', line 6 def models @models end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/active_record_data_loader/dsl/polymorphic_association.rb', line 6 def name @name end |
#queries ⇒ Object (readonly)
Returns the value of attribute queries.
6 7 8 |
# File 'lib/active_record_data_loader/dsl/polymorphic_association.rb', line 6 def queries @queries end |
Instance Method Details
#model(klass, weight: 1, eligible_set: nil) ⇒ Object
15 16 17 18 |
# File 'lib/active_record_data_loader/dsl/polymorphic_association.rb', line 15 def model(klass, weight: 1, eligible_set: nil) @models[klass] = weight.to_i @queries[klass] = eligible_set if eligible_set end |
#weighted_models ⇒ Object
20 21 22 23 24 |
# File 'lib/active_record_data_loader/dsl/polymorphic_association.rb', line 20 def weighted_models gcd = models.values.reduce(:gcd) models.map { |m, w| [m] * (w / gcd) }.flatten end |