class Verifly::Verifier

Verifier is a proto-validator class, which allows to use generic messages formats (instead of errors, which are raw text) @abstract

implement `#message!` method in terms of super

@attr model

Generic object to be verified

@attr messages [Array]

Array with all messages yielded by the verifier

Attributes

messages[RW]
model[RW]

Public Class Methods

bound_applicators() click to toggle source

@return [[ApplicatorWithOptions]]

List of applicators, bound by .verify
# File lib/verifly/verifier.rb, line 70
def self.bound_applicators
  @bound_applicators ||= []
end
call(model, context = {}) click to toggle source

@param model generic model to validate @param context context in which it is valdiated @return [Array] list of messages yielded by the verifier

# File lib/verifly/verifier.rb, line 77
def self.call(model, context = {})
  new(model).verify!(context)
end
new(model) click to toggle source

@param model generic model to validate

# File lib/verifly/verifier.rb, line 82
def initialize(model)
  self.model = model
  self.messages = []
end
verify(*args, &block) click to toggle source

@!method self.verify(action = block, options = {}, &block) @example with a block

verify { |context| message!() if context[:foo] }

@example with a proc

verify -> (context) { message!() if context[:foo] }

@example with a hash

verify -> { message!() } if: { foo: true }

@example context can be provided as a lambda param

verify -> { message!() }, if: -> (context) { context[:foo] }

@example with a symbol

verify :foo, if: :bar
# calls #foo if #bar is true
# bar can accept context if desired

@example with a string

verify 'message!() if context[:foo]'
verify 'message!()', if: 'context[:foo]'

@param action [#to_proc|Symbol|String|nil]

verifier defenition, see examples

@option options [#to_proc|Symbol|String|nil] if (true)

call verifier only if block invocation result is truthy

@option options [#to_proc|Symbol|String|nil] unless (false)

call verifier only if block invocation result is falsey

@yield [context] yields on `#verfify!` calls @raise [ArgumentError] if there is more than two arguments and block @raise [ArgumentError] if there is zero arguments and no block @return [Array] list of all defined verifiers

# File lib/verifly/verifier.rb, line 44
def self.verify(*args, &block)
  bound_applicators << ApplicatorWithOptions.new(*args, &block)
end
verify_with(name, options = {}) click to toggle source

Calls DescendantClass.call(model, context) and merges its messages. DescendantClass should be a descendant of current class @param name [String, Class]

name of descendant class or descendant class itself

@option options [#to_proc|Symbol|String|nil] if (true)

call verifier if only block invocation result is truthy

@option options [#to_proc|Symbol|String|nil] unless (false)

call verifier if only block invocation result is falsey

@return [Array] list of all verifiers already defined

# File lib/verifly/verifier.rb, line 57
    def self.verify_with(name, options = {})
      verify(options) 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

        messages.concat(verifier.call(model, context))
      end
    end

Public Instance Methods

verify!(context = {}) click to toggle source

@param context context in which model is valdiated @return [Array] list of messages yielded by the verifier

# File lib/verifly/verifier.rb, line 89
def verify!(context = {})
  self.messages = []

  self.class.bound_applicators.each do |bound_applicator|
    bound_applicator.call(self, context)
  end

  messages
end

Private Instance Methods

message!(*) { || ... } click to toggle source

@abstract

implementation example:
`super { Message.new(status, text, description) }`

@return new message (yield result)

# File lib/verifly/verifier.rb, line 105
def message!(*)
  new_message = yield
  @messages << new_message
  new_message
end