lib/delorean/base.rb in delorean_lang-2.1.0 vs lib/delorean/base.rb in delorean_lang-2.3.0

- old
+ new

@@ -4,20 +4,27 @@ require 'active_record' require 'bigdecimal' require 'delorean/ruby' require 'delorean/ruby/whitelists/default' require 'delorean/cache' +require 'delorean/const' module Delorean ::Delorean::Ruby.whitelist = ::Delorean::Ruby::Whitelists::Default.new ::Delorean::Cache.adapter = ::Delorean::Cache::Adapters::RubyCache.new( size_per_class: 1000 ) ::Delorean::Ruby.error_handler = ::Delorean::Ruby::DEFAULT_ERROR_HANDLER + cache_callback = ::Delorean::Cache::NODE_CACHE_DEFAULT_CALLBACK + + ::Delorean::Cache.node_cache_callback = cache_callback + + NODE_CACHE_ARG = "_cache#{POST}".to_sym + module BaseModule # _e is used by Marty promise_jobs to pass promise-related # information class NodeCall < Struct.new(:_e, :engine, :node, :params) def cloned_params @@ -27,13 +34,28 @@ # This whole thing needs to be redone. @cloned_params ||= Hash[params] end def evaluate(attr) + if node.respond_to?(NODE_CACHE_ARG) && node.send(NODE_CACHE_ARG, _e) + return _evaluate_with_cache(attr) + end + engine.evaluate(node, attr, cloned_params) end + def _evaluate_with_cache(attr) + ::Delorean::Cache.with_cache( + klass: node, + method: attr, + mutable_params: cloned_params, + params: params + ) do + engine.evaluate(node, attr, cloned_params) + end + end + def /(args) case args when Array engine.eval_to_hash(node, args, cloned_params) when String @@ -83,11 +105,11 @@ when Class return obj.send((attr + POST).to_sym, _e) if obj < BaseClass end begin - return _instance_call(obj, attr, [], _e) + _instance_call(obj, attr, [], _e) rescue StandardError => exc raise( InvalidGetAttribute, "attr lookup failed: '#{attr}' on <#{obj.class}> #{obj} - #{exc}" ) @@ -109,12 +131,12 @@ # If key is not found, check if object responds to method and call it. # If not succeeded, return nil, assuming that it was an attribute call. return nil unless obj.respond_to?(attr) begin - return _instance_call(obj, attr, [], _e) + _instance_call(obj, attr, [], _e) rescue StandardError - return nil + nil end end ######################################################################