Class: Dry::Validation::PredicateRegistry
- Inherits:
-
Object
- Object
- Dry::Validation::PredicateRegistry
- Includes:
- Core::Constants
- Defined in:
- lib/dry/validation/predicate_registry.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#external ⇒ Object
readonly
Returns the value of attribute external.
-
#predicates ⇒ Object
readonly
Returns the value of attribute predicates.
Class Method Summary collapse
Instance Method Summary collapse
- #[](name) ⇒ Object
- #arg_list(name, *values) ⇒ Object
- #ensure_valid_predicate(name, args_or_arity, schema = nil) ⇒ Object
-
#initialize(external, predicates = {}) ⇒ PredicateRegistry
constructor
A new instance of PredicateRegistry.
- #key?(name) ⇒ Boolean
- #new(klass) ⇒ Object
Constructor Details
#initialize(external, predicates = {}) ⇒ PredicateRegistry
Returns a new instance of PredicateRegistry.
49 50 51 52 |
# File 'lib/dry/validation/predicate_registry.rb', line 49 def initialize(external, predicates = {}) @external = external @predicates = predicates end |
Instance Attribute Details
#external ⇒ Object (readonly)
Returns the value of attribute external.
10 11 12 |
# File 'lib/dry/validation/predicate_registry.rb', line 10 def external @external end |
#predicates ⇒ Object (readonly)
Returns the value of attribute predicates.
9 10 11 |
# File 'lib/dry/validation/predicate_registry.rb', line 9 def predicates @predicates end |
Class Method Details
.[](klass, predicates) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dry/validation/predicate_registry.rb', line 36 def self.[](klass, predicates) Unbound.new(predicates).tap do |registry| klass.class_eval do def self.method_added(name) super if name.to_s.end_with?('?') registry.update(name => instance_method(name)) end end end end end |
Instance Method Details
#[](name) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/dry/validation/predicate_registry.rb', line 62 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
76 77 78 79 80 81 82 83 |
# File 'lib/dry/validation/predicate_registry.rb', line 76 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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/dry/validation/predicate_registry.rb', line 85 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 Fixnum raise_invalid_arity_error(name) if args_or_arity != arity end else raise_unknown_predicate_error(name) end end |
#key?(name) ⇒ Boolean
72 73 74 |
# File 'lib/dry/validation/predicate_registry.rb', line 72 def key?(name) predicates.key?(name) || external.public_methods.include?(name) end |
#new(klass) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/dry/validation/predicate_registry.rb', line 54 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 |