Module: CachedRecord::ORM::InstanceMethods

Defined in:
lib/cached_record/orm.rb

Instance Method Summary collapse

Instance Method Details

#as_cache_jsonObject



147
148
149
150
151
152
153
154
# File 'lib/cached_record/orm.rb', line 147

def as_cache_json
  attributes = {:id => id}.merge cache_attributes
  variables = (cache_json_options[:memoize] || {}).inject({}) do |hash, (method, variable)|
    hash[variable] = send method
    hash
  end
  merge_cache_json attributes, variables
end

#cacheObject



169
170
171
172
# File 'lib/cached_record/orm.rb', line 169

def cache
  self.class.cached id
  true
end

#cache_attributesObject

Raises:

  • (NotImplementedError)


139
140
141
# File 'lib/cached_record/orm.rb', line 139

def cache_attributes
  raise NotImplementedError, "Cannot return cache attributes for `#{self.class}` instances"
end

#cache_foreign_keysObject

Raises:

  • (NotImplementedError)


143
144
145
# File 'lib/cached_record/orm.rb', line 143

def cache_foreign_keys
  raise NotImplementedError, "Cannot return cache foreign keys for `#{self.class}` instances"
end

#expireObject



174
175
176
177
# File 'lib/cached_record/orm.rb', line 174

def expire
  Cache.expire self
  true
end

#merge_cache_json(attributes, variables) ⇒ Object



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

def merge_cache_json(attributes, variables)
  if cache_json_options[:include_root]
    variables = variables.inject({}){|h, (k, v)| h[k.to_s.gsub(/^@/, "").to_sym] = v; h}
    {self.class.cache_root => attributes}.merge variables
  else
    attributes.merge variables
  end
end

#to_cache_jsonObject



165
166
167
# File 'lib/cached_record/orm.rb', line 165

def to_cache_json
  as_cache_json.to_json
end