Class: ActiveValidation::Verifier

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

Instance Attribute Summary collapse

Manual version lock collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_klass) {|_self| ... } ⇒ Verifier

Returns a new instance of Verifier.

Yields:

  • (_self)

Yield Parameters:



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

Instance Attribute Details

#base_klassObject (readonly)

Note:

The corresponding model class



22
23
24
# File 'lib/active_validation/verifier.rb', line 22

def base_klass
  @base_klass
end

#enabledTrueClass, FalseClass

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

Returns:

  • (TrueClass, FalseClass)


44
45
46
# File 'lib/active_validation/verifier.rb', line 44

def enabled
  @enabled
end

#failed_attempt_retry_timeObject

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



51
52
53
# File 'lib/active_validation/verifier.rb', line 51

def failed_attempt_retry_time
  @failed_attempt_retry_time
end

#manifestInternal::Manifest

Stick manifest to specific version

Returns:

  • (Internal::Manifest)


32
33
34
# File 'lib/active_validation/verifier.rb', line 32

def manifest
  @manifest
end

#manifest_name_formatterObject

Note:

Custom name formatter for Manifests



28
29
30
# File 'lib/active_validation/verifier.rb', line 28

def manifest_name_formatter
  @manifest_name_formatter
end

#observerInternal::Observers::Manifest (readonly)

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



38
39
40
# File 'lib/active_validation/verifier.rb', line 38

def observer
  @observer
end

#orm_adapterObject

Note:

Orm adapter for exactly this verifier instance



25
26
27
# File 'lib/active_validation/verifier.rb', line 25

def orm_adapter
  @orm_adapter
end

#validations_module_nameObject

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



19
20
21
# File 'lib/active_validation/verifier.rb', line 19

def validations_module_name
  @validations_module_name
end

Class Method Details

.registryRegistry

Shortcut for the global verifiers registry instance

Returns:



8
9
10
# File 'lib/active_validation/verifier.rb', line 8

def registry
  ActiveValidation.config.verifiers_registry
end

Instance Method Details

#add_manifest(**hash) ⇒ Internal::Manifest+

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

param [Hash]

Returns:

  • (Internal::Manifest, Array<Internal::Manifest>)


126
127
128
129
130
131
# 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_classClass

Returns:

  • (Class)


96
97
98
# File 'lib/active_validation/verifier.rb', line 96

def base_class
  base_klass.is_a?(Class) ? base_klass : base_klass.constantize
end

#current_manifestInternal::Manifest

Returns:

  • (Internal::Manifest)


91
92
93
# File 'lib/active_validation/verifier.rb', line 91

def current_manifest
  manifest or find_manifests.first
end

#descendants_with_active_validationArray

Return the list af parents with active_validation

Returns:

  • (Array)


102
103
104
105
106
107
108
109
110
# 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?Boolean

Returns:

  • (Boolean)


45
# File 'lib/active_validation/verifier.rb', line 45

def enabled?; !!enabled; end

#install!(*args) ⇒ Object

Install parent models and self

Return Symbol Installation status



115
116
117
118
# File 'lib/active_validation/verifier.rb', line 115

def install!(*args)
  descendants_with_active_validation.each { |klass| klass.active_validation.install }
  install(*args)
end

#mInternal::Manifest+

Forward the normalized request to ORM mapper

param [Hash]

Returns:

  • (Internal::Manifest, Array<Internal::Manifest>)


138
139
140
# File 'lib/active_validation/verifier.rb', line 138

%i[find_manifest find_manifests].each do |m|
  define_method(m) { |**hash| add_defaults_for_orm_adapter(hash) { |**h| orm_adapter.public_send m, h } }
end

#versionObject



79
80
81
# File 'lib/active_validation/verifier.rb', line 79

def version
  @version ||= versions.last
end

#version=(other) ⇒ Object



83
84
85
86
# 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

#versionsArray<Value::Version>

Returns Sorted list of versions.

Returns:

  • (Array<Value::Version>)

    Sorted list of versions.



71
72
73
74
75
76
# 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