Class: Verifly::Verifier Abstract
- Inherits:
-
Object
- Object
- Verifly::Verifier
- Defined in:
- lib/verifly/verifier.rb
Overview
implement ‘#message!` method in terms of super
Verifier is a proto-validator class, which allows to use generic messages formats (instead of errors, which are raw text)
Instance Attribute Summary collapse
-
#messages ⇒ Array
Array with all messages yielded by the verifier.
-
#model ⇒ Object
Generic object to be verified.
Class Method Summary collapse
-
.bound_applicators ⇒ [ApplicatorWithOptions]
List of applicators, bound by .verify.
-
.call(model, context = {}) ⇒ Array
List of messages yielded by the verifier.
-
.verify(action = block, options = {}) {|context| ... } ⇒ Array
List of all defined verifiers.
-
.verify_with(name, options = {}) ⇒ Array
Calls DescendantClass.call(model, context) and merges its messages.
Instance Method Summary collapse
-
#initialize(model) ⇒ Verifier
constructor
A new instance of Verifier.
-
#verify!(context = {}) ⇒ Array
List of messages yielded by the verifier.
Constructor Details
#initialize(model) ⇒ Verifier
Returns a new instance of Verifier.
82 83 84 85 |
# File 'lib/verifly/verifier.rb', line 82 def initialize(model) self.model = model self. = [] end |
Instance Attribute Details
#messages ⇒ Array
Array with all messages yielded by the verifier
12 13 14 |
# File 'lib/verifly/verifier.rb', line 12 def @messages end |
#model ⇒ Object
Generic object to be verified
12 13 14 |
# File 'lib/verifly/verifier.rb', line 12 def model @model end |
Class Method Details
.bound_applicators ⇒ [ApplicatorWithOptions]
Returns List of applicators, bound by .verify.
70 71 72 |
# File 'lib/verifly/verifier.rb', line 70 def self.bound_applicators @bound_applicators ||= [] end |
.call(model, context = {}) ⇒ Array
Returns list of messages yielded by the verifier.
77 78 79 |
# File 'lib/verifly/verifier.rb', line 77 def self.call(model, context = {}) new(model).verify!(context) end |
.verify(action = block, options = {}) {|context| ... } ⇒ Array
Returns list of all defined verifiers.
44 45 46 |
# File 'lib/verifly/verifier.rb', line 44 def self.verify(*args, &block) bound_applicators << ApplicatorWithOptions.new(*args, &block) end |
.verify_with(name, options = {}) ⇒ Array
Calls DescendantClass.call(model, context) and merges its messages. DescendantClass should be a descendant of current class
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/verifly/verifier.rb', line 57 def self.verify_with(name, = {}) verify() do |context| verifier = name.is_a?(String) ? Object.const_get(name, false) : name raise ArgumentError, <<~ERROR unless verifier < self.class Nested verifiers should be inherited from verifier they nested are in ERROR .concat(verifier.call(model, context)) end end |
Instance Method Details
#verify!(context = {}) ⇒ Array
Returns list of messages yielded by the verifier.
89 90 91 92 93 94 95 96 97 |
# File 'lib/verifly/verifier.rb', line 89 def verify!(context = {}) self. = [] self.class.bound_applicators.each do |bound_applicator| bound_applicator.call(self, context) end end |