Class: MongoImport::Snapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo-import/snapshot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Snapshot

Returns a new instance of Snapshot.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
# File 'lib/mongo-import/snapshot.rb', line 11

def initialize(name, opts={})
  raise ArgumentError, ':db is missing' unless opts[:db]
  raise ArgumentError, ':collection is missing' unless opts[:collection]
  defaults = {host: 'localhost', port: 27017, exec: 'mongoimport', path: 'spec/snapshots'}
  @opts = OpenStruct.new(defaults.merge(opts))
  @path = @opts.path
  @name = name
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



9
10
11
# File 'lib/mongo-import/snapshot.rb', line 9

def collection
  @collection
end

#dbObject (readonly)

Returns the value of attribute db.



9
10
11
# File 'lib/mongo-import/snapshot.rb', line 9

def db
  @db
end

#optsObject (readonly)

Returns the value of attribute opts.



9
10
11
# File 'lib/mongo-import/snapshot.rb', line 9

def opts
  @opts
end

Instance Method Details

#importObject

returns true if the import succeeded



21
22
23
24
25
26
27
# File 'lib/mongo-import/snapshot.rb', line 21

def import
  raise "Could not find #{@opts.exec} in your PATH" unless system("which #{opts.exec} > /dev/null")

  file = File.join(@path, "#{@name}.json")
  cmd  = "#{opts.exec} --host #{opts.host} --port #{opts.port} --drop --db #{opts.db} --collection #{opts.collection} #{file}"
  `#{cmd}`
end