Module: Assertion::Messages

Included in:
Base
Defined in:
lib/assertion/messages.rb

Overview

Module Messages provides a feature for gem-specific translation of messages describing the desired state of the assertion

You need to declare a hash of attributes to be added to the translation.

Examples:

class MyClass
  include Assertion::Messages
  def attributes
    {}
  end
end

item = MyClass.new
item.message(true)
# => "translation missing: en.assertion.my_class.right"
item.message(false)
# => "translation missing: en.assertion.my_class.wrong"

Author:

Constant Summary collapse

ROOT =

The gem-specific root scope for translations

Returns:

  • (Symbol)
:assertion
DICTIONARY =

The states to be translated with their dictionary names

Returns:

  • (Hash<Object => Symbol>)
{ true => :right, false => :wrong }

Instance Method Summary collapse

Instance Method Details

#attributesObject



59
60
61
# File 'lib/assertion/messages.rb', line 59

def attributes
  {}
end

#message(state) ⇒ String

Returns the message describing the desired state of assertion

The translation is provided for the gem-specific scope for the current class

Parameters:

  • state (Boolean)

    <description>

Returns:

  • (String)

    The translation



51
52
53
54
55
56
# File 'lib/assertion/messages.rb', line 51

def message(state)
  key   = DICTIONARY[state]
  scope = [ROOT, Inflector[:to_snake_path][self.class.name]]

  I18n.translate key, attributes.merge(scope: scope)
end