Class: Idioma::RedisBackend

Inherits:
Object
  • Object
show all
Defined in:
app/models/idioma/redis_backend.rb

Overview

Class for dealing with deleting and updating phrases in the i18n backend.

This is specifically when the backend is Redis

Class Method Summary collapse

Class Method Details

.delete_phrase(phrase) ⇒ Boolean

Delete a given Phrase from Redis

Parameters:

  • The (Phrase)

    Phrase to delete

Returns:

  • (Boolean)

    Result of the del command in Redis



10
11
12
# File 'app/models/idioma/redis_backend.rb', line 10

def self.delete_phrase(phrase)
  Idioma.configuration.redis_backend.store.del("#{phrase.locale}.#{phrase.i18n_key}")
end

.float?(input) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'app/models/idioma/redis_backend.rb', line 28

def self.float?(input)
  true if Float(input) rescue false
end

.integer?(input) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'app/models/idioma/redis_backend.rb', line 24

def self.integer?(input)
  !!(input =~ /^-?\d+$/)
end

.parse_value(value) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/idioma/redis_backend.rb', line 32

def self.parse_value(value)
  case
  when (value =~ /^\[.*\]$/) || (value =~ /^\{.*\}$/)
    eval(value)
  when value == "nil"
    nil
  when self.integer?(value)
    Integer(value)
  when self.float?(value)
    Float(value)
  else
    value
  end
end

.update_phrase(phrase) ⇒ Boolean

Update (or create) a Phrase in Redis

Parameters:

  • The (Phrase)

    Phrase to update or create

Returns:

  • (Boolean)

    Result of the store_translations command from the Redis backend (I18n::Backend)



17
18
19
20
21
22
# File 'app/models/idioma/redis_backend.rb', line 17

def self.update_phrase(phrase)
  value = self.parse_value(phrase.i18n_value)

  Idioma.configuration.redis_backend.
    store_translations(phrase.locale, {phrase.i18n_key => value}, :escape => false)
end