Class: ActiveRecord::CachedValue

Inherits:
ObjectProxy
  • Object
show all
Defined in:
lib/cached_value.rb

Instance Method Summary collapse

Constructor Details

#initialize(owner, reflection) ⇒ CachedValue

Returns a new instance of CachedValue.



7
8
9
# File 'lib/cached_value.rb', line 7

def initialize(owner, reflection)
  @owner, @reflection = owner, reflection
end

Instance Method Details

#clearObject



25
26
27
28
29
# File 'lib/cached_value.rb', line 25

def clear
  clear_cache
  @owner.instance_variable_set("@#{@reflection.name}", nil)
  self
end

#find_target(skip_cache = false) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/cached_value.rb', line 35

def find_target(skip_cache = false)
  target = find_target_from_cache unless skip_cache
  unless target
    target ||= @reflection.options[:sql] ? find_target_by_sql : find_target_by_eval
  end
  target
end

#load(force_update = false) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/cached_value.rb', line 11

def load(force_update = false)
  @target = find_target force_update
  if force_update || (has_cache? && find_target_from_cache.nil?)
    update_cache @target
  end
  self
end

#reloadObject Also known as: update



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

def reload
  @owner.send(@reflection.name).load(true)
end

#targetObject



31
32
33
# File 'lib/cached_value.rb', line 31

def target
  @target
end