Module: CacheableDelegator

Extended by:
ActiveSupport::Concern
Defined in:
lib/cacheable_delegator.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

EXCLUDED_FIELDS =
[:id, :cache_created_at, :cache_updated_at, :source_record_id, :source_record_type]
COL_ATTRIBUTES_TO_UPGRADE =
[:limit, :name, :null, :precision, :scale, :sql_type, :type]
DELEGATING_REGEX =
/^delegate_(\w+)/

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(foo, *args, &block) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/cacheable_delegator.rb', line 158

def method_missing(foo, *args, &block)
  if foomatch = foo.to_s.match(DELEGATING_REGEX)
    foo = foomatch[1].to_s
    self.source_record.send(foo, *args, &block)
  elsif source_record.respond_to?(foo)
    source_record.send(foo, *args, &block)
  else
    super
  end
end

Instance Method Details

#refresh_cacheObject

instance method pre: must already have source_record set



212
213
214
215
216
217
# File 'lib/cacheable_delegator.rb', line 212

def refresh_cache
  atts = self.class.build_attributes_hash(self.source_record)
  self.assign_attributes(atts)

  self
end

#refresh_cache!Object



219
220
221
222
# File 'lib/cacheable_delegator.rb', line 219

def refresh_cache!
  refresh_cache
  self.save
end

#respond_to?(meth, x = false) ⇒ Boolean

Returns:



169
170
171
172
173
174
175
176
177
# File 'lib/cacheable_delegator.rb', line 169

def respond_to?(meth, x=false)
  if meth.match(DELEGATING_REGEX)
    true
  elsif source_record.respond_to?(meth)
    true
  else
    super
  end
end