Module: SimpleRecord::Translations

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

Constant Summary collapse

@@offset =
9223372036854775808
@@padding =
20
@@date_format =

Time to second precision

"%Y-%m-%dT%H:%M:%S"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.decrypt(value, key = nil) ⇒ Object

Raises:



207
208
209
210
211
212
213
214
215
216
# File 'lib/simple_record/translations.rb', line 207

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:



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

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

.pad_and_offset(x, att_meta = nil) ⇒ Object

Change name to something more appropriate like ruby_to_sdb



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/simple_record/translations.rb', line 107

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
    else
        return x
    end
end

.pass_hash(value) ⇒ Object



238
239
240
241
242
# File 'lib/simple_record/translations.rb', line 238

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



244
245
246
247
# File 'lib/simple_record/translations.rb', line 244

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



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

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



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

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



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

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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/simple_record/translations.rb', line 9

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 = defined_attributes_local[name.to_sym]

    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


    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]
                ret = Translations.pass_hash(ret)
            end
        end
    end

    return ret.to_s

end

#sdb_to_ruby(name, value) ⇒ Object

Convert value from SimpleDB String version to real ruby value.



48
49
50
51
52
53
54
55
56
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
# File 'lib/simple_record/translations.rb', line 48

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 = defined_attributes_local[name.to_sym]

    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 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 Aws::ActiveSdb::ActiveSdbError
                    if $!.message.include? "Couldn't find"
                        ret = nil
                    else
                        raise $!
                    end
                end

            end
        end
        value = ret
    elsif 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



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

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

#to_date(x) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/simple_record/translations.rb', line 151

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

#unpad(i, attributes) ⇒ Object



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

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



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

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



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/simple_record/translations.rb', line 138

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