class ActiveValidation::Verifier

Attributes

base_klass[R]

@note The corresponding model class

enabled[RW]

Main switch for this class. After switch, all installed validators remain in place, while new installations will be turned off.

@return [TrueClass, FalseClass]

failed_attempt_retry_time[RW]

If something go wrong or there is no any Manifests for selected class yet, to prevent DB flooding we just until the situation resolves. Here we can specify the period between the attempts

manifest[RW]

Stick manifest to specific version @return [Internal::Manifest]

manifest_name_formatter[RW]

@note Custom name formatter for Manifests

observer[R]

Contains Internal::Observers::Manifest, which store everything about used manifests

@return [Internal::Observers::Manifest]

orm_adapter[RW]

@note Orm adapter for exactly this verifier instance

validations_module_name[RW]

Name of the folder, where all validations method should be scoped. Inside, in corresponding sub-folder with version name shall be stored validation related methods

Public Class Methods

new(base_klass) { |self| ... } click to toggle source
# File lib/active_validation/verifier.rb, line 55
def initialize(base_klass)
  config.verifier_defaults.call self
  @base_klass = base_klass.to_s
  @orm_adapter ||= config.orm_adapter
  @manifest_name_formatter ||= config.manifest_name_formatter
  @validations_module_name ||= "Validations"
  @enabled ||= true
  @failed_attempt_retry_time ||= 1.day

  yield self if block_given?

  @observer = Internal::Observers::Manifest.new self
  self.class.registry.register base_klass, self
end
registry() click to toggle source

Shortcut for the global verifiers registry instance @return [Registry]

# File lib/active_validation/verifier.rb, line 8
def registry
  ActiveValidation.config.verifiers_registry
end

Public Instance Methods

add_manifest(**hash) click to toggle source

Forward the normalized request to ORM mapper For the name field we calculate default value

param [Hash] @return [Internal::Manifest, Array<Internal::Manifest>]

# File lib/active_validation/verifier.rb, line 126
def add_manifest(**hash)
  add_defaults_for_orm_adapter(hash) do |**h|
    h[:name] ||= manifest_name_formatter.call(base_klass)
    orm_adapter.public_send :add_manifest, h
  end
end
base_class() click to toggle source

@return [Class]

# File lib/active_validation/verifier.rb, line 96
def base_class
  base_klass.is_a?(Class) ? base_klass : base_klass.constantize
end
current_manifest() click to toggle source

@return [Internal::Manifest]

# File lib/active_validation/verifier.rb, line 91
def current_manifest
  manifest or find_manifests.first
end
descendants_with_active_validation() click to toggle source

Return the list af parents with active_validation @return [Array]

# File lib/active_validation/verifier.rb, line 102
def descendants_with_active_validation
  [].tap do |descendants|
    k = base_class.superclass
    while k.respond_to?(:active_validation) && k.instance_methods.include?(:manifest)
      descendants << k
      k = k.superclass
    end
  end
end
enabled?() click to toggle source
# File lib/active_validation/verifier.rb, line 45
def enabled?; !!enabled; end
install!(*args) click to toggle source

Install parent models and self

Return Symbol Installation status

# File lib/active_validation/verifier.rb, line 115
def install!(*args)
  descendants_with_active_validation.each { |klass| klass.active_validation.install }
  install(*args)
end
version() click to toggle source

@!group Manual version lock

# File lib/active_validation/verifier.rb, line 79
def version
  @version ||= versions.last
end
version=(other) click to toggle source
# File lib/active_validation/verifier.rb, line 83
def version=(other)
  @version = versions.detect { |a| a == ActiveValidation::Values::Version.new(other) } or
    raise ArgumentError, "Version #{other} not found"
end
versions() click to toggle source

@return [Array<Value::Version>] Sorted list of versions.

# File lib/active_validation/verifier.rb, line 71
def versions
  base_class.const_get(validations_module_name)
            .constants.map { |k| ActiveValidation::Values::Version.new(k) }.sort
rescue NameError
  []
end

Private Instance Methods

add_defaults_for_orm_adapter(**hash) { |hash| ... } click to toggle source
# File lib/active_validation/verifier.rb, line 144
def add_defaults_for_orm_adapter(**hash)
  hash[:base_klass] ||= base_klass
  hash[:version]    ||= version if version
  block_given? ? yield(hash) : hash
end