framework 0.0.2 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17ea2a57355b495eff2e3798ed32103017673a3a
4
- data.tar.gz: 01739b92621f21c8122b34a5759e5f811615842f
3
+ metadata.gz: 3572d9060dff548fcfe8b378ad1b25d095b36170
4
+ data.tar.gz: 9ae54d8c3a6a6c97046987244abf89a70527d14a
5
5
  SHA512:
6
- metadata.gz: 9fe435f8328873b76556566d8fadd6ea49911dc07763f95fcc63124c0b8b8ad39a2bda4e9af9e745079226c83a3163653f30bc34ddb184d1240b2181f0f0e1f6
7
- data.tar.gz: 0a7d80ceeb19c004f9823fae991f36ac0d3c2d9a212d24fcb62b4fca3a229b2dbcc70ad3dc7ad956e162b4f99737bf9bf4ee33e9adf049ea9bcaa77e88dd19f9
6
+ metadata.gz: 7cdf544edfd49c3df6f37b8c4855e027170319417d29475db39c6ebece5573113a33d9be8ca057ba0bff845aa47eb3750ef2b1c29bac9ef6b77205b927dcaa0c
7
+ data.tar.gz: 1b33ce1beac4cfbdf6203b0536870411722f7e0ebf7ce5727ca025e5cd6451f36717fbbe2a43b1601ee84df6b17abd089739a9b5e5addbcf60b10f2f3159c13e
data/README.md CHANGED
@@ -69,7 +69,7 @@ Pretty similar to Rails (just a sample):
69
69
  | |____initializers
70
70
  | | |____time_zone.rb
71
71
  |____db
72
- | |____migrations
72
+ | |____migrate
73
73
  | | |____.keep
74
74
  | | |____20141128085924_create_tasks.rb
75
75
  | | |____20141128090211_create_users.rb
@@ -89,8 +89,8 @@ Configurations are stored in `config/application.yml`. Note, that as opposed to
89
89
  development: &common
90
90
  enable_logging: yes
91
91
  autoload_paths:
92
- - config/initializers
93
92
  - app/models
93
+ - app/managers
94
94
  default_timezone: 'Pacific Time (US & Canada)'
95
95
 
96
96
  test:
@@ -1,14 +1,18 @@
1
+ require 'framework/root'
2
+
1
3
  module Framework
2
4
  class Application
3
5
  CONFIG_PATH = "config/application.yml"
4
6
 
5
7
  attr_reader :env
6
8
  attr_accessor :logger
7
- delegate :hint, :note, to: :logger
9
+ attr_reader :root
10
+ delegate :hint, :note, :whisper, :disappointment, to: :logger
8
11
 
9
12
  # @param [String] env Environment from configuration file
10
- def initialize(env = nil)
13
+ def initialize(env: nil, root: nil)
11
14
  @env = env || Framework.env
15
+ @root = Root.new(root || Dir.pwd)
12
16
  Framework.app = self
13
17
  yield self if block_given?
14
18
  end
@@ -21,13 +25,21 @@ module Framework
21
25
  load_application_config
22
26
  load_database_config
23
27
  note "Loading #{env} environment (#{Framework::VERSION})"
24
- autoload
25
28
  note "Establishing database connection"
26
29
  establish_database_connection
30
+ require_dependencies 'config/initializers'
31
+ load_application_files
27
32
  note "Application has been initialized"
28
33
  self
29
34
  end
30
- alias_method :reload!, :init!
35
+
36
+ def reload!
37
+ @config = nil
38
+ disappointment "Reloading #{env}"
39
+ load_application_config
40
+ load_application_files
41
+ self
42
+ end
31
43
 
32
44
  # @return [Hash<String>]
33
45
  def config
@@ -72,11 +84,11 @@ module Framework
72
84
  end
73
85
 
74
86
  def migrate_database(version = nil)
75
- ActiveRecord::Migrator.migrate "db/migrations", version.try(:to_i)
87
+ ActiveRecord::Migrator.migrate root.join("db/migrate"), version.try(:to_i)
76
88
  end
77
89
 
78
90
  def rollback_database(steps = 1)
