class Assertion::Translator

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.

@example

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 Andrew Kozin <Andrew.Kozin@gmail.com>

Constants

ROOT

The gem-specific root scope for translations

@return [Symbol]

Attributes

assertion[R]

@!attribute [r] scope

@return [Class] the assertion whose state should be translated

scope[R]

@!attribute [r] scope

@return [Array<Symbol>] the scope for translations

Public Class Methods

new(assertion) click to toggle source

@private

# File lib/assertion/translator.rb, line 57
def initialize(assertion)
  @assertion = assertion
  @scope = "#{ROOT}.#{Inflecto.underscore assertion}"
  IceNine.deep_freeze(self)
end

Public Instance Methods

call(state, args = {}) click to toggle source

Returns the message describing the desired state of given assertion

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

@param [Boolean] state The state of the assertion @param [Hash] args The hash of arguments to be avaliable in a translation

@return [String] The translation

# File lib/assertion/translator.rb, line 72
def call(state, args = {})
  I18n.translate state, args.merge(scope: scope)
end