Class: Tram::Policy
- Inherits:
-
Object
- Object
- Tram::Policy
- Extended by:
- Dry::Initializer, DSL
- Defined in:
- lib/tram/policy.rb,
lib/tram/policy/dsl.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: DSL, Inflector Classes: Error, Errors, Generator, ValidationError, Validator
Instance Method Summary collapse
-
#errors ⇒ Tram::Policy::Errors
The collection of validation errors.
-
#inspect ⇒ String
Human-readable representation of the policy.
-
#invalid?(&filter) ⇒ Boolean
Checks whether the policy is invalid.
-
#items ⇒ Array<Array>
The array of error items for lazy translation.
-
#messages ⇒ Array<String>
The array of error messages translated for the current locale.
-
#scope ⇒ Array<String>
The scope used for translating error messages.
-
#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.
Methods included from DSL
[], local_validators, root_scope, validate, validators
Instance Method Details
#errors ⇒ Tram::Policy::Errors
The collection of validation errors
42 43 44 |
# File 'lib/tram/policy.rb', line 42 def errors @errors ||= Errors.new(self) end |
#inspect ⇒ String
Human-readable representation of the policy
98 99 100 |
# File 'lib/tram/policy.rb', line 98 def inspect "#<#{self.class.name}[#{__attributes__}]>" end |
#invalid?(&filter) ⇒ Boolean
Checks whether the policy is invalid
76 77 78 |
# File 'lib/tram/policy.rb', line 76 def invalid?(&filter) filter ? errors.any?(&filter) : errors.any? end |
#items ⇒ Array<Array>
The array of error items for lazy translation
50 51 52 |
# File 'lib/tram/policy.rb', line 50 def items errors.items end |
#messages ⇒ Array<String>
The array of error messages translated for the current locale
58 59 60 |
# File 'lib/tram/policy.rb', line 58 def errors. end |
#scope ⇒ Array<String>
The scope used for translating error messages
22 23 24 |
# File 'lib/tram/policy.rb', line 22 def scope Array self.class.scope end |
#t(message, options) ⇒ String
Translates a message in the scope of current policy
33 34 35 36 |
# File 'lib/tram/policy.rb', line 33 def t(, **) return .to_s unless .is_a? Symbol I18n.t , scope: scope, ** end |
#valid?(&filter) ⇒ Boolean
Checks whether the policy is valid
67 68 69 |
# File 'lib/tram/policy.rb', line 67 def valid?(&filter) filter ? errors.reject(&filter).empty? : errors.empty? end |
#validate!(&filter) ⇒ nil
Raises exception if the policy is not valid
86 87 88 |
# File 'lib/tram/policy.rb', line 86 def validate!(&filter) raise ValidationError.new(self, filter) unless valid?(&filter) end |