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
DICTIONARY =

The states to be translated with their dictionary names

Returns:

  • (Hash<Object => Symbol>)
{ true => :truthy, false => :falsey }

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



62
63
64
# File 'lib/assertion/translator.rb', line 62

def assertion
  @assertion
end

#scopeClass (readonly)

Returns the assertion whose state should be translated.

Returns:

  • (Class)

    the assertion whose state should be translated



56
57
58
# File 'lib/assertion/translator.rb', line 56

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 for the 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



89
90
91
# File 'lib/assertion/translator.rb', line 89

def call(state, args = {})
  I18n.translate DICTIONARY[state ? true : false], args.merge(scope: scope)
end

.initialize(assertion) ⇒ Object



73
74
75
76
77
# File 'lib/assertion/translator.rb', line 73

def initialize(assertion)
  @assertion = assertion
  @scope = self.class.scope(assertion)
  freeze
end

.new(assertion) ⇒ Assertion::Translator

Creates a state translator for the given assertion class

Parameters:

  • assertion (Class)

Returns:



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

.scope(klass) ⇒ Array<Symbol>

Provides a scope for the class

Parameters:

  • klass (Class)

Returns:

  • (Array<Symbol>)


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

def self.scope(klass)
  [ROOT, Inflector[:to_snake_path][klass.name].to_sym]
end