Class: Dry::Validation::PredicateRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/validation/predicate_registry.rb

Direct Known Subclasses

Bound, Unbound

Defined Under Namespace

Modules: PredicateDetector Classes: Bound, Unbound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(external, predicates = {}) ⇒ PredicateRegistry

Returns a new instance of PredicateRegistry.



47
48
49
50
# File 'lib/dry/validation/predicate_registry.rb', line 47

def initialize(external, predicates = {})
  @external = external
  @predicates = predicates
end

Instance Attribute Details

#externalObject (readonly)

Returns the value of attribute external.



17
18
19
# File 'lib/dry/validation/predicate_registry.rb', line 17

def external
  @external
end

#predicatesObject (readonly)

Returns the value of attribute predicates.



16
17
18
# File 'lib/dry/validation/predicate_registry.rb', line 16

def predicates
  @predicates
end

Class Method Details

.[](predicates) ⇒ Object



43
44
45
# File 'lib/dry/validation/predicate_registry.rb', line 43

def self.[](predicates)
  Unbound.new(predicates)
end

Instance Method Details

#[](name) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/dry/validation/predicate_registry.rb', line 60

def [](name)
  predicates.fetch(name) do
    if external.public_methods.include?(name)
      external[name]
    else
      raise_unknown_predicate_error(name)
    end
  end
end

#arg_list(name, *values) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/dry/validation/predicate_registry.rb', line 74

def arg_list(name, *values)
  predicate = self[name]

  predicate
    .parameters
    .map(&:last)
    .zip(values + Array.new(predicate.arity - values.size, Undefined))
end

#ensure_valid_predicate(name, args_or_arity, schema = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/dry/validation/predicate_registry.rb', line 83

def ensure_valid_predicate(name, args_or_arity, schema = nil)
  return if schema && schema.instance_methods.include?(name)

  if name == :key?
    raise InvalidSchemaError, "#{name} is a reserved predicate name"
  end

  if key?(name)
    arity = self[name].arity

    case args_or_arity
    when Array
      raise_invalid_arity_error(name) if ![0, args_or_arity.size + 1].include?(arity)
    when Integer
      raise_invalid_arity_error(name) if args_or_arity != arity
    end
  else
    raise_unknown_predicate_error(name)
  end
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/dry/validation/predicate_registry.rb', line 70

def key?(name)
  predicates.key?(name) || external.public_methods.include?(name)
end

#new(klass) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/dry/validation/predicate_registry.rb', line 52

def new(klass)
  new_predicates = predicates
    .keys
    .each_with_object({}) { |key, res| res[key] = klass.instance_method(key) }

  self.class.new(external).update(new_predicates)
end