Class: AwesomeCounterCache::CounterCache

Inherits:
Object
  • Object
show all
Defined in:
lib/awesome_counter_cache/counter_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column_name:, delta_magnitude:, id:, model_class:, relation_name:) ⇒ CounterCache

Returns a new instance of CounterCache.



4
5
6
7
8
9
10
11
12
13
# File 'lib/awesome_counter_cache/counter_cache.rb', line 4

def initialize(column_name:, delta_magnitude:, id:, model_class:, relation_name:)
  @column_name = column_name
  @delta_magnitude = delta_magnitude
  @id = id
  @model_class = model_class
  @relation_name = relation_name

  raise "Invalid model class: #{model_class&.name}" if model_class.blank?
  raise "Invalid relation name: #{relation_name}" if relation_name.blank?
end

Instance Attribute Details

#column_nameObject (readonly)

Returns the value of attribute column_name.



2
3
4
# File 'lib/awesome_counter_cache/counter_cache.rb', line 2

def column_name
  @column_name
end

#delta_magnitudeObject (readonly)

Returns the value of attribute delta_magnitude.



2
3
4
# File 'lib/awesome_counter_cache/counter_cache.rb', line 2

def delta_magnitude
  @delta_magnitude
end

#idObject (readonly)

Returns the value of attribute id.



2
3
4
# File 'lib/awesome_counter_cache/counter_cache.rb', line 2

def id
  @id
end

#model_classObject (readonly)

Returns the value of attribute model_class.



2
3
4
# File 'lib/awesome_counter_cache/counter_cache.rb', line 2

def model_class
  @model_class
end

#relation_nameObject (readonly)

Returns the value of attribute relation_name.



2
3
4
# File 'lib/awesome_counter_cache/counter_cache.rb', line 2

def relation_name
  @relation_name
end

Instance Method Details

#reflectionObject



15
16
17
# File 'lib/awesome_counter_cache/counter_cache.rb', line 15

def reflection
  model_class.reflections.fetch(relation_name.to_s)
end

#relation_foreign_keyObject



19
20
21
# File 'lib/awesome_counter_cache/counter_cache.rb', line 19

def relation_foreign_key
  reflection.foreign_key
end