Class: Migrake::FileSystemStore

Inherits:
Object
  • Object
show all
Defined in:
lib/migrake/file_system_store.rb

Overview

The FileSystemStore handles the file where tasks that have already been run are stored so they aren’t run again.

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileSystemStore

Public: Initialize the store.

path - The path to the file where we store the information. A Pathname of

String should be provided.


12
13
14
# File 'lib/migrake/file_system_store.rb', line 12

def initialize(path)
  @path = Pathname(path)
end

Instance Method Details

#allObject

Public: Load all the tasks from the store.

Returns a Set.



37
38
39
# File 'lib/migrake/file_system_store.rb', line 37

def all
  @all ||= YAML.load(@path.read) || Set.new
end

#prepareObject

Public: Ensure the file we use to store information exists.

Returns nothing.



19
20
21
22
# File 'lib/migrake/file_system_store.rb', line 19

def prepare
  FileUtils.mkdir_p @path.dirname
  FileUtils.touch @path
end

#put(task) ⇒ Object

Public: Add one task to the store. This immediately writes the file to disk, to preserve consistency.

task - A string with a task’s name.

Returns nothing.



30
31
32
# File 'lib/migrake/file_system_store.rb', line 30

def put(task)
  write(all << task)
end

#write(set) ⇒ Object

Public: Write a whole set of tasks to the store, replacing what is in it.

set - A Set of tasks.

Returns nothing.



46
47
48
# File 'lib/migrake/file_system_store.rb', line 46

def write(set)
  @path.open("w+") { |f| f.puts YAML.dump(set) }
end