79
- ActiveRecord::Migrator.rollback "db/migrations", steps
91
+ ActiveRecord::Migrator.rollback root.join("db/migrate"), steps
80
92
  end
81
93
 
82
94
  # @return [Hash<String>]
@@ -88,7 +100,7 @@ module Framework
88
100
  def database
89
101
  adapter = database_config['default'][env]['adapter']
90
102
  database = database_config['default'][env]['database']
91
- adapter == 'sqlite3' ? "db/sqlite/#{env}/#{database}.db" : database
103
+ adapter == 'sqlite3' ? root.join("db/sqlite/#{env}/#{database}.db") : database
92
104
  end
93
105
 
94
106
  def self.init!
@@ -102,6 +114,64 @@ module Framework
102
114
 
103
115
  private
104
116
 
117
+ # Autoloads all app-specific files
118
+ def load_application_files
119
+ if %w(development test).include?(env.to_s)
120
+ config['autoload_paths'].each(&method(:autoreload_constants))
121
+ autoreload_yml
122
+ end
123
+ config['autoload_paths'].each(&method(:require_dependencies))
124
+ end
125
+
126
+ def autoreload_yml(path=root)
127
+ autoreload(path, 'yml') { @yml_reloaded = true } unless @yml_reloaded
128
+ end
129
+
130
+ def autoreload(path=root, ext, &block)
131
+ files = Dir[root.join(path, "/**/*.#{ext}")]
132
+
133
+ reloader = ActiveSupport::FileUpdateChecker.new(files) do
134
+ whisper "Reloading .#{ext} files"
135
+ block.try(:call, files)
136
+ Framework.app.reload!
137
+ end
138
+
139
+ ActionDispatch::Callbacks.to_prepare do
140
+ reloader.execute_if_updated
141
+ end
142
+ end
143
+
144
+ def autoreload_constants(path)
145
+ return if reloadable_paths.include?(path)
146
+
147
+ ActiveSupport::Dependencies.autoload_paths += [root.join(path)]
148
+ absolute_path = Pathname.new(root.join(path))
149
+
150
+ autoreload(path, 'rb') do |files|
151
+ files.each do |file_path|
152
+ pathname = Pathname.new(file_path)
153
+
154
+ # Get Const Name
155
+ const_name = pathname.basename.to_s.sub('.rb', '').camelize.to_sym
156
+ full_name = pathname.relative_path_from(absolute_path).sub('.rb', '').to_s.camelize
157
+
158
+ # Get Module Name
159
+ relative_file_path = Pathname.new(pathname.dirname).relative_path_from(absolute_path).to_s
160
+
161
+ if Object.const_defined?(full_name)
162
+ if relative_file_path == '.'
163
+ Object.send :remove_const, const_name
164
+ else
165
+ module_name = relative_file_path.camelize
166
+ module_name.constantize.send(:remove_const, const_name)
167
+ end
168
+ end
169
+ end
170
+ end
171
+
172
+ reloadable_paths << path
173
+ end
174
+
105
175
  def establish_database_connection
106
176
  if database_config
107
177
  database_config.each do |_, db_config|
@@ -122,34 +192,19 @@ module Framework
122
192
  end
123
193
  end
124
194
 
125
- # Autoloads all app-specific files
126
- def autoload
127
- config['autoload_paths'].each do |path|
128
- if path.end_with?('.rb')
129
- load(path)
130
- else
131
- load_path(File.join(path, 'concerns'))
132
-
133
- Dir["#{path}/**/*.rb"].each do |path_to_load|
134
- load(path_to_load) unless path_to_load.include?('concerns/')
135
- end
136
- end
137
- end
138
- end
139
-
140
195
  # @return [Hash]
141
196
  def load_application_config
142
- @config = YAML.load_file(CONFIG_PATH)[env]
197
+ @config = YAML.load_file(root.join(CONFIG_PATH))[env]
143
198
  end
144
199
 
145
200
  # @return [Hash]
146
201
  def load_database_config
147
- @database_config = YAML.load_file('config/databases.yml')
202
+ @database_config = YAML.load_file(root.join('config/databases.yml'))
148
203
  end
149
204
 
150
- # @param [String] path Relative to project root
205
+ # @param [String] path
151
206
  def load_path(path)
