Class: Tram::Policy::Error

Inherits:
Object
  • Object
show all
Defined in:
lib/tram/policy/error.rb

Overview

Validation error with message and assigned tags

Notice: an error is context-independent; it knows nothing about

a collection it is placed to; it can be safely moved
from one collection of [Tram::Policy::Errors] to another.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



85
86
87
# File 'lib/tram/policy/error.rb', line 85

def method_missing(name, *args, &block)
  args.any? || block ? super : @tags[name]
end

Instance Attribute Details

#messageString (readonly)

Returns The error message text.

Returns:

  • (String)

    The error message text



27
28
29
# File 'lib/tram/policy/error.rb', line 27

def message
  @message
end

Class Method Details

.new(value, opts) ⇒ Tram::Policy::Error

Builds an error

If another error is send to the constructor, the error returned unchanged

Parameters:

Returns:



18
19
20
21
# File 'lib/tram/policy/error.rb', line 18

def self.new(value, **opts)
  return value if value.is_a? self
  super
end

Instance Method Details

#==(other) ⇒ Boolean

Compares an error to another object using method [#to_h]

Parameters:

  • other (Object)

    Other object to compare to

Returns:

  • (Boolean)


70
71
72
# File 'lib/tram/policy/error.rb', line 70

def ==(other)
  other.respond_to?(:to_h) && other.to_h == to_h
end

#[](tag) ⇒ Object

Fetches either message or a tag

Parameters:

  • tag (#to_sym)

Returns:

  • (Object)


50
51
52
# File 'lib/tram/policy/error.rb', line 50

def [](tag)
  to_h[tag.to_sym]
end

#fetch(tag, default, &block) ⇒ Object

Fetches either message or a tag

Parameters:

  • tag (#to_sym)
  • default (Object)
  • block (Proc)

Returns:

  • (Object)


61
62
63
# File 'lib/tram/policy/error.rb', line 61

def fetch(tag, default, &block)
  to_h.fetch(tag.to_sym, default, &block)
end

#full_messageString

The full message (message and tags info)

Returns:

  • (String)


33
34
35
# File 'lib/tram/policy/error.rb', line 33

def full_message
  [message, @tags].reject(&:empty?).join(" ")
end

#to_hHash<Symbol, Object>

Converts the error to a simple hash with message and tags

Returns:

  • (Hash<Symbol, Object>)


41
42
43
# File 'lib/tram/policy/error.rb', line 41

def to_h
  @tags.merge(message: message)
end