Class: Framework::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/framework/application.rb

Constant Summary collapse

CONFIG_PATH =
"config/application.yml"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: nil, root: nil) {|_self| ... } ⇒ Application

Returns a new instance of Application.

Parameters:

  • env (String) (defaults to: nil)

    Environment from configuration file

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
18
# File 'lib/framework/application.rb', line 13

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

#envObject (readonly)

Returns the value of attribute env.



7
8
9
# File 'lib/framework/application.rb', line 7

def env
  @env
end

#logger=(value) ⇒ Object

Sets the attribute logger

Parameters:

  • value

    the value to set the attribute logger to.



8
9
10
# File 'lib/framework/application.rb', line 8

def logger=(value)
  @logger = value
end

#rootObject (readonly)

Returns the value of attribute root.



9
10
11
# File 'lib/framework/application.rb', line 9

def root
  @root
end

Class Method Details

.init! {|Framework::Application.new.init!| ... } ⇒ Object

Yields:



106
107
108
# File 'lib/framework/application.rb', line 106

def self.init!
  yield Framework::Application.new.init!
end

Instance Method Details

#configHash<String>

Returns:

  • (Hash<String>)


45
46
47
# File 'lib/framework/application.rb', line 45

def config
  @config || load_application_config
end

#create_database!(name = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/framework/application.rb', line 49

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

#databaseString

Returns Database name.

Returns:

  • (String)

    Database name



100
101
102
103
104
# File 'lib/framework/application.rb', line 100

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_configHash<String>

Returns:

  • (Hash<String>)


95
96
97
# File 'lib/framework/application.rb', line 95

def database_config
  @database_config || load_database_config
end

#db_connection(db_name = 'default') ⇒ Object



110
111
112
113
# File 'lib/framework/application.rb', line 110

def db_connection(db_name = 'default')
  ActiveRecord::Base.establish_connection(database_config[db_name][env])
  ActiveRecord::Base.connection
end

#drop_database!(name = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/framework/application.rb', line 67

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

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/framework/application.rb', line 22

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



86
87
88
# File 'lib/framework/application.rb', line 86

def migrate_database(version = nil)
  ActiveRecord::Migrator.migrate root.join("db/migrate"), version.try(:to_i)
end

#reload!Object



36
37
38
39
40
41
42
# File 'lib/framework/application.rb', line 36

def reload!
  @config = nil
  disappointment "Reloading #{env}"
  load_application_config
  load_application_files
  self
end

#rollback_database(steps = 1) ⇒ Object



90
91
92
# File 'lib/framework/application.rb', line 90

def rollback_database(steps = 1)
  ActiveRecord::Migrator.rollback root.join("db/migrate"), steps
end