Module: Delorean::Model::ClassMethods

Defined in:
lib/delorean/model.rb

Instance Method Summary collapse

Instance Method Details

#cached_delorean_fn(name, options = {}, &block) ⇒ Object

By default implements a VERY HACKY class-based (per process) caching mechanism for database lookup results. Issues include: cached values are ActiveRecord objects. Query results can be very large lists which we count as one item in the cache. Caching mechanism will result in large processes.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/delorean/model.rb', line 36

def cached_delorean_fn(name, options = {}, &block)
  delorean_fn(name, options) do |*args|
    delorean_cache_adapter = ::Delorean::Cache.adapter
    # Check if caching should be performed
    next block.call(*args) unless delorean_cache_adapter.cache_item?(
      klass: self, method_name: name, args: args
    )

    cache_key = delorean_cache_adapter.cache_key(
      klass: self, method_name: name, args: args
    )
    cached_item = delorean_cache_adapter.fetch_item(
      klass: self, cache_key: cache_key, default: :NF
    )

    next cached_item if cached_item != :NF

    res = block.call(*args)

    delorean_cache_adapter.cache_item(
      klass: self, cache_key: cache_key, item: res
    )

    # Since we're caching this object and don't want anyone
    # changing it.  FIXME: ideally should freeze this object
    # recursively.
    res.freeze if res.respond_to?(:freeze)

    res
  end
end

#clear_lookup_cache!Object



68
69
70
# File 'lib/delorean/model.rb', line 68

def clear_lookup_cache!
  ::Delorean::Cache.adapter.clear!(klass: self)
end

#delorean_fn(name, options = {}, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/delorean/model.rb', line 10

def delorean_fn(name, options = {}, &block)
  define_singleton_method(name) do |*args|
    block.call(*args)
  end

  sig = options[:sig]

  raise "no signature" unless sig

  if sig
    sig = [sig, sig] if sig.is_a? Integer
    raise "Bad signature" unless (sig.is_a? Array and sig.length==2)
    self.const_set(name.to_s.upcase+Delorean::SIG, sig)
  end
end