Class: Metaractor::Errors::Error

Inherits:
Object
  • Object
show all
Defined in:
lib/metaractor/errors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value:, object: nil) ⇒ Error

Returns a new instance of Error.



10
11
12
13
# File 'lib/metaractor/errors.rb', line 10

def initialize(value:, object: nil)
  @value = value
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



8
9
10
# File 'lib/metaractor/errors.rb', line 8

def object
  @object
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/metaractor/errors.rb', line 8

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



48
49
50
51
52
53
54
# File 'lib/metaractor/errors.rb', line 48

def ==(other)
  @value == if other.is_a?(self.class)
    other.value
  else
    other
  end
end

#generate_message(path_elements:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/metaractor/errors.rb', line 15

def generate_message(path_elements:)
  if @value.is_a? Symbol
    defaults = []

    if object.class.respond_to?(:i18n_parent_names) &&
        !object.class.i18n_parent_names.empty?

      names = object.class.i18n_parent_names
      until names.empty?
        defaults << ["errors", names.join("."), "parameters", path_elements.join("."), @value.to_s].reject do |item|
          item.nil? || item == ""
        end.join(".").to_sym
        names.pop
      end
    end

    unless path_elements.empty?
      defaults << :"errors.parameters.#{path_elements.join(".")}.#{@value}"
    end
    defaults << :"errors.parameters.#{@value}"

    key = defaults.shift
    I18n.translate(
      key,
      default: defaults,
      error_key: @value,
      parameter: path_elements.last
    )
  else
    "#{path_elements.join(".")} #{@value}".lstrip
  end
end

#hashObject



57
58
59
# File 'lib/metaractor/errors.rb', line 57

def hash
  @value.hash
end

#inspectObject



61
62
63
# File 'lib/metaractor/errors.rb', line 61

def inspect
  "(Error) #{@value.inspect}"
end