Module: CacheColumn::InstanceMethods

Defined in:
lib/cache_column.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cache_column.rb', line 73

def method_missing(method, *arguments, &block)
  cached_columns = self.class.instance_variable_get('@cached_columns')
  
  if cached_columns.keys.include?(method) #then its a getter
    cached_value = self["#{method}_cache".to_sym]
    
    #If the cached_value is nil we should check to see if we can get a new value
    if cached_value.nil?
      update_cache(method)
      cached_value = self["#{method}_cache".to_sym]
    end
    
    return cached_value 
  elsif method.to_s[-1,1] == '=' and cached_columns.keys.include?(method.to_s.chop.to_sym) and cached_columns[method.to_s.chop.to_sym][:action].blank? #then its a setter
    return self["#{method.to_s.chop}_cache".to_sym] = arguments[0]
  else
    super
  end
end

Instance Method Details

#respond_to?(method, include_priv = false) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
# File 'lib/cache_column.rb', line 62

def respond_to?(method, include_priv=false)
  cached_columns = self.class.instance_variable_get('@cached_columns')
  return if cached_columns.nil?
  cached_columns.each do |key, value|
    return true if method.to_s == key.to_s
    return true if method.to_s == "#{key.to_s}=" and value[:action].blank?
  end

  super
end

#update_cache(key, params = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cache_column.rb', line 38

def update_cache(key, params=nil)
  if params.nil?
    cached_columns = self.class.instance_variable_get('@cached_columns')
    params = cached_columns[key]
  end
  
  return if params.nil?
  
  return if params[:update_validation].present? and self.respond_to?(params[:update_validation]) and self.send("#{params[:update_validation]}") == false
  return if params[:on_save].blank?

  if params[:on_save] == :blank
    new_value = ""
    set = true
  elsif params[:on_save] == :recheck and params[:action].present? and self.respond_to?(params[:action].to_sym)
    new_value = self.send(params[:action].to_sym)
    set = true
  end

  self["#{key}_cache".to_sym] = new_value if set == true
  
end

#update_cache_columnsObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cache_column.rb', line 25

def update_cache_columns
  cached_columns = self.class.instance_variable_get('@cached_columns')
  set = false

  return if cached_columns.nil?

  cached_columns.each do |key,params|
    update_cache(key,params)
  end

  #self.cache_updated_at = Time.now if self.respond_to?(:cached_updated_at)
end