Module: Igniter::Extensions::Contracts::BranchPack
- Defined in:
- lib/igniter/extensions/contracts/branch_pack.rb
Defined Under Namespace
Classes: Definition
Constant Summary collapse
- UNSET =
Object.new.freeze
Class Method Summary collapse
- .branch_keyword ⇒ Object
- .install_into(kernel) ⇒ Object
- .invoke_value(callable_or_value, kwargs) ⇒ Object
- .manifest ⇒ Object
Class Method Details
.branch_keyword ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/igniter/extensions/contracts/branch_pack.rb', line 22 def branch_keyword Igniter::Contracts::DslKeyword.new(:branch) do |name, on:, builder:, depends_on: [], &block| raise ArgumentError, "branch :#{name} requires a block" unless block selector_name = on.to_sym dependency_names = [selector_name, *Array(depends_on).map(&:to_sym)].uniq definition = Definition.new(name: name, selector_name: selector_name) definition.instance_eval(&block) definition.validate! builder.add_operation( kind: :compute, name: name, depends_on: dependency_names, callable: lambda do |**values| definition.resolve(values) end ) end end |
.install_into(kernel) ⇒ Object
17 18 19 20 |
# File 'lib/igniter/extensions/contracts/branch_pack.rb', line 17 def install_into(kernel) kernel.dsl_keywords.register(:branch, branch_keyword) kernel end |
.invoke_value(callable_or_value, kwargs) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/igniter/extensions/contracts/branch_pack.rb', line 43 def invoke_value(callable_or_value, kwargs) return callable_or_value unless callable_or_value.respond_to?(:call) parameters = callable_or_value.parameters accepts_any_keywords = parameters.any? { |kind, _name| kind == :keyrest } return callable_or_value.call(**kwargs) if accepts_any_keywords accepted = parameters.select { |kind, _name| %i[key keyreq].include?(kind) }.map(&:last) callable_or_value.call(**kwargs.slice(*accepted)) end |