Class: Argem::DbHelper

Inherits:
Object
  • Object
show all
Includes:
TR::CondUtils
Defined in:
lib/argem/db_helper.rb

Overview

Move ActiveRecord in Rakefile into here to allow creation of database file (sqlite) in anywhere

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ DbHelper

Returns a new instance of DbHelper.

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/argem/db_helper.rb', line 24

def initialize(config)
  @_config = config
  # which environment to use?
  raise Error, "Given config object must repond to method :env" if not @_config.respond_to?(:env)
  # where to write the migration file to?
  raise Error, "Given config object must repond to method :migration_root" if not @_config.respond_to?(:migration_root)
  # where to read/write the database.yml to?
  raise Error, "Given config object must repond to method :config_root" if not @_config.respond_to?(:config_root)
  # where to store the database file to ? (sqlite or similar only)
  raise Error, "Given config object must repond to method :db_root" if not @_config.respond_to?(:db_root)
  # where to store the generated model class to?
  raise Error, "Given config object must repond to method :model_root" if not @_config.respond_to?(:model_root)
end

Class Method Details

.is_active_record?(obj) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
# File 'lib/argem/db_helper.rb', line 15

def self.is_active_record?(obj)
  case obj
  when Class
    obj.ancestors.include?(ActiveRecord::Base)
  else
    obj.is_a?(ActiveRecord::Base)
  end
end

Instance Method Details

#clearObject



59
60
61
# File 'lib/argem/db_helper.rb', line 59

def clear
  ActiveRecord::Base.clear_all_connections!
end

#db_connectObject



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

def db_connect
  ActiveRecord::Base.establish_connection(db_config[@_config.env])           
end

#db_createObject



42
43
44
45
# File 'lib/argem/db_helper.rb', line 42

def db_create
  db_connect
  ActiveRecord::Base.connection.create_database(db_config[@_config.env]["database"]) if ActiveRecord::Base.connection.respond_to?(:create_database)
end

#db_migrateObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/argem/db_helper.rb', line 47

def db_migrate
  conf = db_config[@_config.env]
  if (conf["adapter"] =~ /sqlite3/) != nil
    path = conf["database"]
    FileUtils.mkdir_p(File.dirname(path)) if not File.exist?(File.dirname(path))
  end

  db_connect
  ActiveRecord::MigrationContext.new(local_migration_root).migrate
  dump_schema
end

#local_migration_rootObject



63
64
65
66
67
68
69
70
71
# File 'lib/argem/db_helper.rb', line 63

def local_migration_root
  if @_local_mig_root.nil?
    #@_local_mig_root = File.join(File.dirname(__FILE__),"..","..","..","..","db","migrate")
    @_local_mig_root = @_config.migration_root
    parent = File.dirname(@_local_mig_root)
    FileUtils.mkdir_p(parent) if not File.exist?(parent)
  end
  @_local_mig_root
end

#model_rootObject



73
74
75
# File 'lib/argem/db_helper.rb', line 73

def model_root
  @_config.model_root
end