Class: Knj::Translations
Defined Under Namespace
Classes: Translation
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#db ⇒ Object
Returns the value of attribute db.
-
#ob ⇒ Object
Returns the value of attribute ob.
Instance Method Summary collapse
-
#delete(obj) ⇒ Object
Deletes all translations for a given object.
-
#get(obj, key, args = {}) ⇒ Object
Returns the translated value for an object by the given key.
-
#initialize(args) ⇒ Translations
constructor
A new instance of Translations.
-
#set(obj, values, args = {}) ⇒ Object
Sets translations for an object by the given hash-keys and hash-values.
Constructor Details
#initialize(args) ⇒ Translations
Returns a new instance of Translations.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/knj/translations.rb', line 4 def initialize(args) @args = args raise "No DB given." if !@args[:db] @db = @args[:db] @ob = Knj::Objects.new( :db => @args[:db], :extra_args => [self], :class_path => File.dirname(__FILE__), :module => Knj::Translations, :require => false, :datarow => true ) end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
2 3 4 |
# File 'lib/knj/translations.rb', line 2 def args @args end |
#cache ⇒ Object
Returns the value of attribute cache.
2 3 4 |
# File 'lib/knj/translations.rb', line 2 def cache @cache end |
#db ⇒ Object
Returns the value of attribute db.
2 3 4 |
# File 'lib/knj/translations.rb', line 2 def db @db end |
#ob ⇒ Object
Returns the value of attribute ob.
2 3 4 |
# File 'lib/knj/translations.rb', line 2 def ob @ob end |
Instance Method Details
#delete(obj) ⇒ Object
Deletes all translations for a given object.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/knj/translations.rb', line 103 def delete(obj) classn = obj.class.name objid = obj.id.to_s if obj.instance_variable_defined?("@knj_translations_cache") cache = obj.instance_variable_get("@knj_translations_cache") end trans = @ob.list(:Translation, { "object_id" => obj.id, "object_class" => obj.class.name }) trans.each do |tran| #Delete the cache if defined on the object. cache.delete(tran[:key].to_sym) if cache and cache.key?(tran[:key].to_sym) #Delete the translation object. @ob.delete(tran) end end |
#get(obj, key, args = {}) ⇒ Object
Returns the translated value for an object by the given key.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/knj/translations.rb', line 21 def get(obj, key, args = {}) return "" if !obj if args[:locale] locale = args[:locale].to_sym else locale = @args[:locale].to_sym end #Force to symbol to save memory when caching. key = key.to_sym #Set-get the cache-hash for the object. if !obj.instance_variable_defined?("@knj_translations_cache") obj.instance_variable_set("@knj_translations_cache", {}) end cache = obj.instance_variable_get("@knj_translations_cache") #Return from cache if set. if cache.key?(key) and cache[key].key?(locale) return cache[key][locale] end trans = @ob.list(:Translation, { "object_class" => obj.class.name, "object_id" => obj.id, "key" => key, "locale" => locale }) if trans.empty? print "Nothing found - returning empty string.\n" if @args[:debug] or args[:debug] return "" end trans.each do |tran| if !cache[key] cache[key] = { locale => tran[:value] } elsif !cache[key][locale] cache[key][locale] = tran[:value] end end return cache[key][locale] end |
#set(obj, values, args = {}) ⇒ Object
Sets translations for an object by the given hash-keys and hash-values.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/knj/translations.rb', line 71 def set(obj, values, args = {}) #Reset cache to reflect the updates when read next time. obj.instance_variable_set("@knj_translations_cache", {}) if args[:locale] locale = args[:locale] else locale = @args[:locale] end values.each do |key, val| trans = @ob.get_by(:Translation, { "object_id" => obj.id, "object_class" => obj.class.name, "key" => key, "locale" => locale }) if trans trans.update(:value => val) else @ob.add(:Translation, { :object => obj, :key => key, :locale => locale, :value => val }) end end end |