Module: SimpleRecord::Translations

Included in:
Base
Defined in:
lib/simple_record/translations.rb

Constant Summary collapse

@@offset =
9223372036854775808
@@padding =
20
@@date_format =
"%Y-%m-%dT%H:%M:%S"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.decrypt(value, key = nil) ⇒ Object

Raises:



238
239
240
241
242
243
244
245
246
247
# File 'lib/simple_record/translations.rb', line 238

def self.decrypt(value, key=nil)
#            puts "decrypt orig value #{value} "
  unencoded_value = Base64.decode64(value)
  raise SimpleRecordError, "Encryption key must be defined on the attribute." if key.nil?
  key             = key || get_encryption_key()
#            puts "decrypting #{unencoded_value} "
  decrypted_value = SimpleRecord::Encryptor.decrypt(:value => unencoded_value, :key => key)
#             "decrypted #{unencoded_value} to #{decrypted_value}"
  decrypted_value
end

.encrypt(value, key = nil) ⇒ Object

Raises:



229
230
231
232
233
234
235
# File 'lib/simple_record/translations.rb', line 229

def self.encrypt(value, key=nil)
  key = key || get_encryption_key()
  raise SimpleRecordError, "Encryption key must be defined on the attribute." if key.nil?
  encrypted_value = SimpleRecord::Encryptor.encrypt(:value => value, :key => key)
  encoded_value   = Base64.encode64(encrypted_value)
  encoded_value
end

.from_float(x) ⇒ Object



161
162
163
164
165
166
# File 'lib/simple_record/translations.rb', line 161

def self.from_float(x)
  return x
#            if x == 0.0
#                return "3 000 0.0000000000000000"
#            end
end

.pad_and_offset(x, att_meta = nil) ⇒ Object

Change name to something more appropriate like ruby_to_sdb



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/simple_record/translations.rb', line 127

def self.pad_and_offset(x, att_meta=nil) # Change name to something more appropriate like ruby_to_sdb
  # todo: add Float, etc
  #    puts 'padding=' + x.class.name + " -- " + x.inspect
  if x.kind_of? Integer
    x     += @@offset
    x_str = x.to_s
    # pad
    x_str = '0' + x_str while x_str.size < 20
    return x_str
  elsif x.respond_to?(:iso8601)
    #  puts x.class.name + ' responds to iso8601'
    #
    # There is an issue here where Time.iso8601 on an incomparable value to DateTime.iso8601.
    # Amazon suggests: 2008-02-10T16:52:01.000-05:00
    #                  "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
    #
    if x.is_a? DateTime
      x_str = x.getutc.strftime(@@date_format)
    elsif x.is_a? Time
      x_str = x.getutc.strftime(@@date_format)
    elsif x.is_a? Date
      x_str = x.strftime(@@date_format)

    end
    return x_str
  elsif x.is_a? Float
    from_float(x)
  else
    return x
  end
end

.pass_hash(value) ⇒ Object



269
270
271
272
273
# File 'lib/simple_record/translations.rb', line 269

def self.pass_hash(value)
  hashed        = Password::create_hash(value)
  encoded_value = Base64.encode64(hashed)
  encoded_value
end

.pass_hash_check(value, value_to_compare) ⇒ Object



275
276
277
278
# File 'lib/simple_record/translations.rb', line 275

def self.pass_hash_check(value, value_to_compare)
  unencoded_value = Base64.decode64(value)
  return Password::check(value_to_compare, unencoded_value)
end

.un_offset_int(x) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/simple_record/translations.rb', line 198

def self.un_offset_int(x)
  if x.is_a?(String)
    x2 = x.to_i
#            puts 'to_i=' + x2.to_s
    x2 -= @@offset
#            puts 'after subtracting offset='+ x2.to_s
    x2
  else
    x
  end
end

Instance Method Details

#convert_dates_to_sdbObject



261
262
263
264
265
266
267
# File 'lib/simple_record/translations.rb', line 261

def convert_dates_to_sdb()

#            defined_attributes_local.each_pair do |name, att_meta|
#          puts 'int encoding: ' + i.to_s

#            end
end

#pad_and_offset_ints_to_sdbObject



250
251
252
253
254
255
256
257
258
259
# File 'lib/simple_record/translations.rb', line 250

def pad_and_offset_ints_to_sdb()

#            defined_attributes_local.each_pair do |name, att_meta|
#                if att_meta.type == :int && !self[name.to_s].nil?
#                    arr = @attributes[name.to_s]
#                    arr.collect!{ |x| self.class.pad_and_offset(x) }
#                    @attributes[name.to_s] = arr
#                end
#            end
end

#ruby_to_sdb(name, value) ⇒ Object

Time to second precision



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
# File 'lib/simple_record/translations.rb', line 22

def ruby_to_sdb(name, value)
  return nil if value.nil?
  name     = name.to_s
