Sha256: 732751444599e7a6ce929c09feb0adee8d963492d266c0da38bb4fcceadc7bfe

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

# -*- coding: utf-8 -*-

require "custom-active-record/sjis-base"

module ActiveRecord
  class NOIDBase < ActiveRecord::SJISBase

    def id
      read_attribute(self.class.primary_key)
    end
    
    def id=(value)
      write_attribute(self.class.primary_key, value.to_mssql_encode)
    end

    def initialize(attr = nil)
      attr_sjis = attr.to_mssql_encode

      super(attr)
      
      unless attr.nil?
        write_attribute(self.class.primary_key,
                        attr_sjis[self.class.primary_key])
      end

      count = self.class.count(:conditions => 
                               ["[#{self.class.primary_key.to_display_encode}] = ? ",
                                attr_sjis[self.class.primary_key]])
      @new_record = count.zero?
    end
  end
end

class Object
  def to_mssql_encode
    return self
  end

  def to_display_encode
    return self
  end
end

class Array
  def to_mssql_encode
    return self.collect { |a|
      a.to_mssql_encode
    }    
  end
end

class Hash
  def to_mssql_encode
    new_hash = {}

    self.each do |key, value|
      new_hash[key.to_mssql_encode] = value.to_mssql_encode
    end

    return new_hash
  end
end

class String
  def to_mssql_encode
    NKF.nkf("-Wsx --cp932", self)
  end

  def to_display_encode
    NKF.nkf("-Swx --cp932", self).strip
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hiroeorz-custom-active-record-0.1.1 lib/custom-active-record/no-id-base.rb
hiroeorz-custom-active-record-0.1.2 lib/custom_active_record/no_id_base.rb