Class: Aequitas::ViolationSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/aequitas/violation_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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



114
115
116
# File 'lib/aequitas/violation_set.rb', line 114

def method_missing(meth, *args, &block)
  violations.send(meth, *args, &block)
end

Instance Attribute Details

#resourceObject (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



110
111
112
# File 'lib/aequitas/violation_set.rb', line 110

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.

Parameters:

  • attribute_name_or_violation (Symbol, Violation)

    The name of the field that caused the violation, or the Violation which describes the validation violation

  • message (NilClass, String, #call, Hash) (defaults to: nil)

    The message to add.

See Also:



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, message = 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, message, nil, attribute_name_or_violation)
    end

  self << violation
end

#clearObject

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

#eachObject



95
96
97
98
99
# File 'lib/aequitas/violation_set.rb', line 95

def each
  violations.each_value do |v|
    yield(v) unless Aequitas.blank?(v)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/aequitas/violation_set.rb', line 102

def empty?
  violations.all? { |_, violations| violations.empty? }
end

#full_messagesObject

Collect all violations into a single list.



69
70
71
72
73
74
75
# File 'lib/aequitas/violation_set.rb', line 69

def full_messages
  violations.inject([]) do |list, (attribute_name, violations)|
    messages = violations
    messages = violations.full_messages if violations.respond_to?(:full_messages)
    list.concat(messages)
  end
end

#on(attribute_name) ⇒ Array(Violation, String), NilClass

Return validation violations for a particular attribute_name.

TODO: use a data structure that ensures uniqueness

Parameters:

  • attribute_name (Symbol)

    The name of the field you want an violation for.

Returns:

  • (Array(Violation, String), NilClass)

    Array of Violations, if there are violations on attribute_name nil if there are no violations on attribute_name



89
90
91
92
# File 'lib/aequitas/violation_set.rb', line 89

def on(attribute_name)
  attribute_violations = violations[attribute_name]
  attribute_violations.empty? ? nil : attribute_violations.uniq
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/aequitas/violation_set.rb', line 118

def respond_to?(method)
  super || violations.respond_to?(method)
end