lib/tram/policy.rb in tram-policy-0.0.1 vs lib/tram/policy.rb in tram-policy-0.0.2
- old
+ new
@@ -6,21 +6,22 @@
class Policy
require_relative "policy/validation_error"
require_relative "policy/inflector"
require_relative "policy/error"
require_relative "policy/errors"
+ require_relative "policy/validator"
extend Dry::Initializer
class << self
# Registers a validator
#
# @param [#to_sym, Array<#to_sym>] names
# @return [self]
#
- def validate(*names)
- @validators = validators | names.flatten.map(&:to_sym)
+ def validate(name, **opts)
+ validators[name.to_sym] = opts
self
end
# Policy constructor/validator (alias for [.new])
#
@@ -32,16 +33,16 @@
end
private
def validators
- @validators ||= []
+ @validators ||= {}
end
def inherited(klass)
super
- klass.validate validators
+ validators.each { |name, opts| klass.validate name, opts }
end
end
# Translates a message in the scope of current policy
#
@@ -105,9 +106,13 @@
private
def initialize(*)
super
@__scope__ = Inflector.underscore(self.class.name)
- self.class.send(:validators).each { |name| send(name) }
+ self.class.send(:validators).each do |name, opts|
+ size = errors.count
+ send(name)
+ break if (errors.count > size) && opts[:stop_on_failure]
+ end
end
end
end