File: rule_application.rb

package info (click to toggle)
ruby-dry-logic 1.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 728 kB
  • sloc: ruby: 4,929; makefile: 6
file content (30 lines) | stat: -rw-r--r-- 996 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

require_relative "setup"

unless Dry::Logic::Rule.respond_to?(:build)
  Dry::Logic::Rule.singleton_class.alias_method(:build, :new)
end

predicates = Dry::Logic::Predicates

type_check = Dry::Logic::Rule.build(predicates[:type?]).curry(Integer)
int_check = Dry::Logic::Rule.build(predicates[:int?])
key_check = Dry::Logic::Rule.build(predicates[:key?]).curry(:user)
with_user = { user: {} }
without_user = {}

comparison = Dry::Logic::Rule.build(predicates[:gteq?]).curry(18)

Benchmark.ips do |x|
  x.report("type check - success") { type_check.(0) }
  x.report("type check - failure") { type_check.("0") }
  x.report("int check - success") { int_check.(0) }
  x.report("int check - failure") { int_check.("0") }
  x.report("key check - success") { key_check.(with_user) }
  x.report("key check - failure") { key_check.(without_user) }
  x.report("comparison - success") { comparison.(20) }
  x.report("comparison - failure") { comparison.(17) }

  x.compare!
end