OSDN Git Service

refactored
authornomeu <nomeu@nomeu.org>
Sat, 12 Jun 2010 19:09:41 +0000 (04:09 +0900)
committernomeu <nomeu@nomeu.org>
Sat, 12 Jun 2010 19:09:41 +0000 (04:09 +0900)
bin/load-entries.rb
lib/location.rb

index 271ea17..c0d95c4 100644 (file)
@@ -26,41 +26,28 @@ def encode(str)
   str && NKF.nkf('-Sw', str)
 end
 
-def dump_zip(path, location)
+def dump_zip(path, location, name)
   Zip::Archive.open(path) do |ar|
     puts "# #{path}"
-    basename = File.basename(path)
-    md = /\.(.+)\z/.match(basename)
-    extname = md[1]
-    code = basename.sub(/\.(.+)\z/, '')
-    case location.code
-    when 'mmd'
-      #
-    when 'mmdfile'
-      code = 'file%04d' % code.to_i
-    when 'mmdacc1'
-      code = 'mini%04d' % code.to_i
-    when 'mmdacc2'
-      code = code[3..-1]
-    end
-    arc = Arc.find_by_location_id_and_code(location.id, code)
+    code, extname = location.generate_arc_code_and_extname(name)
+    arc = location.arcs.find_by_code(code)
+    next unless arc
 
     n = ar.num_files # number of entries
     n.times do |i|
-      entry_name = ar.get_name(i) # get entry name from archive
+      entry_name = ar.get_name(i) # get entry name from archive
 
       # open entry
       ar.fopen(i) do |f|
-        size, comp_size, date, path = f.size, f.comp_size, f.mtime.strftime("%d-%m-%y %H:%M"), encode(f.name)
-        if arc && /\.pmd\z/.match(path)
+        path = encode(f.name)
+        case path
+        when /\.pmd\z/
           puts [ code, path ].join(' ')
           arc.pmds.find_or_create_by_path(path)
-        end
-        if arc && /\.vmd\z/.match(path)
+        when /\.vmd\z/
           puts [ code, path ].join(' ')
           arc.vmds.find_or_create_by_path(path)
-        end
-        if arc && /\.x\z/.match(path)
+        when /\.x\z/
           puts [ code, path ].join(' ')
           arc.xes.find_or_create_by_path(path)
         end
@@ -78,7 +65,7 @@ open(ent) do |f|
   while line = f.gets
     if scrap.match(line)
       name, = scrap.row
-      dump_zip("/Volumes/uploader/arc/#{location.code}/#{name}", location)
+      dump_zip("/Volumes/uploader/arc/#{location.code}/#{name}", location, name)
       puts
     end
   end
index d937c0a..2dae5a8 100644 (file)
@@ -1,7 +1,7 @@
 class Location < ActiveRecord::Base
   has_many :arcs
 
-  def create_arc(name, summary, size, date, origname)
+  def generate_arc_code_and_extname(name)
     md = /\.(.+)\z/.match(name)
     extname = md[1]
     code = File.basename(name, '.' + extname)
@@ -15,6 +15,11 @@ class Location < ActiveRecord::Base
     when 'mmdacc2'
       code = code[3..-1]
     end
+    [ code, extname ]
+  end
+
+  def create_arc(name, summary, size, date, origname)
+    code, extname = generate_arc_code_and_extname(name)
     arc = Arc.find_by_code(code)
     if arc
       puts "found code: #{code}"