#            puts "Converting #{name} to sdb value=#{value}"
#            puts "atts_local=" + defined_attributes_local.inspect

  att_meta = get_att_meta(name)

  if value.is_a? Array
    ret = value.collect { |x| ruby_to_string_val(att_meta, x) }
  else
    ret = ruby_to_string_val(att_meta, value)
  end

  unless value.blank?
    if att_meta.options
      if att_meta.options[:encrypted]
#                    puts "ENCRYPTING #{name} value #{value}"
        ret = Translations.encrypt(ret, att_meta.options[:encrypted])
#                    puts 'encrypted value=' + ret.to_s
      end
      if att_meta.options[:hashed]
#                        puts "hashing #{name}"
        ret = Translations.pass_hash(ret)
#                        puts "hashed value=" + ret.inspect
      end
    end
  end

  return ret

end

#ruby_to_string_val(att_meta, value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/simple_record/translations.rb', line 9

def ruby_to_string_val(att_meta, value)
  if att_meta.type == :int
    ret = Translations.pad_and_offset(value, att_meta)
  elsif att_meta.type == :date
    ret = Translations.pad_and_offset(value, att_meta)
  else
    ret = value.to_s
  end
  ret
end

#sdb_to_ruby(name, value) ⇒ Object

Convert value from SimpleDB String version to real ruby value.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/simple_record/translations.rb', line 57

def sdb_to_ruby(name, value)
#            puts 'sdb_to_ruby arg=' + name.inspect + ' - ' + name.class.name + ' - value=' + value.to_s
  return nil if value.nil?
  att_meta = get_att_meta(name)

  if att_meta.options
    if att_meta.options[:encrypted]
      value = Translations.decrypt(value, att_meta.options[:encrypted])
    end
    if att_meta.options[:hashed]
      return PasswordHashed.new(value)
    end
  end


  if !has_id_on_end(name) && att_meta.type == :belongs_to
    class_name = att_meta.options[:class_name] || name.to_s[0...1].capitalize + name.to_s[1...name.to_s.length]
    # Camelize classnames with underscores (ie my_model.rb --> MyModel)
    class_name = class_name.camelize
    #      puts "attr=" + @attributes[arg_id].inspect
    #      puts 'val=' + @attributes[arg_id][0].inspect unless @attributes[arg_id].nil?
    ret        = nil
    arg_id     = name.to_s + '_id'
    arg_id_val = send("#{arg_id}")
    if arg_id_val
      if !cache_store.nil?
#                        arg_id_val = @attributes[arg_id][0]
        cache_key = self.class.cache_key(class_name, arg_id_val)
#          puts 'cache_key=' + cache_key
        ret       = cache_store.read(cache_key)
#          puts 'belongs_to incache=' + ret.inspect
      end
      if ret.nil?
        to_eval = "#{class_name}.find('#{arg_id_val}')"
#      puts 'to eval=' + to_eval
        begin
          ret = eval(to_eval) # (defined? #{arg}_id)
        rescue SimpleRecord::ActiveSdb::ActiveSdbError => ex
          if ex.message.include? "Couldn't find"
            ret = RemoteNil.new
          else
            raise ex
          end
        end

      end
    end
    value = ret
  else
    if value.is_a? Array
      value = value.collect { |x| string_val_to_ruby(att_meta, x) }
    else
      value = string_val_to_ruby(att_meta, value)
    end
  end
  value
end

#string_val_to_ruby(att_meta, value) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/simple_record/translations.rb', line 115

def string_val_to_ruby(att_meta, value)
  if att_meta.type == :int
    value = Translations.un_offset_int(value)
  elsif att_meta.type == :date
    value = to_date(value)
  elsif att_meta.type == :boolean
    value = to_bool(value)
  end
  value
end

#to_bool(x) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/simple_record/translations.rb', line 190

def to_bool(x)
  if x.is_a?(String)
    x == "true" || x == "1"
  else
    x
  end
end

#to_date(x) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/simple_record/translations.rb', line 182

def to_date(x)
  if x.is_a?(String)
    DateTime.parse(x)
  else
    x
  end
end

#unpad(i, attributes) ⇒ Object



210
211
212
213
214
215
216
217
218
# File 'lib/simple_record/translations.rb', line 210

def unpad(i, attributes)
  if !attributes[i].nil?
#          puts 'before=' + self[i].inspect
    attributes[i].collect! { |x|
      un_offset_int(x)

    }
  end
end

#unpad_selfObject



220
221
222
223
224
225
226
# File 'lib/simple_record/translations.rb', line 220

def unpad_self
  defined_attributes_local.each_pair do |name, att_meta|
    if att_meta.type == :int
      unpad(name, @attributes)
    end
  end
end

#wrap_if_required(arg, value, sdb_val) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/simple_record/translations.rb', line 169

def wrap_if_required(arg, value, sdb_val)
  return nil if value.nil?

  att_meta = defined_attributes_local[arg.to_sym]
  if att_meta && att_meta.options
    if att_meta.options[:hashed]
#                    puts 'wrapping ' + arg_s
      return PasswordHashed.new(sdb_val)
    end
  end
  value
end