lib/attestor/validations/validator.rb in attestor-0.2.0 vs lib/attestor/validations/validator.rb in attestor-0.3.0
- old
+ new
@@ -5,11 +5,11 @@
module Validations
# Describe a validator for class instances
#
# @example
- # validator = Validator.new(:foo, policy: :bar, only: :baz)
+ # validator = Validator.new(:foo, only: :baz)
#
# validator.used_in_context? :baz # => true
# validator.validate object
#
# @api private
@@ -24,33 +24,25 @@
# @option [#to_sym, Array<#to_sym>] :only
#
# @return [Attestor::Validations::Validator]
# @private
- def initialize(name, except: nil, only: nil, policy: nil)
+ def initialize(name, except: nil, only: nil, &block)
@name = name.to_sym
- @policy = policy
@whitelist = normalize(only)
@blacklist = normalize(except)
+ @block = block
generate_id
freeze
end
# @!attribute [r] name
# The name of the item
#
# @return [Symbol]
attr_reader :name
- # @!method policy?
- # Whether the validator uses a policy
- #
- # @return [Boolean]
- def policy?
- @policy ? true : false
- end
-
# Compares an item to another one
#
# @param [Object] other
#
# @return [Boolean]
@@ -75,12 +67,11 @@
# @raise [Attestor::InvalidError]
# if object doesn't match validation rule
#
# @return [undefined]
def validate(object)
- result = object.__send__(name)
- object.__send__(:invalid, name) if policy? && result.invalid?
+ block ? object.instance_eval(&block) : object.__send__(name)
end
protected
# @!attribute [r] id
@@ -89,10 +80,10 @@
# @return [String]
attr_reader :id
private
- attr_reader :whitelist, :blacklist
+ attr_reader :whitelist, :blacklist, :block
def whitelisted?(symbol)
whitelist.empty? || whitelist.include?(symbol)
end