152
- Dir["#{path}/**/*.rb"].each(&method(:load))
207
+ Dir["#{path}/**/*.rb"].each(&method(:require_dependency))
153
208
  end
154
209
 
155
210
  # @return [IO, nil]
@@ -161,5 +216,23 @@ module Framework
161
216
  def logger
162
217
  @logger ||= Framework::Logger.new(log_level)
163
218
  end
219
+
220
+ def reloadable_paths
221
+ @reloadable_paths ||= []
222
+ end
223
+
224
+ def require_dependencies(autoload_path)
225
+ path = root.join(autoload_path)
226
+
227
+ if path.end_with?('.rb')
228
+ require_dependency(path)
229
+ else
230
+ load_path(File.join(path, 'concerns'))
231
+
232
+ Dir["#{path}/**/*.rb"].each do |path_to_load|
233
+ require_dependency(path_to_load) unless path_to_load.include?('concerns/')
234
+ end
235
+ end
236
+ end
164
237
  end
165
238
  end
@@ -7,16 +7,16 @@ module Framework
7
7
  module ClassMethods
8
8
 
9
9
  # @override
10
- def inherited(child_class)
11
- super
12
-
13
- unless child_class == ::ActiveRecord::SchemaMigration
14
- if (chunks = child_class.name.split('::')).many?
15
- child_class.store_full_sti_class = false
16
- child_class.use_database(chunks.first.downcase)
17
- end
18
- end
19
- end
10
+ # def inherited(child_class)
11
+ # super
12
+ #
13
+ # unless child_class == ::ActiveRecord::SchemaMigration
14
+ # if (chunks = child_class.name.split('::')).many?
15
+ # child_class.store_full_sti_class = false
16
+ # child_class.use_database(chunks.first.downcase)
17
+ # end
18
+ # end
19
+ # end
20
20
 
21
21
  # Makes your model use different databases
22
22
  # @param [String, Symbol] db_name
@@ -26,8 +26,8 @@ module Framework
26
26
  env = env.to_s
27
27
  db_name = db_name.to_s
28
28
 
29
- self.abstract_class = Framework.env != 'test'
30
- self.table_name = self.name.split('::').last.tableize if self.superclass == ::ActiveRecord::Base
29
+ # self.abstract_class = Framework.env != 'test'
30
+ # self.table_name = self.name.split('::').last.tableize if self.superclass == ::ActiveRecord::Base
31
31
  establish_connection(Framework.app.database_config[db_name][env])
32
32
  end
33
33
  end
@@ -20,8 +20,8 @@ module Framework
20
20
  create_file 'lib/.keep'
21
21
 
22
22
  empty_directory 'db'
23
- empty_directory 'db/migrations'
24
- create_file 'db/migrations/.keep'
23
+ empty_directory 'db/migrate'
24
+ create_file 'db/migrate/.keep'
25
25
  end
26
26
 
27
27
  def create_application_config
@@ -30,7 +30,6 @@ module Framework
30
30
  development: &common
31
31
  enable_logging: yes
32
32
  autoload_paths:
33
- - config/initializers
34
33
  - app/models
35
34
  default_timezone: 'Pacific Time (US & Canada)'
36
35
 
@@ -10,7 +10,7 @@ module Framework
10
10
  @db_name = db_name.underscore
11
11
  @migration_name = migration_name.underscore
12
12
  @timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
13
- @path = "db/migrations/#{@timestamp}_#@migration_name.rb"
13
+ @path = "db/migrate/#{@timestamp}_#@migration_name.rb"
14
14
  end
15
15
 
16
16
  def generate
@@ -44,7 +44,7 @@ module Framework
44
44
  end
45
45
 
46
46
  def load_migrations
47
- Dir["./db/migrations/**/*.rb"].each(&method(:load))
47
+ Dir["./db/migrate/**/*.rb"].each(&method(:load))
48
48
  rescue
49
49
  raise 'An error was occurred'
50
50
  end
