Class: Assertion::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/assertion/translator.rb

Overview

Module defines how to translate messages describing the desired state of the current assertion

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

Examples:

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

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

Author:

Constant Summary collapse

ROOT =

The gem-specific root scope for translations

Returns:

  • (Symbol)
:assertion

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#assertionClass (readonly)

Returns the assertion whose state should be translated.

Returns:

  • (Class)

    the assertion whose state should be translated



46
47
48
# File 'lib/assertion/translator.rb', line 46

def assertion
  @assertion
end

#scopeClass (readonly)

Returns the assertion whose state should be translated.

Returns:

  • (Class)

    the assertion whose state should be translated



40
41
42
# File 'lib/assertion/translator.rb', line 40

def scope
  @scope
end

Class Method Details

.call(state, args = {}) ⇒ String

Returns the message describing the desired state of given assertion

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

Parameters:

  • state (Boolean)

    The state of the assertion

  • args (Hash) (defaults to: {})

    The hash of arguments to be avaliable in a translation

Returns:

  • (String)

    The translation



72
73
74
# File 'lib/assertion/translator.rb', line 72

def call(state, args = {})
  I18n.translate state, args.merge(scope: scope)
end

.initialize(assertion) ⇒ Object



57
58
59
60
61
# File 'lib/assertion/translator.rb', line 57

def initialize(assertion)
  @assertion = assertion
  @scope = "#{ROOT}.#{Inflecto.underscore assertion}"
  IceNine.deep_freeze(self)
end

.new(assertion) ⇒ Assertion::Translator

Creates a state translator for the given assertion class

Parameters:

  • assertion (Class)

Returns:



# File 'lib/assertion/translator.rb', line 48