Class: Tram::Policy
- Inherits:
-
Object
- Object
- Tram::Policy
- Extended by:
- Dry::Initializer
- Defined in:
- lib/tram/policy.rb,
lib/tram/policy/error.rb,
lib/tram/policy/errors.rb,
lib/tram/policy/generator.rb,
lib/tram/policy/inflector.rb,
lib/tram/policy/validator.rb,
lib/tram/policy/validation_error.rb
Overview
Base class for policy objects with composable validation errors
Defined Under Namespace
Modules: Inflector Classes: Error, Errors, Generator, ValidationError, Validator
Class Method Summary collapse
-
.[](*args) ⇒ Tram::Policy
Policy constructor/validator (alias for [.new]).
-
.all ⇒ Array<Proc>
List of all applicable validators from both the policy and its parent.
-
.local ⇒ Array<Proc>
List of validators defined by a policy per se.
-
.scope ⇒ Array<String>
Translation scope for a policy.
-
.validate(name, opts) ⇒ self
Registers a validator.
Instance Method Summary collapse
-
#errors ⇒ Tram::Policy::Errors
Collection of validation errors.
-
#inspect ⇒ String
Human-readable representation of the policy.
-
#invalid?(&filter) ⇒ Boolean
Checks whether the policy is invalid.
-
#t(message, options = {}) ⇒ String
Translates a message in the scope of current policy.
-
#valid?(&filter) ⇒ Boolean
Checks whether the policy is valid.
-
#validate!(&filter) ⇒ nil
Raises exception if the policy is not valid.
Class Method Details
.[](*args) ⇒ Tram::Policy
Policy constructor/validator (alias for [.new])
34 35 36 |
# File 'lib/tram/policy.rb', line 34 def [](*args) new(*args) end |
.all ⇒ Array<Proc>
List of all applicable validators from both the policy and its parent
58 59 60 |
# File 'lib/tram/policy.rb', line 58 def all (((self == Tram::Policy) ? [] : superclass.send(:all)) + local).uniq end |
.local ⇒ Array<Proc>
List of validators defined by a policy per se
50 51 52 |
# File 'lib/tram/policy.rb', line 50 def local @local ||= [] end |
.scope ⇒ Array<String>
Translation scope for a policy
42 43 44 |
# File 'lib/tram/policy.rb', line 42 def scope @scope ||= ["tram-policy", *Inflector.underscore(name)] end |
.validate(name, opts) ⇒ self
Registers a validator
24 25 26 27 |
# File 'lib/tram/policy.rb', line 24 def validate(name = nil, **opts, &block) local << Validator.new(scope, name, block, opts) self end |
Instance Method Details
#errors ⇒ Tram::Policy::Errors
Collection of validation errors
78 79 80 |
# File 'lib/tram/policy.rb', line 78 def errors @errors ||= Errors.new(self) end |
#inspect ⇒ String
Human-readable representation of the policy
118 119 120 |
# File 'lib/tram/policy.rb', line 118 def inspect "#<#{self.class.name}[#{@__options__}]>" end |
#invalid?(&filter) ⇒ Boolean
Checks whether the policy is invalid
96 97 98 |
# File 'lib/tram/policy.rb', line 96 def invalid?(&filter) filter ? errors.any?(&filter) : errors.any? end |
#t(message, options = {}) ⇒ String
Translates a message in the scope of current policy
69 70 71 72 |
# File 'lib/tram/policy.rb', line 69 def t(, = {}) return .to_s unless .is_a? Symbol I18n.t , .merge(scope: @__scope__) end |
#valid?(&filter) ⇒ Boolean
Checks whether the policy is valid
87 88 89 |
# File 'lib/tram/policy.rb', line 87 def valid?(&filter) filter ? errors.reject(&filter).empty? : errors.empty? end |
#validate!(&filter) ⇒ nil
Raises exception if the policy is not valid
106 107 108 |
# File 'lib/tram/policy.rb', line 106 def validate!(&filter) raise ValidationError.new(self, filter) unless valid?(&filter) end |