Class: Cassie::Schema::VersionWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/cassie/schema/version_writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version, io = nil) ⇒ VersionWriter

Returns a new instance of VersionWriter.



11
12
13
14
15
16
17
18
# File 'lib/cassie/schema/version_writer.rb', line 11

def initialize(version, io=nil)
  @io = io
  @version = version
  @up_code = default_up_code
  @down_code = default_down_code

  ensure_dir_exist
end

Instance Attribute Details

#down_codeObject

Returns the value of attribute down_code.



9
10
11
# File 'lib/cassie/schema/version_writer.rb', line 9

def down_code
  @down_code
end

#ioObject (readonly)

Returns the value of attribute io.



5
6
7
# File 'lib/cassie/schema/version_writer.rb', line 5

def io
  @io
end

#up_codeObject

Returns the value of attribute up_code.



8
9
10
# File 'lib/cassie/schema/version_writer.rb', line 8

def up_code
  @up_code
end

#versionObject (readonly)

Returns the value of attribute version.



6
7
8
# File 'lib/cassie/schema/version_writer.rb', line 6

def version
  @version
end

Instance Method Details

#basenameObject



62
63
64
# File 'lib/cassie/schema/version_writer.rb', line 62

def basename
  "#{version_prefix}#{description_suffix}.rb"
end

#directoryObject



58
59
60
# File 'lib/cassie/schema/version_writer.rb', line 58

def directory
  Cassie::Schema.paths[:migrations_directory]
end

#existing_fileObject



66
67
68
# File 'lib/cassie/schema/version_writer.rb', line 66

def existing_file
  Dir.glob("#{directory}/#{version_prefix}*.rb").first
end

#filenameObject



54
55
56
# File 'lib/cassie/schema/version_writer.rb', line 54

def filename
  "#{directory}/#{basename}"
end

#migration_contentsObject



49
50
51
52
# File 'lib/cassie/schema/version_writer.rb', line 49

def migration_contents
  return @migration_contents if defined?(@migration_contents)
  build_migration_contents
end

#migration_contents=(contents) ⇒ Object



45
46
47
# File 'lib/cassie/schema/version_writer.rb', line 45

def migration_contents=(contents)
  @migration_contents = contents
end

#with_ioObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cassie/schema/version_writer.rb', line 31

def with_io
  if io.respond_to?(:<<)
    yield io
  else
    ensure_unique_version
    File.open(filename, 'w'){ |file| yield file }
    # load the file, so the definition for source
    # for the methods will come from that file
    # instead of (eval)
    load File.absolute_path(filename)
    filename
  end
end

#writeString

Writes the version’s migration clas code to a new ruby migraton file in the Cassie::Schema.paths[:migrations_directory]

Returns:

  • (String)

    the filename of the path written

Raises:

  • (IOError)

    if a file with a matching version already exists



25
26
27
28
29
# File 'lib/cassie/schema/version_writer.rb', line 25

def write
  with_io do |io|
    io << migration_contents
  end
end