From aaf5037d58e6cedcf5fb234a482f787e41a21c1b Mon Sep 17 00:00:00 2001 From: Nikita Shilnikov Date: Fri, 3 Jan 2025 19:40:10 +0100 Subject: [PATCH] Syntax updates --- lib/dry/logic/operations/abstract.rb | 2 +- lib/dry/logic/operations/each.rb | 2 +- lib/dry/logic/result.rb | 2 +- lib/dry/logic/rule.rb | 4 +-- lib/dry/logic/rule/interface.rb | 38 ++++++++++++---------------- 5 files changed, 21 insertions(+), 27 deletions(-) diff --git a/lib/dry/logic/operations/abstract.rb b/lib/dry/logic/operations/abstract.rb index 41a8738..408eadc 100644 --- a/lib/dry/logic/operations/abstract.rb +++ b/lib/dry/logic/operations/abstract.rb @@ -5,7 +5,7 @@ module Logic module Operations class Abstract include Core::Constants - include Dry::Equalizer(:rules, :options) + include ::Dry::Equalizer(:rules, :options) include Operators attr_reader :rules diff --git a/lib/dry/logic/operations/each.rb b/lib/dry/logic/operations/each.rb index a3b0320..23374df 100644 --- a/lib/dry/logic/operations/each.rb +++ b/lib/dry/logic/operations/each.rb @@ -23,7 +23,7 @@ def call(input) end def [](arr) - arr.map { |input| rule[input] }.all? + arr.all? { |input| rule[input] } end end end diff --git a/lib/dry/logic/result.rb b/lib/dry/logic/result.rb index 25c9a9d..f563387 100644 --- a/lib/dry/logic/result.rb +++ b/lib/dry/logic/result.rb @@ -3,7 +3,7 @@ module Dry module Logic class Result - SUCCESS = Class.new { + SUCCESS = ::Class.new { def success? true end diff --git a/lib/dry/logic/rule.rb b/lib/dry/logic/rule.rb index 1efbce1..5e7ffd1 100644 --- a/lib/dry/logic/rule.rb +++ b/lib/dry/logic/rule.rb @@ -14,7 +14,7 @@ def self.Rule(*args, **options, &block) class Rule include Core::Constants - include Dry::Equalizer(:predicate, :options) + include ::Dry::Equalizer(:predicate, :options) include Operators attr_reader :predicate @@ -32,7 +32,7 @@ def self.interfaces def self.specialize(arity, curried, base = Rule) base.interfaces.fetch_or_store([arity, curried]) do interface = Interface.new(arity, curried) - klass = Class.new(base) { include interface } + klass = ::Class.new(base) { include interface } base.const_set("#{base.name.split("::").last}#{interface.name}", klass) klass end diff --git a/lib/dry/logic/rule/interface.rb b/lib/dry/logic/rule/interface.rb index e083304..4560e4e 100644 --- a/lib/dry/logic/rule/interface.rb +++ b/lib/dry/logic/rule/interface.rb @@ -29,17 +29,11 @@ def initialize(arity, curried) end end - def constant? - arity.zero? - end + def constant? = arity.zero? - def variable_arity? - arity.negative? - end + def variable_arity? = arity.negative? - def curried? - !curried.zero? - end + def curried? = !curried.zero? def unapplied if variable_arity? @@ -116,26 +110,26 @@ def define_application 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 + 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 def curried_args - @curried_args ||= ::Array.new(curried) { |i| "@arg#{i}" } + @curried_args ||= ::Array.new(curried) { "@arg#{_1}" } end def unapplied_args - @unapplied_args ||= ::Array.new(unapplied) { |i| "input#{i}" } + @unapplied_args ||= ::Array.new(unapplied) { "input#{_1}" } end end end