Refactoring. Extracted AttributeValidator.
This commit is contained in:
parent
a02e0f8ea9
commit
41931b9de0
|
@ -1,5 +1,5 @@
|
|||
require 'lotus/utils/kernel'
|
||||
require 'lotus/validations/version'
|
||||
require 'lotus/validations/attribute_validator'
|
||||
|
||||
module Lotus
|
||||
module Validations
|
||||
|
@ -7,41 +7,6 @@ module Lotus
|
|||
base.extend ClassMethods
|
||||
end
|
||||
|
||||
attr_reader :errors
|
||||
|
||||
def initialize(attributes)
|
||||
@attributes = attributes
|
||||
@errors = Hash.new {|h,k| h[k] = [] }
|
||||
end
|
||||
|
||||
def valid?
|
||||
self.class.attributes.all? do |attribute, options|
|
||||
value = __send__(attribute)
|
||||
|
||||
if options[:presence]
|
||||
if value.nil?
|
||||
@errors[attribute].push :presence
|
||||
end
|
||||
end
|
||||
|
||||
if !value.nil?
|
||||
if format = options[:format]
|
||||
if !value.to_s.match(format)
|
||||
@errors[attribute].push :format
|
||||
end
|
||||
end
|
||||
|
||||
if coercer = options[:type]
|
||||
value = Lotus::Utils::Kernel.send(coercer.to_s, value)
|
||||
end
|
||||
|
||||
@attributes[attribute] = value
|
||||
end
|
||||
|
||||
@errors.empty?
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def attribute(name, options = {})
|
||||
attributes[name] = options
|
||||
|
@ -53,10 +18,30 @@ module Lotus
|
|||
}
|
||||
end
|
||||
|
||||
# FIXME make this private
|
||||
private
|
||||
def attributes
|
||||
@attributes ||= Hash.new
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :attributes, :errors
|
||||
|
||||
def initialize(attributes)
|
||||
@attributes = attributes
|
||||
@errors = Hash.new {|h,k| h[k] = [] }
|
||||
end
|
||||
|
||||
def valid?
|
||||
_attributes.each do |name, options|
|
||||
AttributeValidator.new(self, name, options).validate!
|
||||
end
|
||||
|
||||
@errors.empty?
|
||||
end
|
||||
|
||||
private
|
||||
def _attributes
|
||||
self.class.__send__(:attributes)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
require 'lotus/utils/kernel'
|
||||
|
||||
module Lotus
|
||||
module Validations
|
||||
class AttributeValidator
|
||||
def initialize(validator, name, options)
|
||||
@validator, @name, @options = validator, name, options
|
||||
|
||||
@errors = @validator.errors
|
||||
@value = @validator.__send__(@name)
|
||||
end
|
||||
|
||||
def validate!
|
||||
presence
|
||||
_run_validations
|
||||
end
|
||||
|
||||
private
|
||||
def skip?
|
||||
@value.nil?
|
||||
end
|
||||
|
||||
def _run_validations
|
||||
return if skip?
|
||||
|
||||
format
|
||||
coerce
|
||||
end
|
||||
|
||||
def presence
|
||||
if @options[:presence] && skip?
|
||||
@errors[@name].push(:presence)
|
||||
end
|
||||
end
|
||||
|
||||
def format
|
||||
if (matcher = @options[:format]) && !@value.to_s.match(matcher)
|
||||
@errors[@name].push(:format)
|
||||
end
|
||||
end
|
||||
|
||||
def coerce
|
||||
if coercer = @options[:type]
|
||||
@value = Lotus::Utils::Kernel.send(coercer.to_s, @value)
|
||||
@validator.attributes[@name] = @value
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue