Module: Waves::Layers::ORM

Defined in:
lib/waves/layers/orm/migration.rb,
lib/waves/layers/orm/providers/sequel.rb,
lib/waves/layers/orm/providers/filebase.rb,
lib/waves/layers/orm/providers/data_mapper.rb,
lib/waves/layers/orm/providers/active_record.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ActiveRecord, DataMapper, Filebase, Sequel

Constant Summary collapse

MIGRATION_FILE_PATTERN =

Glob pattern

'[0-9][0-9][0-9]_*.rb'.freeze

Class Method Summary collapse

Class Method Details

.create_migration_for(orm) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/waves/layers/orm/migration.rb', line 10

def self.create_migration_for(orm)
  source          = migration_template(orm.to_s.snake_case, ENV['template'])
  destination     = migration_destination(ENV['name'])
  migration_name  = migration_name(ENV['name'])

  context = {:class_name => migration_name.camel_case}

  write_migration(context, source, destination)
end

.latest_migration_versionObject

Uses the migration files in the migration directory to determine the highest numbered existing migration.



45
46
47
48
# File 'lib/waves/layers/orm/migration.rb', line 45

def self.latest_migration_version
  l = migration_files.last
  l ? File.basename(l).to_i : nil
end

.migration_destination(name) ⇒ Object

Given a migration name, returns the path of the file that would be created.



63
64
65
66
# File 'lib/waves/layers/orm/migration.rb', line 63

def self.migration_destination(name)
  version = next_migration_version
  migration_directory / "#{'%03d' % version}_#{migration_name(name)}.rb"
end

.migration_directoryObject

Where Waves keeps its migration files



21
22
23
# File 'lib/waves/layers/orm/migration.rb', line 21

def self.migration_directory
  :schema / :migrations
end

.migration_files(range = nil) ⇒ Object

Returns any found migration files in the supplied directory.



26
27
28
29
30
31
32
33
34
# File 'lib/waves/layers/orm/migration.rb', line 26

def self.migration_files(range = nil)
  pattern = migration_directory / MIGRATION_FILE_PATTERN
  files = Dir[pattern].inject([]) do |m, path|
    m[File.basename(path).to_i] = path
    m
  end
  filtered = range ? files[range] : files
  filtered ? filtered.compact : []
end

.migration_name(name = nil) ⇒ Object

If the user doesn’t pass a name, defaults to “migration”



51
52
53
# File 'lib/waves/layers/orm/migration.rb', line 51

def self.migration_name(name=nil)
  name || 'migration'
end

.migration_template(orm, name = nil) ⇒ Object

Returns the path to the migration template file for the given ORM. orm can be a symbol or string



57
58
59
60
# File 'lib/waves/layers/orm/migration.rb', line 57

def self.migration_template(orm, name=nil)
  file = ( name || 'empty' ) + '.rb.erb'
  source = File.dirname(__FILE__) / :providers / orm / :migrations / file
end

.next_migration_versionObject

Use the supplied version number or determine the next in sequence based on the migration files in the migration directory



38
39
40
41
# File 'lib/waves/layers/orm/migration.rb', line 38

def self.next_migration_version
  version = ENV['version'] || latest_migration_version
  version.to_i + 1
end

.write_migration(context, source, destination) ⇒ Object

Takes an assigns hash as the Erubis context. Keys in the hash become instance variable names.



70
71
72
73
74
# File 'lib/waves/layers/orm/migration.rb', line 70

def self.write_migration(context, source, destination)
  code = Erubis::Eruby.new( File.read( source ) ).evaluate( context )
  puts "Creating #{destination}"
  File.write( destination, code )
end