<commit_before># This module defines all the methods that perform data translations for storage and retrieval.
module SimpleRecord
  module Translations

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

    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

    # Time to second precision

    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


    # Convert value from SimpleDB String version to real ruby value.
    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

    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)
      elsif att_meta.type == :float
        value = to_f(value)
      end
      value
    end


    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<commit_msg>One more time, with feeling.<commit_after># This module defines all the methods that perform data translations for storage and retrieval.
module SimpleRecord
  module Translations

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

    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

    # Time to second precision

    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


    # Convert value from SimpleDB String version to real ruby value.
    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

    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)
      elsif att_meta.type == :float
        value = Float(value)
      end
      value
    end


    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
{'fraction_non_alphanumeric': 0.09966428871170793, 'fraction_numerical': 0.013848090642047839, 'mean_word_length': 1.837202380952381, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4802933', 'n_tokens_mistral': 3192, 'n_tokens_neox': 3027, 'n_words': 695}