Class: ActiveValidation::Internal::Models::Manifest::Installer
- Inherits:
-
Object
- Object
- ActiveValidation::Internal::Models::Manifest::Installer
- Defined in:
- lib/active_validation/internal/models/manifest/installer.rb
Instance Attribute Summary collapse
-
#base_class ⇒ Object
readonly
Returns the value of attribute base_class.
-
#checks ⇒ Object
readonly
Returns the value of attribute checks.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#installed_callbacks ⇒ Object
readonly
Returns the value of attribute installed_callbacks.
Instance Method Summary collapse
- #callbacks_chain ⇒ Object
-
#initialize(base_class:, checks: [], context:) ⇒ Installer
constructor
A new instance of Installer.
-
#install ⇒ TrueClass
Add all checks (validations) to base_class.
-
#uninstall ⇒ FalseClass
Remove all checks (validations) from base_class.
Constructor Details
#initialize(base_class:, checks: [], context:) ⇒ Installer
Returns a new instance of Installer.
12 13 14 15 16 17 |
# File 'lib/active_validation/internal/models/manifest/installer.rb', line 12 def initialize(base_class:, checks: [], context:) @base_class = base_class @checks = checks @context = context @installed_callbacks = [] end |
Instance Attribute Details
#base_class ⇒ Object (readonly)
Returns the value of attribute base_class.
10 11 12 |
# File 'lib/active_validation/internal/models/manifest/installer.rb', line 10 def base_class @base_class end |
#checks ⇒ Object (readonly)
Returns the value of attribute checks.
10 11 12 |
# File 'lib/active_validation/internal/models/manifest/installer.rb', line 10 def checks @checks end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
10 11 12 |
# File 'lib/active_validation/internal/models/manifest/installer.rb', line 10 def context @context end |
#installed_callbacks ⇒ Object (readonly)
Returns the value of attribute installed_callbacks.
10 11 12 |
# File 'lib/active_validation/internal/models/manifest/installer.rb', line 10 def installed_callbacks @installed_callbacks end |
Instance Method Details
#callbacks_chain ⇒ Object
51 52 53 |
# File 'lib/active_validation/internal/models/manifest/installer.rb', line 51 def callbacks_chain base_class._validate_callbacks end |
#install ⇒ TrueClass
we have to use the hack with
Add all checks (validations) to base_class
‘callbacks_chain.each.to_a # => chain` since `chain` method is protected.
26 27 28 29 30 31 32 33 |
# File 'lib/active_validation/internal/models/manifest/installer.rb', line 26 def install @installed_callbacks = chain_mutex.synchronize do before = callbacks_chain_elements checks.each { |check| base_class.public_send(*check.to_validation_arguments(context: context)) } callbacks_chain_elements - before end true end |
#uninstall ⇒ FalseClass
Remove all checks (validations) from base_class
we can not use ActiveSupport provided methods, since we already have our installed callbacks.
41 42 43 44 45 46 47 48 49 |
# File 'lib/active_validation/internal/models/manifest/installer.rb', line 41 def uninstall installed_validators = installed_callbacks.map(&:filter).select { |f| f.is_a? ActiveModel::Validator } ([base_class] + ActiveSupport::DescendantsTracker.descendants(base_class)).reverse_each do |target| uninstall! target: target, installed_validators: installed_validators end installed_callbacks.clear false end |