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)



104
105
106
# File 'lib/tram/policy/error.rb', line 104

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

Instance Attribute Details

#keySymbol, String (readonly)

Returns error key.

Returns:

  • (Symbol, String)

    error key



24
25
26
# File 'lib/tram/policy/error.rb', line 24

def key
  @key
end

#tagsHash<Symbol, Object> (readonly)

Returns error tags.

Returns:

  • (Hash<Symbol, Object>)

    error tags



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

def tags
  @tags
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
# File 'lib/tram/policy/error.rb', line 18

def self.new(value, **opts)
  value.instance_of?(self) ? value : super
end

Instance Method Details

#==(other) ⇒ Boolean

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

Parameters:

  • other (Object)

    Other object to compare to

Returns:

  • (Boolean)


76
77
78
# File 'lib/tram/policy/error.rb', line 76

def ==(other)
  other.respond_to?(:to_a) && other.to_a == item
end

#[](tag) ⇒ Object

Fetches an option

Parameters:

  • tag (#to_sym)

Returns:

  • (Object)


52
53
54
# File 'lib/tram/policy/error.rb', line 52

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

#contain?(some_key = nil, some_tags = {}) ⇒ Boolean

Checks whether the error contain given key and tags

Parameters:

  • some_key (Object) (defaults to: nil)

    Expected key of the error

  • some_tags (Hash<Symbol, Object>) (defaults to: {})

    Expected tags of the error

Returns:

  • (Boolean)


87
88
89
90
91
# File 'lib/tram/policy/error.rb', line 87

def contain?(some_key = nil, **some_tags)
  return false if some_key&.!= key
  some_tags.each { |k, v| return false unless tags[k] == v }
  true
end

#fetch(tag, default = Dry::Initializer::UNDEFINED, &block) ⇒ Object

Fetches the tag

Parameters:

  • tag (#to_sym)
  • default (Object) (defaults to: Dry::Initializer::UNDEFINED)
  • block (Proc)

Returns:

  • (Object)


63
64
65
66
67
68
69
# File 'lib/tram/policy/error.rb', line 63

def fetch(tag, default = Dry::Initializer::UNDEFINED, &block)
  if default == Dry::Initializer::UNDEFINED
    tags.fetch(tag.to_sym, &block)
  else
    tags.fetch(tag.to_sym, default, &block)
  end
end

#itemArray Also known as: to_a

The list of arguments for [I18n.t]

Returns:

  • (Array)


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

def item
  [key, tags]
end

#messageString

The text of error message translated to the current locale

Returns:

  • (String)


43
44
45
# File 'lib/tram/policy/error.rb', line 43

def message
  key.is_a?(Symbol) ? I18n.t(*item) : key.to_s
end