lib/tram/policy.rb in tram-policy-0.2.0 vs lib/tram/policy.rb in tram-policy-0.2.1
- old
+ new
@@ -1,8 +1,9 @@
require "dry-initializer"
require "i18n"
+# Top-level scope for Tram collection of gems
module Tram
# Base class for policy objects with composable validation errors
class Policy
require_relative "policy/validation_error"
require_relative "policy/inflector"
@@ -11,13 +12,14 @@
require_relative "policy/validator"
extend Dry::Initializer
class << self
+ # @!method validate(name, opts)
# Registers a validator
#
- # @param [#to_sym, Array<#to_sym>] names
+ # @param [#to_sym, nil] name (nil)
# @option opts [Boolean] :stop_on_failure
# @return [self]
#
def validate(name = nil, **opts, &block)
local << Validator.new(scope, name, block, opts)
@@ -42,21 +44,21 @@
def local
@local ||= []
end
def all
- ((self == Tram::Policy) ? [] : superclass.send(:all)) + local
+ (((self == Tram::Policy) ? [] : superclass.send(:all)) + local).uniq
end
end
# Translates a message in the scope of current policy
#
# @param [#to_s] message
# @param [Hash<Symbol, Object>] options
# @return [String]
#
- def t(message, **options)
+ def t(message, options = {})
return message.to_s unless message.is_a? Symbol
I18n.t message, options.merge(scope: @__scope__)
end
# Collection of validation errors
@@ -109,9 +111,12 @@
private
def initialize(*)
super
+
+ @__options__ = self.class.dry_initializer.attributes(self)
+
self.class.send(:all).each do |validator|
size = errors.count
validator.check(self)
break if (errors.count > size) && validator.stop_on_failure
end