Class: Bluepine::Validators::Proxy

Inherits:
Object
  • Object
show all
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).

Examples:

attribute = StringAttribute.new(:username)
value     = "john"

proxy = Proxy.new(attribute, value)
proxy.username # => "john"
proxy.valid?   # => true|false
proxy.errors

Constant Summary

Constants included from Assertions

Assertions::Error, Assertions::KeyError, Assertions::SubsetError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @attribute = attribute
  @value     = attribute.value(value)
  @params    = { attribute.name.to_sym => @value }
  @context   = options[: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

#validatorsObject (readonly)

Returns the value of attribute validators.



20
21
22
# File 'lib/bluepine/validators/proxy.rb', line 20

def validators
  @validators
end

#valueObject (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_nameObject

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

#messagesObject



56
57
58
# File 'lib/bluepine/validators/proxy.rb', line 56

def messages
  errors.messages.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

Returns:

  • (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