Class: Mobility::Backend::KeyValue::TranslationsCache

Inherits:
Object
  • Object
show all
Defined in:
lib/mobility/backend/key_value.rb

Overview

Simple cache to memoize translations as a hash so they can be fetched quickly.

Instance Method Summary collapse

Constructor Details

#initialize(backend) ⇒ TranslationsCache

Parameters:

  • backend

    Instance of KeyValue backend to cache



49
50
51
# File 'lib/mobility/backend/key_value.rb', line 49

def initialize(backend)
  @cache = Hash.new { |hash, locale| hash[locale] = backend.translation_for(locale) }
end

Instance Method Details

#[](locale) ⇒ Object

Parameters:

  • locale (Symbol)

    Locale to fetch



54
55
56
# File 'lib/mobility/backend/key_value.rb', line 54

def [](locale)
  @cache[locale].value
end

#[]=(locale, value) ⇒ Object

Parameters:

  • locale (Symbol)

    Locale to set

  • value (String)

    Value to set



60
61
62
# File 'lib/mobility/backend/key_value.rb', line 60

def []=(locale, value)
  @cache[locale].value = value
end

#each_translation {|locale, translation| ... } ⇒ Object

Yields:

  • (locale, translation)


65
66
67
# File 'lib/mobility/backend/key_value.rb', line 65

def each_translation &block
  @cache.each_value &block
end