resolving / dry-logic

Predicate logic with rule composition

Home Page:https://dry-rb.org/gems/dry-logic/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dry-logic Join the chat at https://gitter.im/dry-rb/chat

Gem Version Build Status Dependency Status Code Climate Test Coverage Inline docs

Predicate logic and rule composition used by:

Synopsis

require 'dry/logic'
require 'dry/logic/predicates'

include Dry::Logic

user_present = Rule::Key.new(Predicates[:filled?], name: :user)

has_min_age = Rule::Key.new(Predicates[:int?], name: [:user, :age])
  & Rule::Key.new(Predicates[:gt?].curry(18), name: [:user, :age])

user_rule = user_present & has_min_age

user_rule.(user: { age: 19 })
# #<Dry::Logic::Result::Named success?=true input={:user=>{:age=>19}} rule=#<Dry::Logic::Rule::Key predicate=#<Dry::Logic::Predicate id=:gt? args=[18, 19]> options={:evaluator=>#<Dry::Logic::Evaluator::Key path=[:user, :age]>, :name=>[:user, :age]}>>

user_rule.(user: { age: 18 })
# #<Dry::Logic::Result::Named success?=false input={:user=>{:age=>18}} rule=#<Dry::Logic::Rule::Key predicate=#<Dry::Logic::Predicate id=:gt? args=[18, 18]> options={:evaluator=>#<Dry::Logic::Evaluator::Key path=[:user, :age]>, :name=>[:user, :age]}>>

user_rule.(user: { age: 'seventeen' }).inspect
#<Dry::Logic::Result::Named success?=false input={:user=>{:age=>"seventeen"}} rule=#<Dry::Logic::Rule::Key predicate=#<Dry::Logic::Predicate id=:int? args=["seventeen"]> options={:evaluator=>#<Dry::Logic::Evaluator::Key path=[:user, :age]>, :name=>[:user, :age]}>>

user_rule.(user: { }).inspect
#<Dry::Logic::Result::Named success?=false input={:user=>{}} rule=#<Dry::Logic::Rule::Key predicate=#<Dry::Logic::Predicate id=:filled? args=[{}]> options={:evaluator=>#<Dry::Logic::Evaluator::Key path=[:user]>, :name=>:user}>>

puts user_rule.({}).inspect
#<Dry::Logic::Result::Named success?=false input={} rule=#<Dry::Logic::Rule::Key predicate=#<Dry::Logic::Predicate id=:filled? args=[nil]> options={:evaluator=>#<Dry::Logic::Evaluator::Key path=[:user]>, :name=>:user}>>

License

See LICENSE file.

About

Predicate logic with rule composition

https://dry-rb.org/gems/dry-logic/

License:MIT License


Languages

Language:Ruby 100.0%