Class: Aequitas::Violation
- Inherits:
-
Object
- Object
- Aequitas::Violation
- Extended by:
- ValueObject
- Defined in:
- lib/aequitas/violation.rb
Instance Attribute Summary collapse
- #attribute_name ⇒ Object readonly
-
#custom_message ⇒ Object
readonly
Returns the value of attribute custom_message.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#rule ⇒ Object
readonly
Returns the value of attribute rule.
Attributes included from ValueObject
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
In general we want Aequitas::ValueObject-type equality/equivalence, but this allows direct equivalency test against Strings, which is handy.
- #evaluate_message(message) ⇒ Object
- #info ⇒ Object
-
#initialize(resource, message = nil, rule = nil, attribute_name = nil) ⇒ Violation
constructor
Configure a Violation instance.
- #message(transformer = Undefined) ⇒ Object (also: #to_s)
- #transformer ⇒ Object
- #type ⇒ Object
- #values ⇒ Object
Methods included from ValueObject
Constructor Details
#initialize(resource, message = nil, rule = nil, attribute_name = nil) ⇒ Violation
Configure a Violation instance
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/aequitas/violation.rb', line 38 def initialize(resource, = nil, rule = nil, attribute_name = nil) unless || rule raise ArgumentError, "expected +message+ or +rule+" end @resource = resource @rule = rule @attribute_name = attribute_name @custom_message = () end |
Instance Attribute Details
#attribute_name ⇒ Object (readonly)
62 63 64 |
# File 'lib/aequitas/violation.rb', line 62 def attribute_name @attribute_name end |
#custom_message ⇒ Object (readonly)
Returns the value of attribute custom_message.
22 23 24 |
# File 'lib/aequitas/violation.rb', line 22 def @custom_message end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
21 22 23 |
# File 'lib/aequitas/violation.rb', line 21 def resource @resource end |
#rule ⇒ Object (readonly)
Returns the value of attribute rule.
23 24 25 |
# File 'lib/aequitas/violation.rb', line 23 def rule @rule end |
Class Method Details
.default_transformer ⇒ Object
13 14 15 |
# File 'lib/aequitas/violation.rb', line 13 def self.default_transformer @default_transformer ||= MessageTransformer.default end |
.default_transformer=(transformer) ⇒ Object
17 18 19 |
# File 'lib/aequitas/violation.rb', line 17 def self.default_transformer=(transformer) @default_transformer = transformer end |
Instance Method Details
#==(other) ⇒ Object
In general we want Aequitas::ValueObject-type equality/equivalence, but this allows direct equivalency test against Strings, which is handy
107 108 109 110 111 112 113 |
# File 'lib/aequitas/violation.rb', line 107 def ==(other) if other.respond_to?(:to_str) self.to_s == other.to_str else super end end |
#evaluate_message(message) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/aequitas/violation.rb', line 92 def () if .respond_to?(:call) if resource.respond_to?(:model) && resource.model.respond_to?(:properties) property = resource.model.properties[attribute_name] .call(resource, property) else .call(resource) end else end end |
#info ⇒ Object
76 77 78 |
# File 'lib/aequitas/violation.rb', line 76 def info rule ? rule.violation_info(resource) : { } end |
#message(transformer = Undefined) ⇒ Object Also known as: to_s
50 51 52 53 54 55 56 |
# File 'lib/aequitas/violation.rb', line 50 def (transformer = Undefined) return @custom_message if @custom_message transformer = self.transformer if Undefined.equal?(transformer) transformer.transform(self) end |
#transformer ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/aequitas/violation.rb', line 84 def transformer if resource.respond_to?(:validation_rules) && transformer = resource.validation_rules.transformer transformer else Violation.default_transformer end end |
#type ⇒ Object
71 72 73 |
# File 'lib/aequitas/violation.rb', line 71 def type rule ? rule.violation_type(resource) : nil end |
#values ⇒ Object
80 81 82 |
# File 'lib/aequitas/violation.rb', line 80 def values rule ? rule.violation_values(resource) : [ ] end |