Class: Aequitas::Violation

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

Direct Known Subclasses

Message, Rule

Defined Under Namespace

Classes: Message, Rule

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

- (Violation) initialize(resource, message, options = {})

Configure a Violation instance



56
57
58
59
# File 'lib/aequitas/violation.rb', line 56

def initialize(resource, message, options = {})
  @resource       = resource
  @custom_message = evaluate_message(message)
end

Instance Attribute Details

- (String, #call) custom_message (readonly)

Custom message for this Violation



35
36
37
# File 'lib/aequitas/violation.rb', line 35

def custom_message
  @custom_message
end

- (Object) resource (readonly)

Object that triggered this Violation



27
28
29
# File 'lib/aequitas/violation.rb', line 27

def resource
  @resource
end

- (Aequitas::Rule?) rule (readonly)

Rule which generated this Violation



44
45
46
# File 'lib/aequitas/violation.rb', line 44

def rule
  @rule
end

Class Method Details

+ (Object) default_transformer



13
14
15
# File 'lib/aequitas/violation.rb', line 13

def self.default_transformer
  @default_transformer ||= MessageTransformer.default
end

+ (Object) default_transformer=(transformer)



17
18
19
# File 'lib/aequitas/violation.rb', line 17

def self.default_transformer=(transformer)
  @default_transformer = transformer
end

Instance Method Details

- (Object) ==(other)

In general we want Aequitas::ValueObject-type equality/equivalence, but this allows direct equivalency test against Strings, which is handy



114
115
116
117
118
119
120
# File 'lib/aequitas/violation.rb', line 114

def ==(other)
  if other.respond_to?(:to_str)
    self.to_s == other.to_str
  else
    super
  end
end

- (Object) evaluate_message(message)

TODO: Drop this or heavily refactor it.

This is too complicated and coupled to DM.


99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/aequitas/violation.rb', line 99

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

- (Object) info

Raises:

  • (NotImplementedError)


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

def info
  raise NotImplementedError, "#{self.class}#info is not implemented"
end

- (Object) message(transformer = Undefined) Also known as: to_s



62
63
64
65
66
67
68
# File 'lib/aequitas/violation.rb', line 62

def message(transformer = Undefined)
  return @custom_message if @custom_message

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

  transformer.transform(self)
end

- (Object) transformer

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.



89
90
91
92
93
94
95
# File 'lib/aequitas/violation.rb', line 89

def transformer
  if resource.respond_to?(:validation_rules) && transformer = resource.validation_rules.transformer
    transformer
  else
    Violation.default_transformer
  end
end

- (Object) type

Raises:

  • (NotImplementedError)


74
75
76
# File 'lib/aequitas/violation.rb', line 74

def type
  raise NotImplementedError, "#{self.class}#type is not implemented"
end

- (Object) values

Raises:

  • (NotImplementedError)


84
85
86
# File 'lib/aequitas/violation.rb', line 84

def values
  raise NotImplementedError, "#{self.class}#values is not implemented"
end