class Verifier::Verification

Verification runner

@api private

Attributes

block[R]

@note

The class has a private constructor. Use {.run} method instead.
name[R]

@note

The class has a private constructor. Use {.run} method instead.
object[R]

@note

The class has a private constructor. Use {.run} method instead.
variable[R]

@note

The class has a private constructor. Use {.run} method instead.

Public Class Methods

run(object, variable, name, &block) click to toggle source

Constructs and runs a verification object

@param (see initialize)

@raise (see run)

@return (see run)

# File lib/verifier/verification.rb, line 50
def self.run(object, variable, name, &block)
  send(:new, object, variable, name, &block).run
end

Private Class Methods

new(object, variable, name, &block) click to toggle source

Initializes a verification object

@param [Object] object

the object whose method should be verified

@param [Symbol] variable

the name of the variable to be verified

@param [Symbol] name

the name of verification

@param [Proc] block

@return [undefined]

# File lib/verifier/verification.rb, line 21
def initialize(object, variable, name, &block)
  fail SyntaxError.new "No block given" unless block_given?
  @object, @variable, @name, @block = object, variable, name, block
end

Public Instance Methods

run() click to toggle source

Runs a verification

@yield block in the scope of a value returned by the {#variable}

@raise [Verifier::MethodNotDefined]

if the {#variable} not defined by {#object}

@raise [Verifier::MethodFails]

it calling the {#variable} raises a +StandardError+

@raise [Verifier::Invalid]

if the block fails or returns a value whose negation is truthy

@return [undefined]

# File lib/verifier/verification.rb, line 38
def run
  check_method_existence
  check_condition
end

Private Instance Methods

check_condition() click to toggle source
# File lib/verifier/verification.rb, line 72
def check_condition
  fail unless value.instance_eval(&block)
rescue
  raise invalid
end
check_method_existence() click to toggle source
# File lib/verifier/verification.rb, line 62
def check_method_existence
  fail(undefined) unless object.respond_to?(variable)
end
default_message() click to toggle source
# File lib/verifier/verification.rb, line 107
def default_message
  "#{ method_name } verification failed: #{ value.inspect } not #{ name }"
end
invalid() click to toggle source
# File lib/verifier/verification.rb, line 98
def invalid
  Invalid.new translate(
    name,
    scope:   scope.full,
    value:   value,
    default: default_message
  )
end
method_fails() click to toggle source
# File lib/verifier/verification.rb, line 89
def method_fails
  MethodFails.new translate(
    :fails,
    scope:   scope.short,
    value:   variable,
    default: "Method fails: #{ method_name }"
  )
end
method_name() click to toggle source

Translation helpers

# File lib/verifier/verification.rb, line 113
def method_name
  @method_name ||= "#{ object.class.name }#{ scope.tag }#{ variable }"
end
scope() click to toggle source
# File lib/verifier/verification.rb, line 117
def scope
  @scope ||= Scope.new(object, variable)
end
translate(name, options = {}) click to toggle source
# File lib/verifier/verification.rb, line 121
def translate(name, options = {})
  I18n.t(name, options)
end
undefined() click to toggle source

Exceptions to be raised

# File lib/verifier/verification.rb, line 80
def undefined
  MethodNotDefined.new translate(
    :undefined,
    scope:   scope.short,
    value:   variable,
    default: "Method not defined: #{ method_name }"
  )
end
value() click to toggle source
# File lib/verifier/verification.rb, line 66
def value
  @value ||= object.send(variable)
rescue
  raise method_fails
end