lib/contextualizer.rb in contextualizer-0.0.6 vs lib/contextualizer.rb in contextualizer-0.1.0
- old
+ new
@@ -1,7 +1,9 @@
-require 'contextualizer/version'
+# frozen_string_literal: true
+require "contextualizer/version"
+
module Contextualizer
OPTIONAL = Object.new.freeze
def context(*attrs, **opt_attrs)
unless @__setter
@@ -22,18 +24,18 @@
end
def self.init_for(setter, inherited = true)
Module.new do |mod|
if inherited
- mod.send(:define_method, :initialize) do |args = {}|
- super(args)
- setter.set(self, args)
+ mod.send(:define_method, :initialize) do |ctx = {}|
+ super(ctx)
+ setter.set(self, ctx)
end
else
- mod.send(:define_method, :initialize) do |args = {}|
+ mod.send(:define_method, :initialize) do |ctx = {}|
super()
- setter.set(self, args)
+ setter.set(self, ctx)
end
end
end
end
@@ -48,17 +50,17 @@
@mandatory |= attrs
@optional |= optional.keys
@with_default.merge!(with_default)
end
- def set(obj, args)
+ def set(obj, ctx)
context = obj.context&.dup || {}
- @with_default.each { |key, default| context[key] = args.fetch(key, default) }
- @optional.each { |key| context[key] = args[key] if args.key?(key) }
+ @with_default.each { |key, default| context[key] = ctx.fetch(key, default) }
+ @optional.each { |key| context[key] = ctx[key] if ctx.key?(key) }
@mandatory.each do |key|
- fail ":#{key} was not found in scope" unless args.key?(key)
- context[key] = args[key]
+ fail ":#{key} was not found in scope" unless ctx.key?(key)
+ context[key] = ctx[key]
end
context.each do |attr, value|
obj.instance_variable_set :"@#{attr}", value
end