Class: DeployMongo::DeltaLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/deploy_mongo/delta_loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(deltas_folder) ⇒ DeltaLoader

Returns a new instance of DeltaLoader.



4
5
6
# File 'lib/deploy_mongo/delta_loader.rb', line 4

def initialize(deltas_folder)
  @deltas_folder = deltas_folder
end

Instance Method Details

#convert_to_delta(id, file) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/deploy_mongo/delta_loader.rb', line 28

def convert_to_delta(id, file)
  file = File.open(file, "rb")
  contents = file.read
  file.close
  command = ''
  rollback_command = ''
  commands = contents.split('//@undo')
  raise "#{file_name} content is not valid " if commands.count == 0
  command = commands[0]
  rollback_command = commands[1] if commands[1]
  
  file_name = File.basename(file)
  Delta.new(id,file_name,command,rollback_command)
end

#get_deltasObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/deploy_mongo/delta_loader.rb', line 8

def get_deltas
  hash = {}
  files = Dir["#{@deltas_folder}/*.js"].select {|f| File.file?(f)}
  files.each do |file|
    file_name = File.basename(file)
    key = file_name.split('_')[0].to_i
    if (key == 0) 
      e = RuntimeError.new("invalid file name #{file_name}")
      raise e
    end
    if (hash.has_key?(key)) 
      e = RuntimeError.new("duplicate key found in file #{file_name}")
      raise e
    end
  
    hash[key] = convert_to_delta(key,file) 
  end
  hash
end