class MockleyCrew::Database
Attributes
filename[RW]
Public Class Methods
clean_database_files(seconds = 60)
click to toggle source
# File lib/mockley_crew/database.rb, line 91 def clean_database_files seconds = 60 MockleyCrew.configuration.database_files.each do |filename| filename_parts = File.basename(filename, ".db").split("_"); if Time.zone.now.to_i - filename_parts.first.to_i >= seconds or filename_parts.length < 2 self.remove_file_by_full_filename(filename) end end end
commit_transactions()
click to toggle source
# File lib/mockley_crew/database.rb, line 47 def commit_transactions if ActiveRecord::Base.connected? if ActiveRecord::Base.connection.transaction_open? ActiveRecord::Base.connection.commit_transaction end else # raise "Cannot commit transactions. You are disconnected from the database." end end
connect(args = {})
click to toggle source
# File lib/mockley_crew/database.rb, line 4 def connect args = {} if args["filename"].present? args["database"] = "#{MockleyCrew.configuration.database_files_path}#{args["filename"]}" end self.commit_transactions ActiveRecord::Base.connection_handler.establish_connection({ adapter: "sqlite3", database: args["database"], host: args["host"] || File.basename(args["database"], ".*"), username: "crew_master", password: "crew_man" }, { thread_name: args["thread_name"] || "mockley_crew_default" }) end
create()
click to toggle source
# File lib/mockley_crew/database.rb, line 120 def create db = self.new db.save db end
create_default_database()
click to toggle source
# File lib/mockley_crew/database.rb, line 64 def create_default_database unless File.exists?(MockleyCrew.configuration.default_database_path) Database.connect("database" => MockleyCrew.configuration.default_database_path) Database.migrate end end
delete_default_databse()
click to toggle source
# File lib/mockley_crew/database.rb, line 71 def delete_default_databse if File.exists?(MockleyCrew.configuration.default_database_path) File.delete(MockleyCrew.configuration.default_database_path) terminate_thread "mockley_crew_default" end end
disconnect()
click to toggle source
# File lib/mockley_crew/database.rb, line 28 def disconnect self.commit_transactions if ActiveRecord::Base.connected? ActiveRecord::Base.connection.close ActiveRecord::Base.connection.disconnect! self.restore_default_connection else # raise "Cannot disconnect. You are already disconnected from the database." end end
find_by_filename(filename)
click to toggle source
# File lib/mockley_crew/database.rb, line 111 def find_by_filename filename db = self.new(filename: filename) if db.exists? return db else return nil end end
generate_unique_code()
click to toggle source
# File lib/mockley_crew/database.rb, line 104 def generate_unique_code loop do code = SecureRandom.hex(10) break code unless MockleyCrew.configuration.database_codes.include?(code) end end
migrate()
click to toggle source
# File lib/mockley_crew/database.rb, line 22 def migrate ActiveRecord::Migration.verbose = false ActiveRecord::Base.connection.migration_context.migrate ActiveRecord::Migration.verbose = true end
new(args = {})
click to toggle source
# File lib/mockley_crew/database.rb, line 129 def initialize args = {} args.each do |k, v| self.send("#{k}=".to_sym, v) end unless self.filename.present? self.filename = self.class.next_name end end
next_name()
click to toggle source
# File lib/mockley_crew/database.rb, line 100 def next_name "#{Time.zone.now.to_i}_#{generate_unique_code}.db" end
remove_file_by_filename(filename)
click to toggle source
# File lib/mockley_crew/database.rb, line 87 def remove_file_by_filename filename self.remove_file_by_full_filename(MockleyCrew.configuration.database_files_path + filename) end
remove_file_by_full_filename(filename)
click to toggle source
# File lib/mockley_crew/database.rb, line 83 def remove_file_by_full_filename filename File.delete(filename) end
reset_default_database()
click to toggle source
# File lib/mockley_crew/database.rb, line 78 def reset_default_database delete_default_databse create_default_database end
restore_default_connection()
click to toggle source
# File lib/mockley_crew/database.rb, line 41 def restore_default_connection ActiveRecord::Base.connection_handler.establish_connection( ActiveRecord::Base.configurations[Rails.env], no_reaper: true ) end
terminate_thread(filename)
click to toggle source
# File lib/mockley_crew/database.rb, line 57 def terminate_thread filename threads = Thread.list.select { |t| t["thread_name"] == filename } threads.each do |t| t.kill end end
Public Instance Methods
connect()
click to toggle source
# File lib/mockley_crew/database.rb, line 166 def connect self.class.connect( "thread_name" => @filename, "filename" => @filename ) end
Also aliased as: on
create_database_file()
click to toggle source
# File lib/mockley_crew/database.rb, line 159 def create_database_file self.class.create_default_database unless File.exists?(MockleyCrew.configuration.default_database_path) FileUtils.mkdir_p(File.dirname(full_file_path)) FileUtils.cp MockleyCrew.configuration.default_database_path, full_file_path end
destroy()
click to toggle source
# File lib/mockley_crew/database.rb, line 179 def destroy self.disconnect self.class.remove_file_by_filename(self.filename) self.class.terminate_thread(self.filename) end
disconnect()
click to toggle source
# File lib/mockley_crew/database.rb, line 174 def disconnect self.class.disconnect end
Also aliased as: off
exists?()
click to toggle source
# File lib/mockley_crew/database.rb, line 139 def exists? File.exists?(full_file_path) end
full_file_path()
click to toggle source
# File lib/mockley_crew/database.rb, line 155 def full_file_path "#{MockleyCrew.configuration.database_files_path}#{@filename}" end
get_created_at()
click to toggle source
# File lib/mockley_crew/database.rb, line 147 def get_created_at Time.at(File.basename(@filename, ".*").split("_").first.to_i) end
get_name()
click to toggle source
# File lib/mockley_crew/database.rb, line 143 def get_name File.basename(@filename, ".*").split("_").last end
save()
click to toggle source
# File lib/mockley_crew/database.rb, line 151 def save self.create_database_file unless File.exists?(full_file_path) end