Class: Verifly::Verifier Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/verifly/verifier.rb

Overview

This class is abstract.

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Verifier

Returns a new instance of Verifier.

Parameters:

  • model

    generic model to validate



82
83
84
85
# File 'lib/verifly/verifier.rb', line 82

def initialize(model)
  self.model = model
  self.messages = []
end

Instance Attribute Details

#messagesArray

Array with all messages yielded by the verifier

Returns:

  • (Array)

    the current value of messages



12
13
14
# File 'lib/verifly/verifier.rb', line 12

def messages
  @messages
end

#modelObject

Generic object to be verified

Returns:

  • (Object)

    the current value of model



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.

Returns:



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.

Parameters:

  • model

    generic model to validate

  • context (defaults to: {})

    context in which it is valdiated

Returns:

  • (Array)

    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.

Examples:

with a block

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

with a proc

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

with a hash

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

context can be provided as a lambda param

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

with a symbol

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

with a string

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

Parameters:

  • action (#to_proc|Symbol|String|nil) (defaults to: block)

    verifier defenition, see examples

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • if (#to_proc|Symbol|String|nil) — default: true

    call verifier only if block invocation result is truthy

  • unless (#to_proc|Symbol|String|nil) — default: false

    call verifier only if block invocation result is falsey

Yields:

  • (context)

    yields on ‘#verfify!` calls

Returns:

  • (Array)

    list of all defined verifiers

Raises:

  • (ArgumentError)

    if there is more than two arguments and block

  • (ArgumentError)

    if there is zero arguments and no block



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

Parameters:

  • name (String, Class)

    name of descendant class or descendant class itself

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • if (#to_proc|Symbol|String|nil) — default: true

    call verifier if only block invocation result is truthy

  • unless (#to_proc|Symbol|String|nil) — default: false

    call verifier if only block invocation result is falsey

Returns:

  • (Array)

    list of all verifiers already defined



57
58
59
60
61
62
63
64
65
66
# 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

Instance Method Details

#verify!(context = {}) ⇒ Array

Returns list of messages yielded by the verifier.

Parameters:

  • context (defaults to: {})

    context in which model is valdiated

Returns:

  • (Array)

    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.messages = []

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

  messages
end