Module: Rtt::Storage

Included in:
Rtt
Defined in:
lib/rtt/storage.rb

Instance Method Summary collapse

Instance Method Details

#config(env = :production) ⇒ Object



21
22
23
# File 'lib/rtt/storage.rb', line 21

def config(env = :production)
  @config ||= YAML::load_file(File.join(File.dirname(__FILE__), '..', '..', 'db', 'config.yml'))[env.to_s]
end

#database_fileObject



9
10
11
# File 'lib/rtt/storage.rb', line 9

def database_file
  File.expand_path(File.join(ENV['HOME'], '.rtt', config['database']))
end

#export(filename) ⇒ Object



13
14
15
# File 'lib/rtt/storage.rb', line 13

def export filename
  FileUtils.cp(database_file, filename)
end

#import(filename) ⇒ Object



17
18
19
# File 'lib/rtt/storage.rb', line 17

def import filename
  FileUtils.cp(filename, database_file)
end

#init(env = :production) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rtt/storage.rb', line 25

def init(env = :production)
  database_dir = File.dirname(database_file)
  FileUtils.mkdir_p(database_dir) unless FileTest::directory?(database_dir)
  ActiveRecord::Base.establish_connection(config(env).merge('database' => database_file))
  log_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'log'))
  Dir::mkdir(log_dir) unless FileTest::directory?(log_dir)
  ActiveRecord::Base.logger = Logger.new(File.open(File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'log', 'database.log')), 'a'))
  silence_stream(STDOUT) do
    require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'db', 'schema.rb')) unless tables_exists?
  end
end

#tables_exists?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rtt/storage.rb', line 37

def tables_exists?
  %w(projects clients tasks users).any? { |t| ActiveRecord::Base.connection.tables.include?(t) }
end