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.
16 17 18 19 20 21 |
# File 'lib/framework/application.rb', line 16 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.
10 11 12 |
# File 'lib/framework/application.rb', line 10 def env @env end |
#logger=(value) ⇒ Object
Sets the attribute logger
11 12 13 |
# File 'lib/framework/application.rb', line 11 def logger=(value) @logger = value end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
12 13 14 |
# File 'lib/framework/application.rb', line 12 def root @root end |
Class Method Details
.init! {|Framework::Application.new.init!| ... } ⇒ Object
109 110 111 |
# File 'lib/framework/application.rb', line 109 def self.init! yield Framework::Application.new.init! end |
Instance Method Details
#config ⇒ Hash<String>
48 49 50 |
# File 'lib/framework/application.rb', line 48 def config @config || load_application_config end |
#create_database!(name = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/framework/application.rb', line 52 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.
103 104 105 106 107 |
# File 'lib/framework/application.rb', line 103 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>
98 99 100 |
# File 'lib/framework/application.rb', line 98 def database_config @database_config || load_database_config end |
#db_connection(db_name = 'default') ⇒ Object
113 114 115 116 |
# File 'lib/framework/application.rb', line 113 def db_connection(db_name = 'default') ActiveRecord::Base.establish_connection(database_config[db_name][env]) ActiveRecord::Base.connection end |
#drop_database!(name = nil) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/framework/application.rb', line 70 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
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/framework/application.rb', line 25 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
89 90 91 |
# File 'lib/framework/application.rb', line 89 def migrate_database(version = nil) ActiveRecord::Migrator.migrate root.join("db/migrate"), version.try(:to_i) end |
#reload! ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/framework/application.rb', line 39 def reload! @config = nil disappointment "Reloading #{env}" load_application_config load_application_files self end |
#rollback_database(steps = 1) ⇒ Object
93 94 95 |
# File 'lib/framework/application.rb', line 93 def rollback_database(steps = 1) ActiveRecord::Migrator.rollback root.join("db/migrate"), steps end |