Module: CachedRecord::ORM::ClassMethods

Defined in:
lib/cached_record/orm.rb

Instance Method Summary collapse

Instance Method Details

#as_cache(*args) ⇒ Object



14
15
16
17
# File 'lib/cached_record/orm.rb', line 14

def as_cache(*args)
  @as_cache = parse_as_cache_options args if args.any?
  @as_cache ||= {:as_json => {}}
end

#as_memoized_cache(*args) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/cached_record/orm.rb', line 19

def as_memoized_cache(*args)
  retain = args.last.delete(:retain) if args.last.is_a?(Hash)
  as_cache(*args).tap do |options|
    options[:memoize] = true
    options[:retain] = retain if retain
  end
end

#cache_key(id) ⇒ Object



27
28
29
# File 'lib/cached_record/orm.rb', line 27

def cache_key(id)
  "#{name.underscore.gsub("/", ".")}.#{id}"
end

#cache_rootObject



31
32
33
# File 'lib/cached_record/orm.rb', line 31

def cache_root
  "#{name.underscore.gsub(/^.*\//, "")}".to_sym
end

#cached(id) ⇒ Object



35
36
37
38
39
# File 'lib/cached_record/orm.rb', line 35

def cached(id)
  Cache.get(self, id) do
    uncached id
  end
end

#load_cache_json(json) ⇒ Object



45
46
47
48
49
50
# File 'lib/cached_record/orm.rb', line 45

def load_cache_json(json)
  json.symbolize_keys!
  properties, variables = cache_json_to_properties_and_variables(json)
  foreign_keys, attributes = properties.partition{|k, v| k.to_s.match /_ids?$/}.collect{|x| Hash[x]}
  new_cached_instance attributes, foreign_keys, variables
end

#new_cached_instance(attributes, foreign_keys, variables) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cached_record/orm.rb', line 52

def new_cached_instance(attributes, foreign_keys, variables)
  id = attributes.delete(:id) || attributes.delete("id")
  _new_cached_instance_(id, attributes).tap do |instance|
    instance.id = id if instance.respond_to?(:id=)
    foreign_keys.each do |key, value|
      set_cached_association instance, key, value
    end
    variables.each do |key, value|
      instance.instance_variable_set key, value
    end
  end
end

#uncached(id) ⇒ Object

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/cached_record/orm.rb', line 41

def uncached(id)
  raise NotImplementedError, "Cannot fetch uncached `#{self.class}` instances"
end