Class: Interpreter::Translation

Inherits:
Base
  • Object
show all
Defined in:
lib/interpreter/translation.rb

Direct Known Subclasses

InterpreterTranslation

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

attributes, #attributes, locales, #new_record?

Constructor Details

#initialize(options = {}) ⇒ Translation

Returns a new instance of Translation.



7
8
9
10
11
12
# File 'lib/interpreter/translation.rb', line 7

def initialize options={}
  self.key = options[:key]
  self.class.available_locales.each do |locale|
    self.send("#{locale}=", options[locale])
  end
end

Class Method Details

.allObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/interpreter/translation.rb', line 48

def self.all
  result = []
  Interpreter.backend.keys("??.*").each do |full_key|
    key, locale, value = parse_key(full_key)

    obj = result.select{|r| r.key == key }.first || self.new
    obj.send("#{locale}=", value)
    obj.send('key=', key)
    result << obj
  end
  return result.uniq
end

.available_localesObject



90
91
92
# File 'lib/interpreter/translation.rb', line 90

def self.available_locales
  Interpreter.locales
end

.destroy(key) ⇒ Object



86
87
88
# File 'lib/interpreter/translation.rb', line 86

def self.destroy key
  Interpreter.backend.keys("??.#{key}").sum{|k| Interpreter.backend.del(k)}
end

.find_all_by_value_like(str) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/interpreter/translation.rb', line 76

def self.find_all_by_value_like str
  keys = []
  Interpreter.backend.keys.each do |full_key|
    if Interpreter.backend.get(full_key).downcase.include?(str.downcase)
      keys << parse_key(full_key).first
    end
  end
  keys.map{|k| find_by_key k}
end

.find_by_key(key) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/interpreter/translation.rb', line 61

def self.find_by_key key
  collection = Interpreter.backend.keys("??.#{key}")
  if collection.present?
    obj = self.new
    obj.send('key=', key)
    collection.each do |full_key|
      key, locale, value = parse_key(full_key)
      obj.send("#{locale}=", value)
    end
    return obj
  else
    return nil
  end
end

Instance Method Details

#==(other) ⇒ Object



44
45
46
# File 'lib/interpreter/translation.rb', line 44

def ==(other)
  self.key == other.key
end

#idObject



22
23
24
# File 'lib/interpreter/translation.rb', line 22

def id
  self.key.gsub('.','-')
end

#persisted?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
# File 'lib/interpreter/translation.rb', line 14

def persisted?
  if key?
    Interpreter.backend.keys("??.#{key}").present?
  else
    super
  end
end

#saveObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/interpreter/translation.rb', line 26

def save
  if self.valid?
    self.class.available_locales.each do |locale|
      value = self.send(locale)
      Interpreter.backend.set("#{locale}.#{key}", value.to_json) unless value.nil?
    end
  else
    return false
  end
end

#update_attributes(options = {}) ⇒ Object



37
38
39
40
41
42
# File 'lib/interpreter/translation.rb', line 37

def update_attributes options={}
  self.key = options[:key]
  self.class.available_locales.each do |locale|
    self.send("#{locale}=", options[locale])
  end
end