Created
October 9, 2011 00:21
-
-
Save ultraist/1273097 to your computer and use it in GitHub Desktop.
NGFile.txt用のActiveRecordのModel (Ruby 1.9.2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateNgFiles < ActiveRecord::Migration | |
def self.up | |
create_table :ng_files do |t| | |
t.string :ng_hash | |
t.string :memo | |
t.timestamps | |
end | |
add_index :ng_files, :ng_hash, :unique => true | |
end | |
def self.down | |
drop_table :ng_files | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
class NgFile < ActiveRecord::Base | |
def self.find_by_file(filename) | |
find_by_ng_hash(hash_from_file(filename)) | |
end | |
def self.find_by_blob(blob) | |
find_by_ng_hash(hash_from_blob(blob)) | |
end | |
def self.find_by_md5(md5str) | |
md5digest = [md5str].pack("H*") | |
find_by_ng_hash(hash_from_md5digest(md5digest)) | |
end | |
def self.pull(data_file) | |
transaction do | |
File.open(data_file, "r:windows-31j:utf-8") do |f| | |
until (f.eof?) | |
line = f.readline.chomp | |
hash, memo = line.split("=") | |
memo = memo.gsub(/^\*/, '') | |
NgFile.find_or_create_by_ng_hash(hash) do |ngfile| | |
ngfile.memo = memo | |
end | |
end | |
end | |
end | |
end | |
private | |
NGHASH_CHAR = | |
[ | |
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', | |
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', | |
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', | |
'U', 'V' | |
] | |
NGHASH_LEN = 26 | |
def self.hash_from_file(filename) | |
hash_from_blob(File.read(filename)) | |
end | |
def self.hash_from_blob(blob) | |
hash_from_md5digest(Digest::MD5.digest(blob)) | |
end | |
def self.make_word(lo, hi) | |
lo + (hi << 8) | |
end | |
def self.hash_from_md5digest(md5digest) | |
hash = [] | |
md5 = Array.new | |
(0 ... md5digest.length).each{|i| | |
md5.push(md5digest[i].unpack('C')[0]) | |
} | |
md5.push(0) | |
(0 ... NGHASH_LEN).each do |i| | |
hash[i] = NGHASH_CHAR[(make_word(md5[5 * i / 8], md5[5 * i / 8 + 1]) >> (5 * i % 8)) & 0x1F] | |
end | |
return hash.join | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment