Module: Delorean::Functions

Defined in:
lib/delorean/functions.rb

Instance Method Summary collapse

Instance Method Details

#cached_delorean_fn(name, options = {}) ⇒ 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.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/delorean/functions.rb', line 25

def cached_delorean_fn(name, options = {})
  delorean_fn(name, options) do |*args|
    delorean_cache_adapter = ::Delorean::Cache.adapter
    # Check if caching should be performed
    next yield(*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 = yield(*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



57
58
59
# File 'lib/delorean/functions.rb', line 57

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

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



5
6
7
8
9
10
11
12
13
# File 'lib/delorean/functions.rb', line 5

def delorean_fn(name, _options = {}, &block)
  any_args = Delorean::Ruby::Whitelists::Matchers::Arguments::ANYTHING

  define_singleton_method(name, block)

  ::Delorean::Ruby.whitelist.add_class_method name do |method|
    method.called_on self, with: any_args
  end
end