Skip to content

Commit

Permalink
Syntax updates
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Jan 3, 2025
1 parent 1e72b21 commit aaf5037
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/dry/logic/operations/abstract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/logic/operations/each.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def call(input)
end

def [](arr)
arr.map { |input| rule[input] }.all?
arr.all? { |input| rule[input] }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/logic/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Dry
module Logic
class Result
SUCCESS = Class.new {
SUCCESS = ::Class.new {
def success?
true
end
Expand Down
4 changes: 2 additions & 2 deletions lib/dry/logic/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
38 changes: 16 additions & 22 deletions lib/dry/logic/rule/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aaf5037

Please sign in to comment.