Class: Bluepine::Validators::Proxy
- Inherits:
-
Object
- Object
- Bluepine::Validators::Proxy
- Includes:
- ActiveModel::Validations, Assertions
- Defined in:
- lib/bluepine/validators/proxy.rb
Overview
Proxy will act as a wrapper for a pair of attribute and value. It internally creates anonymous model with single attribute. This will simplify validation process for nested attributes (let’s Visitor handles traversal instead).
Constant Summary
Constants included from Assertions
Assertions::Error, Assertions::KeyError, Assertions::SubsetError
Instance Attribute Summary collapse
-
#validators ⇒ Object
readonly
Returns the value of attribute validators.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.model_name ⇒ Object
rails requires this for anonymous model.
Instance Method Summary collapse
-
#initialize(attribute, value = nil, options = {}) ⇒ Proxy
constructor
A new instance of Proxy.
- #messages ⇒ Object
-
#register(validators) ⇒ Object
Register validators to model.
- #valid? ⇒ Boolean
Methods included from Assertions
#assert, #assert_in, #assert_kind_of, #assert_not, #assert_subset_of, included
Constructor Details
#initialize(attribute, value = nil, options = {}) ⇒ Proxy
Returns a new instance of Proxy.
27 28 29 30 31 32 |
# File 'lib/bluepine/validators/proxy.rb', line 27 def initialize(attribute, value = nil, = {}) @attribute = attribute @value = attribute.value(value) @params = { attribute.name.to_sym => @value } @context = [:context] || {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object (private)
Delegates method call to hash accessor e.g. ‘a.name` will become `a` and return `nil` for all undefined attributes e.g. `a.non_exists` => `nil`
64 65 66 |
# File 'lib/bluepine/validators/proxy.rb', line 64 def method_missing(m, *args, &block) normalize_missing_value m end |
Instance Attribute Details
#validators ⇒ Object (readonly)
Returns the value of attribute validators.
20 21 22 |
# File 'lib/bluepine/validators/proxy.rb', line 20 def validators @validators end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
20 21 22 |
# File 'lib/bluepine/validators/proxy.rb', line 20 def value @value end |
Class Method Details
.model_name ⇒ Object
rails requires this for anonymous model
23 24 25 |
# File 'lib/bluepine/validators/proxy.rb', line 23 def self.model_name ActiveModel::Name.new(self, nil, name) end |
Instance Method Details
#messages ⇒ Object
56 57 58 |
# File 'lib/bluepine/validators/proxy.rb', line 56 def errors..values.flatten end |
#register(validators) ⇒ Object
Register validators to model
register(presense: true, ..., validators: [Validator1, Validator2, ...])
46 47 48 49 50 51 52 53 54 |
# File 'lib/bluepine/validators/proxy.rb', line 46 def register(validators) customs = validators.delete(:validators) || [] # register custom validators (requires :attributes) self.class.validates_with(*customs, attributes: [@attribute.name]) if customs.any? # register ActiveModel's validations e.g. presence: true self.class.validates(@attribute.name, validators) if validators.any? end |
#valid? ⇒ Boolean
34 35 36 37 38 39 40 41 |
# File 'lib/bluepine/validators/proxy.rb', line 34 def valid? # clear validators self.class.clear_validators! register(@attribute.validators.dup) super end |