Class: Dry::Logic::Rule::Interface
- Inherits:
-
Module
- Object
- Module
- Dry::Logic::Rule::Interface
- Defined in:
- lib/dry/logic/rule/interface.rb
Constant Summary collapse
- SPLAT =
["*rest"].freeze
Instance Attribute Summary collapse
-
#arity ⇒ Object
readonly
Returns the value of attribute arity.
-
#curried ⇒ Object
readonly
Returns the value of attribute curried.
Instance Method Summary collapse
- #constant? ⇒ Boolean
- #curried? ⇒ Boolean
- #curried_args ⇒ Object
- #define_application ⇒ Object
- #define_constant_application ⇒ Object
- #define_constructor ⇒ Object
-
#initialize(arity, curried) ⇒ Interface
constructor
A new instance of Interface.
- #name ⇒ Object
- #unapplied ⇒ Object
- #unapplied_args ⇒ Object
- #variable_arity? ⇒ Boolean
Constructor Details
#initialize(arity, curried) ⇒ Interface
Returns a new instance of Interface.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/dry/logic/rule/interface.rb', line 13 def initialize(arity, curried) super() @arity = arity @curried = curried if !variable_arity? && curried > arity raise ArgumentError, "wrong number of arguments (#{curried} for #{arity})" end define_constructor if curried? if constant? define_constant_application else define_application end end |
Instance Attribute Details
#arity ⇒ Object (readonly)
Returns the value of attribute arity.
9 10 11 |
# File 'lib/dry/logic/rule/interface.rb', line 9 def arity @arity end |
#curried ⇒ Object (readonly)
Returns the value of attribute curried.
11 12 13 |
# File 'lib/dry/logic/rule/interface.rb', line 11 def curried @curried end |
Instance Method Details
#constant? ⇒ Boolean
32 |
# File 'lib/dry/logic/rule/interface.rb', line 32 def constant? = arity.zero? |
#curried? ⇒ Boolean
36 |
# File 'lib/dry/logic/rule/interface.rb', line 36 def curried? = !curried.zero? |
#curried_args ⇒ Object
127 128 129 |
# File 'lib/dry/logic/rule/interface.rb', line 127 def curried_args @curried_args ||= ::Array.new(curried) { "@arg#{_1}" } end |
#define_application ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/dry/logic/rule/interface.rb', line 107 def define_application splat = variable_arity? ? SPLAT : EMPTY_ARRAY parameters = (unapplied_args + splat).join(", ") application = "@predicate[#{(curried_args + unapplied_args + splat).join(", ")}]" module_eval(<<~RUBY, __FILE__, __LINE__ + 1) def call(#{parameters}) # def call(input0, input1, *rest) if #{application} # if @predicate[@arg0, @arg1, input0, input1, *rest] Result::SUCCESS # ::Dry::Logic::Result::Success else # else Result.new(false, id) { ast(#{parameters}) } # ::Dry::Logic::Result.new(false, id) { ast(input0, input1, *rest) } end # end end # end # def [](#{parameters}) # def [](@arg0, @arg1, input0, input1, *rest) #{application} # @predicate[@arg0, @arg1, input0, input1, *rest] end # end RUBY end |
#define_constant_application ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/dry/logic/rule/interface.rb', line 91 def define_constant_application module_exec do def call(*) if @predicate[] Result::SUCCESS else Result.new(false, id) { ast } end end def [](*) @predicate[] end end end |
#define_constructor ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/dry/logic/rule/interface.rb', line 74 def define_constructor assignment = if curried.equal?(1) "@arg0 = @args[0]" else "#{curried_args.join(", ")} = @args" end module_eval(<<~RUBY, __FILE__, __LINE__ + 1) def initialize(*) # def initialize(*) super # super # #{assignment} # @arg0 = @args[0] end # end RUBY end |
#name ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/dry/logic/rule/interface.rb', line 52 def name if constant? "Constant" else arity_str = if variable_arity? "Variable#{arity.abs - 1}Arity" else "#{arity}Arity" end curried_str = if curried? "#{curried}Curried" else EMPTY_STRING end "#{arity_str}#{curried_str}" end end |
#unapplied ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/dry/logic/rule/interface.rb', line 38 def unapplied if variable_arity? unapplied = arity.abs - 1 - curried if unapplied.negative? 0 else unapplied end else arity - curried end end |
#unapplied_args ⇒ Object
131 132 133 |
# File 'lib/dry/logic/rule/interface.rb', line 131 def unapplied_args @unapplied_args ||= ::Array.new(unapplied) { "input#{_1}" } end |
#variable_arity? ⇒ Boolean
34 |
# File 'lib/dry/logic/rule/interface.rb', line 34 def variable_arity? = arity.negative? |