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)



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

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



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

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:



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

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)


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

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)


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

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)


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

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)


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

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


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

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