File: constraints.rb

package info (click to toggle)
ruby-dry-types 1.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 504 kB
  • sloc: ruby: 3,059; makefile: 4
file content (34 lines) | stat: -rw-r--r-- 722 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
31
32
33
34
# frozen_string_literal: true

require 'dry/logic/rule_compiler'
require 'dry/logic/predicates'
require 'dry/logic/rule/predicate'

module Dry
  # Helper methods for constraint types
  #
  # @api public
  module Types
    # @param [Hash] options
    #
    # @return [Dry::Logic::Rule]
    #
    # @api public
    def self.Rule(options)
      rule_compiler.(
        options.map { |key, val|
          Logic::Rule::Predicate.build(
            Logic::Predicates[:"#{key}?"]
          ).curry(val).to_ast
        }
      ).reduce(:and)
    end

    # @return [Dry::Logic::RuleCompiler]
    #
    # @api private
    def self.rule_compiler
      @rule_compiler ||= Logic::RuleCompiler.new(Logic::Predicates)
    end
  end
end