lib/delorean/model.rb in delorean_lang-0.5.1 vs lib/delorean/model.rb in delorean_lang-0.5.2
- old
+ new
@@ -1,45 +1,48 @@
+# frozen_string_literal: true
+
require 'delorean/const'
module Delorean
module Model
def self.included(base)
base.send :extend, ClassMethods
end
module ClassMethods
- def delorean_fn(name, options = {}, &block)
+ def delorean_fn(name, options = {})
define_singleton_method(name) do |*args|
- block.call(*args)
+ yield(*args)
end
sig = options[:sig]
- raise "no signature" unless 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)
+ raise 'Bad signature' unless sig.is_a?(Array) && (sig.length == 2)
+
+ const_set(name.to_s.upcase + Delorean::SIG, sig)
end
end
- # FIXME IDEA: we just make :cache an argument to delorean_fn.
+ # FIXME: IDEA: we just make :cache an argument to delorean_fn.
# That way, we don't need the cached_ flavors. It'll make all
# this code a lot simpler. We should also just add the :private
# mechanism here.
# 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.
- def cached_delorean_fn(name, options = {}, &block)
+ 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 block.call(*args) unless delorean_cache_adapter.cache_item?(
+ 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
@@ -48,10 +51,10 @@
klass: self, cache_key: cache_key, default: :NF
)
next cached_item if cached_item != :NF
- res = block.call(*args)
+ res = yield(*args)
delorean_cache_adapter.cache_item(
klass: self, cache_key: cache_key, item: res
)