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



144
145
146
147
148
149
150
151
152
153
# File 'lib/cacheable_delegator.rb', line 144

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



198
199
200
201
202
203
# File 'lib/cacheable_delegator.rb', line 198

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

  self
end

#refresh_cache!Object



205
206
207
208
# File 'lib/cacheable_delegator.rb', line 205

def refresh_cache!
  refresh_cache
  self.save
end

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

Returns:

  • (Boolean)


155
156
157
158
159
160
161
162
163
# File 'lib/cacheable_delegator.rb', line 155

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