Class: Aequitas::Violation

Inherits:
Object
  • Object
show all
Extended by:
ValueObject
Defined in:
lib/aequitas/violation.rb

Instance Attribute Summary collapse

Attributes included from ValueObject

#equalizer

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ValueObject

equalize_on

Constructor Details

#initialize(resource, message = nil, rule = nil, attribute_name = nil) ⇒ Violation

Configure a Violation instance

Parameters:

  • resource (Object)

    the validated object

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

    an optional custom message for this Violation

  • rule (Rule) (defaults to: nil)

    the Rule whose violation triggered the creation of the receiver

  • attribute_name (Symbol) (defaults to: nil)

    the name of the attribute whose validation rule was violated or nil, if a Rule was provided.



38
39
40
41
42
43
44
45
46
47
# File 'lib/aequitas/violation.rb', line 38

def initialize(resource, message = nil, rule = nil, attribute_name = nil)
  unless message || rule
    raise ArgumentError, "expected +message+ or +rule+"
  end

  @resource       = resource
  @rule           = rule
  @attribute_name = attribute_name
  @custom_message = evaluate_message(message)
end

Instance Attribute Details

#attribute_nameObject (readonly)



62
63
64
# File 'lib/aequitas/violation.rb', line 62

def attribute_name
  @attribute_name
end

#custom_messageObject (readonly)

Returns the value of attribute custom_message.



22
23
24
# File 'lib/aequitas/violation.rb', line 22

def custom_message
  @custom_message
end

#resourceObject (readonly)

Returns the value of attribute resource.



21
22
23
# File 'lib/aequitas/violation.rb', line 21

def resource
  @resource
end

#ruleObject (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_transformerObject



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 evaluate_message(message)
  if message.respond_to?(:call)
    if resource.respond_to?(:model) && resource.model.respond_to?(:properties)
      property = resource.model.properties[attribute_name]
      message.call(resource, property)
    else
      message.call(resource)
    end
  else
    message
  end
end

#infoObject



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 message(transformer = Undefined)
  return @custom_message if @custom_message

  transformer = self.transformer if Undefined.equal?(transformer)

  transformer.transform(self)
end

#transformerObject



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

#typeObject



71
72
73
# File 'lib/aequitas/violation.rb', line 71

def type
  rule ? rule.violation_type(resource) : nil
end

#valuesObject



80
81
82
# File 'lib/aequitas/violation.rb', line 80

def values
  rule ? rule.violation_values(resource) : [ ]
end