Class: Aequitas::ViolationSet
- Inherits:
-
Object
- Object
- Aequitas::ViolationSet
- Includes:
- Enumerable
- Defined in:
- lib/aequitas/violation_set.rb
Instance Attribute Summary collapse
- #resource ⇒ Object readonly private
Instance Method Summary collapse
- #<<(violation) ⇒ Object
-
#[](attribute_name) ⇒ Object
FIXME: calling #to_sym on uncontrolled input is an invitation for a memory leak.
-
#add(attribute_name_or_violation, message = nil) ⇒ Object
Add a validation error.
-
#clear ⇒ Object
Clear existing validation violations.
- #concat(other) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#full_messages ⇒ Object
Collect all violations into a single list.
-
#initialize(resource) ⇒ ViolationSet
constructor
TODO: replace OrderedHash with OrderedSet and remove vendor’d OrderedHash.
- #method_missing(meth, *args, &block) ⇒ Object
-
#on(attribute_name) ⇒ Array(Violation, String)
Return validation violations for a particular attribute_name.
- #respond_to?(method) ⇒ Boolean
Constructor Details
#initialize(resource) ⇒ ViolationSet
TODO: replace OrderedHash with OrderedSet and remove vendor’d OrderedHash
21 22 23 24 |
# File 'lib/aequitas/violation_set.rb', line 21 def initialize(resource) @resource = resource @violations = OrderedHash.new { |h,k| h[k] = [] } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
113 114 115 |
# File 'lib/aequitas/violation_set.rb', line 113 def method_missing(meth, *args, &block) violations.send(meth, *args, &block) end |
Instance Attribute Details
#resource ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
13 14 15 |
# File 'lib/aequitas/violation_set.rb', line 13 def resource @resource end |
Instance Method Details
#<<(violation) ⇒ Object
57 58 59 60 |
# File 'lib/aequitas/violation_set.rb', line 57 def <<(violation) violations[violation.attribute_name] << violation self end |
#[](attribute_name) ⇒ Object
FIXME: calling #to_sym on uncontrolled input is an invitation for a memory leak
109 110 111 |
# File 'lib/aequitas/violation_set.rb', line 109 def [](attribute_name) violations[attribute_name.to_sym] end |
#add(attribute_name_or_violation, message = nil) ⇒ Object
Add a validation error. Use the attribute_name :general if the violations does not apply to a specific field of the Resource.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/aequitas/violation_set.rb', line 45 def add(attribute_name_or_violation, = nil) violation = if attribute_name_or_violation.kind_of?(Violation) attribute_name_or_violation else # TODO: Violation.from_message(resource, message, attribute_name_or_violation) Violation.new(resource, , nil, attribute_name_or_violation) end self << violation end |
#clear ⇒ Object
Clear existing validation violations.
29 30 31 |
# File 'lib/aequitas/violation_set.rb', line 29 def clear violations.clear end |
#concat(other) ⇒ Object
62 63 64 |
# File 'lib/aequitas/violation_set.rb', line 62 def concat(other) other.each { |violation| self << violation } end |
#each ⇒ Object
94 95 96 97 98 |
# File 'lib/aequitas/violation_set.rb', line 94 def each violations.each_value do |v| yield(v) unless Aequitas.blank?(v) end end |
#empty? ⇒ Boolean
101 102 103 |
# File 'lib/aequitas/violation_set.rb', line 101 def empty? violations.all? { |_, violations| violations.empty? } end |
#full_messages ⇒ Object
Collect all violations into a single list.
69 70 71 72 73 74 75 |
# File 'lib/aequitas/violation_set.rb', line 69 def violations.inject([]) do |list, (attribute_name, violations)| = violations = violations. if violations.respond_to?(:full_messages) list.concat() end end |
#on(attribute_name) ⇒ Array(Violation, String)
Return validation violations for a particular attribute_name.
TODO: use a data structure that ensures uniqueness
89 90 91 |
# File 'lib/aequitas/violation_set.rb', line 89 def on(attribute_name) violations[attribute_name].uniq end |
#respond_to?(method) ⇒ Boolean
117 118 119 |
# File 'lib/aequitas/violation_set.rb', line 117 def respond_to?(method) super || violations.respond_to?(method) end |