lib/atacama/contract.rb in atacama-0.1.2 vs lib/atacama/contract.rb in atacama-0.1.3
- old
+ new
@@ -19,33 +19,50 @@
NameInterface = Types::Strict::Symbol.constrained(excluded_from: RESERVED_KEYS)
ContextInterface = Types::Strict::Hash | Types.Instance(Context)
class << self
+ def injected=(hash)
+ @injected = Types::Strict::Hash[hash]
+ end
+
+ def injected
+ @injected || {}
+ end
+
def options
@options ||= {}
end
# Define an initializer value.
# @param [Symbol] name of the argument
def option(name, **kwargs)
- NameInterface[name] # Validate type
- options[name] = Parameter.new(name: name, **kwargs)
+ options[NameInterface[name]] = Parameter.new(name: name, **kwargs)
define_method(name) { @context[name] }
define_method("#{name}?") { !!@context[name] }
end
def call(context = {})
new(context: context).call
end
+
+ def inject(injected)
+ Class.new(self) do
+ self.injected = injected
+ end
+ end
end
attr_reader :context
def initialize(context: {}, **)
ContextInterface[context] # Validate the type
- @context = context.is_a?(Context) ? context : Context.new(context)
+
+ @context = Context.new(self.class.injected).tap do |ctx|
+ ctx.merge!(context.is_a?(Context) ? context.to_h : context)
+ end
+
Validator.call(options: self.class.options, context: @context)
end
def call
self