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



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

#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



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.

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



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

Returns:

  • (Boolean)


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

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)

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))

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



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

def on(attribute_name)
  violations[attribute_name].uniq
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

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