Class: Clickwrap::LocalizedText

Inherits:
Object
  • Object
show all
Defined in:
lib/clickwrap/localized_text.rb

Overview

Every human-facing string in a policy — an assertion, a label, a link label, a call to action — can be written three ways:

statement: "I agree to the Terms."          # a literal
statement: :"clickwrap.signup.terms"        # an I18n key
statement: { en: "I agree", es: "Acepto" }  # a locale map

Whichever form the policy uses, Clickwrap resolves it to actual text before the presentation is built, and stores the resolved text and locale in the evidence. Storing only an I18n key would be a false economy: the key's meaning can change in a later deploy, and then the receipt no longer says what the server bound to the presentation.

A missing translation fails closed. Presenting a required legal statement as a raw key, a blank string, or an unexpected fallback language would put something in front of a person and then record it as though it had been legible to them.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(declaration) ⇒ LocalizedText

Returns a new instance of LocalizedText.



24
25
26
27
# File 'lib/clickwrap/localized_text.rb', line 24

def initialize(declaration)
  @declaration = declaration
  freeze
end

Instance Attribute Details

#declarationObject (readonly)

Returns the value of attribute declaration.



22
23
24
# File 'lib/clickwrap/localized_text.rb', line 22

def declaration
  @declaration
end

Instance Method Details

#present?Boolean

Returns:

  • (Boolean)


44
# File 'lib/clickwrap/localized_text.rb', line 44

def present? = !declaration.nil?

#resolve(locale:, interpolations: {}) ⇒ Object

Returns [text, locale_actually_used].



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/clickwrap/localized_text.rb', line 30

def resolve(locale:, interpolations: {})
  case declaration
  when String then [declaration, locale.to_s]
  when Symbol then resolve_i18n_key(locale, interpolations)
  when Hash then resolve_locale_map(locale)
  when Proc then [declaration.call(locale).to_s, locale.to_s]
  when nil then [nil, nil]
  else
    raise DefinitionError,
          "A policy text must be a String, an I18n key Symbol, a locale Hash, or a " \
          "callable, got #{declaration.class}"
  end
end

#to_snapshotObject

How the declaration appears in the compiled policy snapshot. The snapshot records the shape, not the resolved text, because the same revision legitimately renders differently per locale.



49
50
51
52
53
54
55
56
# File 'lib/clickwrap/localized_text.rb', line 49

def to_snapshot
  case declaration
  when String then { "kind" => "literal", "value" => declaration }
  when Symbol then { "kind" => "i18n_key", "value" => declaration.to_s }
  when Hash then { "kind" => "locale_map", "value" => declaration.transform_keys(&:to_s).transform_values(&:to_s) }
  when Proc then { "kind" => "callable" }
  end
end