Sha256: 8f88c81fc02cd7f29f4e9d9656bc9a675eda3b114f7dc0978a9ef21b1c0880bd
Contents?: true
Size: 1.65 KB
Versions: 1
Compression:
Stored size: 1.65 KB
Contents
require 'hotcell/scope' module Hotcell class Context attr_reader :options delegate :[], :[]=, :key?, :scoped, to: :scope DEFAULT_RESCUER = ->(e){ "#{e.class}: #{e.message}" } def initialize options = {} @options = normalize_options options end def normalize_options options options = options.dup scope = options.delete(:scope) || {} scope.merge! (options.delete(:variables) || {}).stringify_keys scope.merge! (options.delete(:environment) || {}).symbolize_keys shared = options.delete(:shared).presence || {} helpers = Array.wrap(options.delete(:helpers)) rescuer = options.delete(:rescuer) || DEFAULT_RESCUER reraise = !!options.delete(:reraise) scope.merge!(options.stringify_keys) { scope: scope, shared: shared, helpers: helpers, reraise: reraise, rescuer: rescuer } end def scope @scope ||= Scope.new options[:scope] end def shared @shared ||= options[:shared] end def safe *default yield rescue => e rescue_result = options[:rescuer].call(e) default.size > 0 ? default.first : rescue_result ensure raise e if e && options[:reraise] end def tong_invoke method, *arguments if arguments.any? helpers.tong_invoke(method, *arguments) else scope.key?(method) ? scope[method] : helpers.tong_invoke(method) end end private def helpers @helpers ||= helpers_class.new end def helpers_class @helpers_class ||= Class.new(Hotcell::Tong).tap do |klass| options[:helpers].each { |helper| klass.send(:include, helper) } end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
hotcell-0.3.0 | lib/hotcell/context.rb |