Class: Framework::Application
- Inherits:
-
Object
- Object
- Framework::Application
- Defined in:
- lib/framework/application.rb
Constant Summary collapse
- CONFIG_PATH =
"config/application.yml"
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#logger ⇒ Object
writeonly
Sets the attribute logger.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Class Method Summary collapse
Instance Method Summary collapse
- #config ⇒ Hash<String>
- #create_database!(name = nil) ⇒ Object
-
#database ⇒ String
Database name.
- #database_config ⇒ Hash<String>
- #db_connection(db_name = 'default') ⇒ Object
- #drop_database!(name = nil) ⇒ Object
-
#init! ⇒ Application
Entry point for the app.
-
#initialize(env: nil, root: nil) {|_self| ... } ⇒ Application
constructor
A new instance of Application.
- #migrate_database(version = nil) ⇒ Object
- #reload! ⇒ Object
- #rollback_database(steps = 1) ⇒ Object
Constructor Details
#initialize(env: nil, root: nil) {|_self| ... } ⇒ Application
Returns a new instance of Application.
17 18 19 20 21 22 |
# File 'lib/framework/application.rb', line 17 def initialize(env: nil, root: nil) @env = env || Framework.env @root = Root.new(root || Dir.pwd) Framework.app = self yield self if block_given? end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
11 12 13 |
# File 'lib/framework/application.rb', line 11 def env @env end |
#logger=(value) ⇒ Object
Sets the attribute logger
12 13 14 |
# File 'lib/framework/application.rb', line 12 def logger=(value) @logger = value end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
13 14 15 |
# File 'lib/framework/application.rb', line 13 def root @root end |
Class Method Details
.init! {|Framework::Application.new.init!| ... } ⇒ Object
110 111 112 |
# File 'lib/framework/application.rb', line 110 def self.init! yield Framework::Application.new.init! end |
Instance Method Details
#config ⇒ Hash<String>
49 50 51 |
# File 'lib/framework/application.rb', line 49 def config @config || load_application_config end |
#create_database!(name = nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/framework/application.rb', line 53 def create_database!(name = nil) name ||= 'default' cfg = database_config[name][env] case cfg['adapter'] when 'postgresql' establish_postgres_connection(name) ActiveRecord::Base.connection.create_database(cfg['database']) when 'sqlite3' raise 'Database already exists' if File.exist?(cfg['database']) establish_database_connection else raise "Unknown adapter '#{cfg['adapter']}'" end puts "The database #{cfg['database']} has been successfully created" end |
#database ⇒ String
Returns Database name.
104 105 106 107 108 |
# File 'lib/framework/application.rb', line 104 def database adapter = database_config['default'][env]['adapter'] database = database_config['default'][env]['database'] adapter == 'sqlite3' ? root.join("db/sqlite/#{env}/#{database}.db") : database end |
#database_config ⇒ Hash<String>
99 100 101 |
# File 'lib/framework/application.rb', line 99 def database_config @database_config || load_database_config end |
#db_connection(db_name = 'default') ⇒ Object
114 115 116 117 |
# File 'lib/framework/application.rb', line 114 def db_connection(db_name = 'default') ActiveRecord::Base.establish_connection(database_config[db_name][env]) ActiveRecord::Base.connection end |
#drop_database!(name = nil) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/framework/application.rb', line 71 def drop_database!(name = nil) name ||= 'default' cfg = database_config[name][env] case cfg['adapter'] when 'postgresql' establish_postgres_connection(name) ActiveRecord::Base.connection.drop_database(cfg['database']) when 'sqlite3' require 'pathname' path = Pathname.new(cfg['database']).to_s FileUtils.rm(path) if File.exist?(path) else raise "Unknown adapter '#{cfg['adapter']}'" end puts "The database #{database_config[name][env]['database']} has been successfully dropped" end |
#init! ⇒ Application
Entry point for the app
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/framework/application.rb', line 26 def init! @logger = @config = @database_config = nil load_application_config load_database_config note "Loading #{env} environment (#{Framework::VERSION})" note "Establishing database connection" establish_database_connection require_dependencies 'config/initializers' load_application_files note "Application has been initialized" self end |
#migrate_database(version = nil) ⇒ Object
90 91 92 |
# File 'lib/framework/application.rb', line 90 def migrate_database(version = nil) ActiveRecord::Migrator.migrate root.join("db/migrate"), version.try(:to_i) end |
#reload! ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/framework/application.rb', line 40 def reload! @config = nil disappointment "Reloading #{env}" load_application_config load_application_files self end |
#rollback_database(steps = 1) ⇒ Object
94 95 96 |
# File 'lib/framework/application.rb', line 94 def rollback_database(steps = 1) ActiveRecord::Migrator.rollback root.join("db/migrate"), steps end |