Permalink
Browse files

Add a method to import from a file

  • Loading branch information...
1 parent 985b822 commit 9592360fbb086fe9cd99ef5adda37e0735a22686 @scotchi scotchi committed Jul 21, 2012
Showing with 19 additions and 5 deletions.
  1. +19 −5 Ruby/lib/directededge/updatejob.rb
@@ -29,7 +29,7 @@ class UpdateJob
FOOTER = "</directededge>\n"
def initialize(database, mode)
- raise ArgumentError.new unless [ :replace, :update ].include?(mode)
+ validate_mode(mode)
@database = database
@mode = mode
@add_file = temp(:add)
@@ -60,6 +60,16 @@ def run
end
end
+ def self.import_from_file(database, filename, mode = :replace)
+ validate_mode(mode)
+ file = File.open(filename, 'r')
+ if mode == :replace
+ database.resource.put(file)
+ elsif mode == :update
+ database.resource[:update_method => :add].post(file)
+ end
+ end
+
private
class Item < DirectedEdge::Item
@@ -81,10 +91,8 @@ def save
end
end
- def temp(action)
- file = Tempfile.new("#{@database.name}-#{action}")
- file.puts(HEADER)
- file
+ def self.validate_mode(mode)
+ raise ArgumentError.new unless [ :replace, :update ].include?(mode)
end
def validate_updates(item)
@@ -97,5 +105,11 @@ def validate_updates(item)
end
end
end
+
+ def temp(action)
+ file = Tempfile.new("#{@database.name}-#{action}")
+ file.puts(HEADER)
+ file
+ end
end
end

0 comments on commit 9592360

Please sign in to comment.