Class: ActiveRecordDataLoader::Dsl::PolymorphicAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_data_loader/dsl/polymorphic_association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_classObject (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

#modelsObject (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

#nameObject (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

#queriesObject (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_modelsObject



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