Class: ActiveRecord::CachedValue

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

Instance Method Summary collapse

Constructor Details

#initialize(owner, reflection) ⇒ CachedValue



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

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

Instance Method Details

#clearObject



27
28
29
30
31
# File 'lib/cached_value.rb', line 27

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

#find_target(skip_cache = false) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/cached_value.rb', line 41

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
18
19
# File 'lib/cached_value.rb', line 11

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

#reloadObject Also known as: update



21
22
23
# File 'lib/cached_value.rb', line 21

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

#targetObject



33
34
35
# File 'lib/cached_value.rb', line 33

def target
  @target
end

#to_yamlObject



37
38
39
# File 'lib/cached_value.rb', line 37

def to_yaml
  target.is_a?(String) ? target : target.to_yaml
end