Class: ActiveValidation::Verifier
- Inherits:
-
Object
- Object
- ActiveValidation::Verifier
- Defined in:
- lib/active_validation/verifier.rb
Instance Attribute Summary collapse
- #base_klass ⇒ Object readonly
-
#enabled ⇒ TrueClass, FalseClass
Main switch for this class.
-
#failed_attempt_retry_time ⇒ Object
If something go wrong or there is no any Manifests for selected class yet, to prevent DB flooding we just until the situation resolves.
-
#manifest ⇒ Internal::Manifest
Stick manifest to specific version.
- #manifest_name_formatter ⇒ Object
-
#observer ⇒ Internal::Observers::Manifest
readonly
Contains Internal::Observers::Manifest, which store everything about used manifests.
- #orm_adapter ⇒ Object
-
#validations_module_name ⇒ Object
Name of the folder, where all validations method should be scoped.
Manual version lock collapse
Class Method Summary collapse
-
.registry ⇒ Registry
Shortcut for the global verifiers registry instance.
Instance Method Summary collapse
-
#add_manifest(**hash) ⇒ Internal::Manifest+
Forward the normalized request to ORM mapper For the name field we calculate default value.
- #base_class ⇒ Class
- #current_manifest ⇒ Internal::Manifest
-
#descendants_with_active_validation ⇒ Array
Return the list af parents with active_validation.
- #enabled? ⇒ Boolean
-
#initialize(base_klass) {|_self| ... } ⇒ Verifier
constructor
A new instance of Verifier.
-
#install!(*args) ⇒ Object
Install parent models and self.
-
#m ⇒ Internal::Manifest+
Forward the normalized request to ORM mapper.
-
#versions ⇒ Array<Value::Version>
Sorted list of versions.
Constructor Details
#initialize(base_klass) {|_self| ... } ⇒ Verifier
Returns a new instance of Verifier.
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_klass ⇒ Object (readonly)
The corresponding model class
22 23 24 |
# File 'lib/active_validation/verifier.rb', line 22 def base_klass @base_klass end |
#enabled ⇒ TrueClass, FalseClass
Main switch for this class. After switch, all installed validators remain in place, while new installations will be turned off.
44 45 46 |
# File 'lib/active_validation/verifier.rb', line 44 def enabled @enabled end |
#failed_attempt_retry_time ⇒ Object
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 |
#manifest ⇒ Internal::Manifest
Stick manifest to specific version
32 33 34 |
# File 'lib/active_validation/verifier.rb', line 32 def manifest @manifest end |
#manifest_name_formatter ⇒ Object
Custom name formatter for Manifests
28 29 30 |
# File 'lib/active_validation/verifier.rb', line 28 def manifest_name_formatter @manifest_name_formatter end |
#observer ⇒ Internal::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_adapter ⇒ Object
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_name ⇒ Object
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
.registry ⇒ Registry
Shortcut for the global verifiers registry instance
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]
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_class ⇒ 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_manifest ⇒ 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_validation ⇒ Array
Return the list af parents with active_validation
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
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 |
#m ⇒ Internal::Manifest+
Forward the normalized request to ORM mapper
param [Hash]
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 |
#version ⇒ Object
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 |
#versions ⇒ Array<Value::Version>
Returns 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 |