Class: RakeAR

Inherits:
Object
  • Object
show all
Defined in:
lib/rake_ar.rb,
lib/rake_ar/version.rb

Constant Summary collapse

VERSION =
'1.0.0'

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ RakeAR

Returns a new instance of RakeAR.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rake_ar.rb', line 6

def initialize(settings = {})
  @settings = {
    connect_file:   "#{Dir.pwd}/db/connection.rb",
    migration_path: "#{Dir.pwd}/db/migrate/",
    seed_file:      "#{Dir.pwd}/db/seeds.rb",
    schema_file:    "#{Dir.pwd}/db/schema.rb",
    models_path:    "#{Dir.pwd}/app/models"
  }.merge(settings)

  FileUtils.mkdir_p(@settings[:migration_path])
end

Instance Method Details

#clear_dbObject



46
47
48
49
50
# File 'lib/rake_ar.rb', line 46

def clear_db
  ActiveRecord::Base.descendants.each do |model|
    model.delete_all
  end
end

#connect_dbObject



18
19
20
21
22
# File 'lib/rake_ar.rb', line 18

def connect_db
  @settings[:connect_file].tap do |connection|
    require connection
  end
end

#create_migrationObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rake_ar.rb', line 64

def create_migration
  name = ENV['NAME']
  abort 'No NAME specified. Use `rake db:create_migration NAME=create_users`' if !name

  version = ENV['VERSION'] || Time.now.utc.strftime('%Y%m%d%H%M%S') 
  migration_file = "#{version}_#{name}.rb"
  migration_name = name.gsub(/_(.)/) { $1.upcase }.gsub(/^(.)/) { $1.upcase }

  File.open("#{@settings[:migration_path]}/#{migration_file}", 'w') do |migration|
    migration << (<<-EOS).gsub('      ', '')
    class #{migration_name} < ActiveRecord::Migration
      def self.up
      end

      def self.down
      end
    end
    EOS
  end
end

#drop_dbObject



52
53
54
55
56
57
# File 'lib/rake_ar.rb', line 52

def drop_db
  (ActiveRecord::Base.descendants << 'schema_migration').each do |table|
    sql = "DROP TABLE #{table.to_s.pluralize.downcase};"
    ActiveRecord::Base.connection.execute(sql) rescue 1
  end
end

#dump_schemaObject



32
33
34
35
36
# File 'lib/rake_ar.rb', line 32

def dump_schema
  File.open(@settings[:schema_file], 'w:utf-8') do |schema_file|
    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, schema_file)
  end 
end

#load_modelsObject



24
25
26
27
28
29
30
# File 'lib/rake_ar.rb', line 24

def load_models
  "#{@settings[:models_path]}/*.rb".tap do |path|
    Dir[path].each do |model|
      require model
    end
  end
end

#load_schemaObject



38
39
40
# File 'lib/rake_ar.rb', line 38

def load_schema
  load(@settings[:schema_file]) if File.exists? @settings[:schema_file]
end

#migrate_dbObject



59
60
61
62
# File 'lib/rake_ar.rb', line 59

def migrate_db
  version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
  ActiveRecord::Migrator.migrate(@settings[:migration_path], version)
end

#seed_dbObject



42
43
44
# File 'lib/rake_ar.rb', line 42

def seed_db
  load(@settings[:seed_file]) if File.exists? @settings[:seed_file]
end