@@ -0,0 +1,24 @@
1
+ module Framework
2
+ class Root < String
3
+
4
+ # @param path [String]
5
+ def initialize(path=Dir.pwd)
6
+ super(path)
7
+ self.freeze
8
+ end
9
+
10
+ # @param args [Array<String>]
11
+ # @return [String]
12
+ def join(*args)
13
+ File.join(self, *args)
14
+ end
15
+
16
+ def inspect
17
+ to_s
18
+ end
19
+
20
+ def to_s
21
+ self
22
+ end
23
+ end
24
+ end
@@ -2,13 +2,13 @@ desc "Runs irb console and initializes the application"
2
2
  task :console, :env do |_, args|
3
3
  require 'irb'
4
4
 
5
- unless env = ENV['FRAMEWORK_ENV']
5
+ unless env = ENV['FRAMEWORK_ENV'] || ENV['RAILS_ENV']
6
6
  env = args[:env] || Framework::DEFAULT_ENV
7
7
  end
8
8
 
9
9
  system "mkdir -p #{Dir.pwd}/db/#{env}"
10
10
 
11
- Framework::Application.new(env) do |app|
11
+ Framework::Application.new(env: env) do |app|
12
12
  app.init!
13
13
  app.hint("Use `Framework.app` variable to deal with application API")
14
14
 
@@ -28,22 +28,22 @@ namespace :db do
28
28
  task :create, :name do |_, args|
29
29
  env = 'test'
30
30
  puts "Loading #{env.inspect}"
31
- Framework::Application.new(env).create_database!(args[:name])
31
+ Framework::Application.new(env: env).create_database!(args[:name])
32
32
  end
33
33
 
34
34
  desc "Drops test database"
35
35
  task :drop, :name do |_, args|
36
36
  env = 'test'
37
37
  puts "Loading #{env.inspect}"
38
- Framework::Application.new(env).drop_database!(args[:name])
38
+ Framework::Application.new(env: env).drop_database!(args[:name])
39
39
  end
40
40
 
41
41
  desc "Runs test migrations"
42
42
  task :migrate do
43
43
  env = 'test'
44
44
  puts "Loading #{env.inspect}"
45
- Framework::Application.new(env).init!
46
- ActiveRecord::Migrator.migrate('db/migrations/')
45
+ Framework::Application.new(env: env).init!
46
+ ActiveRecord::Migrator.migrate('db/migrate/')
47
47
  end
48
48
  end
49
49
  end
@@ -1,3 +1,3 @@
1
1
  module Framework
2
- VERSION = '0.0.2'.freeze
2
+ VERSION = '0.0.5'.freeze
3
3
  end
data/lib/framework.rb CHANGED
@@ -21,7 +21,12 @@ module Framework
21
21
  end
22
22
 
23
23
  def self.env
24
- @app ? @app.env : (ENV['FRAMEWORK_ENV'] || DEFAULT_ENV)
24
+ @app ? @app.env : (ENV['FRAMEWORK_ENV'] || ENV['RAILS_ENV'] || DEFAULT_ENV)
25
25
  end
26
- end
27
26
 
27
+ # Returns current work dir String
28
+ # @return [Framework::Root]
29
+ def self.root
30
+ @app.try(:root)
31
+ end
32
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: framework
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergei Nikolaevich Zinin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-28 00:00:00.000000000 Z
11
+ date: 2014-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,40 +16,40 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '4.0'
19
+ version: '3.2'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 4.0.0
22
+ version: 3.2.16
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: '4.0'
29
+ version: '3.2'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 4.0.0
32
+ version: 3.2.16
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: activesupport
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '4.0'
39
+ version: '3.2'
40
40
  - - ">="
41
41
  - !ruby/object:Gem::Version
42
- version: 4.0.0
42
+ version: 3.2.16
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - "~>"
48
48
  - !ruby/object:Gem::Version
49
- version: '4.0'
49
+ version: '3.2'
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
- version: 4.0.0
52
+ version: 3.2.16
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: awesome_print
55
55
  requirement: !ruby/object:Gem::Requirement
@@ -172,6 +172,7 @@ files:
172
172
  - lib/framework/logger.rb
173
173
  - lib/framework/migration.rb
174
174
  - lib/framework/rake.rb
175
+ - lib/framework/root.rb
175
176
  - lib/framework/tasks/console.rake
176
177
  - lib/framework/tasks/db.rake
177
178
  - lib/framework/tasks/engine/environment.rake