ru.Bee 1.3.1 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/rubee.rb CHANGED
@@ -1,14 +1,18 @@
1
- require "singleton"
2
- require "bundler/setup"
1
+ require 'singleton'
2
+ require 'bundler/setup'
3
3
  require 'bundler'
4
4
 
5
- Bundler.require(:default) rescue nil
5
+ begin
6
+ Bundler.require(:default)
7
+ rescue StandardError
8
+ nil
9
+ end
6
10
 
7
11
  module Rubee
8
12
  APP_ROOT = File.expand_path(Dir.pwd) unless defined?(APP_ROOT)
9
13
  IMAGE_DIR = File.join(APP_ROOT, 'images') unless defined?(IMAGE_DIR)
10
14
  PROJECT_NAME = File.basename(APP_ROOT) unless defined?(PROJECT_NAME)
11
- VERSION = '1.3.0'
15
+ VERSION = '1.3.3'
12
16
 
13
17
  class Application
14
18
  include Singleton
@@ -19,16 +23,16 @@ module Rubee
19
23
  # register images paths
20
24
  request = Rack::Request.new(env)
21
25
  # Add default path for images
22
- Router.draw { |route| route.get "/images/{path}", to: "base#image", namespace: "Rubee"}
26
+ Router.draw { |route| route.get('/images/{path}', to: 'base#image', namespace: 'Rubee') }
23
27
  # define route
24
28
  route = Router.route_for(request)
25
29
  # init controller class
26
- return [404, { "content-type" => "text/plain" }, ["Route not found"]] unless route
30
+ return [404, { 'content-type' => 'text/plain' }, ['Route not found']] unless route
27
31
 
28
- if route[:namespace]
29
- controller_class = "#{route[:namespace]}::#{route[:controller].capitalize}Controller"
32
+ controller_class = if route[:namespace]
33
+ "#{route[:namespace]}::#{route[:controller].capitalize}Controller"
30
34
  else
31
- controller_class = "#{route[:controller].capitalize}Controller"
35
+ "#{route[:controller].capitalize}Controller"
32
36
  end
33
37
  # instantiate controller
34
38
  controller = Object.const_get(controller_class).new(request, route)
@@ -44,15 +48,15 @@ module Rubee
44
48
 
45
49
  @configuraiton = {
46
50
  development: {
47
- database_url: "",
48
- port: 7000
51
+ database_url: '',
52
+ port: 7000,
49
53
  },
50
54
  production: {},
51
- test: {}
55
+ test: {},
52
56
  }
53
57
 
54
58
  class << self
55
- def setup(env)
59
+ def setup(_env)
56
60
  yield(self)
57
61
  end
58
62
 
@@ -64,10 +68,10 @@ module Rubee
64
68
  @configuraiton[args[:env].to_sym][:async_adapter] = args[:async_adapter]
65
69
  end
66
70
 
67
- def method_missing(method_name, *args, &block)
68
- if method_name.to_s.start_with?("get_")
69
- @configuraiton[ENV['RACK_ENV']&.to_sym || :development]&.[](method_name.to_s.delete_prefix("get_").to_sym)
70
- end
71
+ def method_missing(method_name, *_args)
72
+ return unless method_name.to_s.start_with?('get_')
73
+
74
+ @configuraiton[ENV['RACK_ENV']&.to_sym || :development]&.[](method_name.to_s.delete_prefix('get_').to_sym)
71
75
  end
72
76
 
73
77
  def envs
@@ -79,7 +83,7 @@ module Rubee
79
83
  class Router
80
84
  include Singleton
81
85
 
82
- HTTP_METHODS = [:get, :post, :put, :patch, :delete, :head, :connect, :options, :trace].freeze
86
+ HTTP_METHODS = %i[get post put patch delete head connect options trace].freeze
83
87
 
84
88
  attr_reader :request, :routes
85
89
 
@@ -92,19 +96,19 @@ module Rubee
92
96
 
93
97
  def route_for(request)
94
98
  puts request.request_method
95
- method = (request.params["_method"] || request.request_method).downcase.to_sym
99
+ method = (request.params['_method'] || request.request_method).downcase.to_sym
96
100
  @routes.find do |route|
97
101
  return route if request.path == route[:path] && request.request_method&.downcase&.to_sym == route[:method]
98
102
 
99
103
  pattern = route[:path].gsub(/{.*?}/, '([^/]+)')
100
- regex = %r{^#{pattern}$}
104
+ regex = /^#{pattern}$/
101
105
  regex.match?(request.path) && method.to_s == route[:method].to_s
102
106
  end
103
107
  end
104
108
 
105
109
  def set_route(path, to:, method: __method__, **args)
106
- controller, action = to.split("#")
107
- @routes.delete_if { |route| route[:path] == path && route[:method] == method }
110
+ controller, action = to.split('#')
111
+ @routes.delete_if { |route| route[:path] == path && route[:method] == method }
108
112
  @routes << { path:, controller:, action:, method:, **args }
109
113
  end
110
114
 
@@ -116,10 +120,9 @@ module Rubee
116
120
  end
117
121
  end
118
122
 
119
-
120
123
  class Autoload
121
124
  class << self
122
- def call(black_list=[])
125
+ def call(black_list = [])
123
126
  # autoload all rbs
124
127
  root_directory = File.dirname(__FILE__)
125
128
  priority_order_require(root_directory, black_list)
@@ -150,19 +153,20 @@ module Rubee
150
153
  end
151
154
  # app config and routes
152
155
  lib = PROJECT_NAME == 'rubee' ? 'lib/' : ''
153
- require_relative File.join(APP_ROOT, lib, "config/base_configuration") unless black_list.include?('base_configuration.rb')
156
+ unless black_list.include?('base_configuration.rb')
157
+ require_relative File.join(APP_ROOT, lib,
158
+ 'config/base_configuration')
159
+ end
154
160
  # This is necessary prerequisitedb init step
155
- unless defined?(Rubee::SequelObject::DB)
156
- if PROJECT_NAME == 'rubee'
157
- Rubee::Configuration.setup(env=:test) do |config|
158
- config.database_url = { url: "sqlite://lib/tests/test.db", env: }
159
- end
161
+ if !defined?(Rubee::SequelObject::DB) && (PROJECT_NAME == 'rubee')
162
+ Rubee::Configuration.setup(env = :test) do |config|
163
+ config.database_url = { url: 'sqlite://lib/tests/test.db', env: }
160
164
  end
161
165
  end
162
166
 
163
- require_relative File.join(APP_ROOT, lib, "config/routes") unless black_list.include?('routes.rb')
167
+ require_relative File.join(APP_ROOT, lib, 'config/routes') unless black_list.include?('routes.rb')
164
168
  # rubee extensions
165
- Dir[File.join(root_directory, "rubee/extensions/**", '*.rb')].each do |file|
169
+ Dir[File.join(root_directory, 'rubee/extensions/**', '*.rb')].each do |file|
166
170
  require_relative file unless black_list.include?("#{file}.rb")
167
171
  end
168
172
  # rubee controllers
@@ -172,10 +176,19 @@ module Rubee
172
176
  Dir[File.join(root_directory, 'rubee/controllers/extensions/**', '*.rb')].each do |file|
173
177
  require_relative file unless black_list.include?("#{file}.rb")
174
178
  end
175
- require_relative File.join(root_directory, "rubee/controllers/base_controller") unless black_list.include?('base_controller.rb')
179
+ unless black_list.include?('base_controller.rb')
180
+ require_relative File.join(root_directory,
181
+ 'rubee/controllers/base_controller')
182
+ end
176
183
  # rubee models
177
- require_relative File.join(root_directory, "rubee/models/database_objectable") unless black_list.include?('database_objectable.rb')
178
- require_relative File.join(root_directory, "rubee/models/sequel_object") unless black_list.include?('sequel_object.rb')
184
+ unless black_list.include?('database_objectable.rb')
185
+ require_relative File.join(root_directory,
186
+ 'rubee/models/database_objectable')
187
+ end
188
+ return if black_list.include?('sequel_object.rb')
189
+
190
+ require_relative File.join(root_directory,
191
+ 'rubee/models/sequel_object')
179
192
  end
180
193
  end
181
194
  end
@@ -184,7 +197,7 @@ module Rubee
184
197
  def initialize(model_name, attributes, controller_name, action_name)
185
198
  @model_name = model_name&.downcase
186
199
  @attributes = attributes
187
- @plural_name = "#{controller_name.to_s.gsub("Controller", "").downcase}"
200
+ @plural_name = controller_name.to_s.gsub('Controller', '').downcase.to_s
188
201
  @action_name = action_name
189
202
  @controller_name = controller_name
190
203
  end
@@ -207,12 +220,12 @@ module Rubee
207
220
 
208
221
  content = <<~RUBY
209
222
  class #{@model_name.capitalize} < Rubee::SequelObject
210
- attr_accessor #{@attributes.map { |hash| ":#{hash[:name]}" }.join(", ")}
223
+ attr_accessor #{@attributes.map { |hash| ":#{hash[:name]}" }.join(', ')}
211
224
  end
212
225
  RUBY
213
226
 
214
227
  File.open(model_file, 'w') { |file| file.write(content) }
215
- color_puts "Model #{@model_name} created", color: :green
228
+ color_puts("Model #{@model_name} created", color: :green)
216
229
  end
217
230
 
218
231
  def generate_controller
@@ -231,7 +244,7 @@ module Rubee
231
244
  RUBY
232
245
 
233
246
  File.open(controller_file, 'w') { |file| file.write(content) }
234
- color_puts "Controller #{@plural_name} created", color: :green
247
+ color_puts("Controller #{@plural_name} created", color: :green)
235
248
  end
236
249
 
237
250
  def generate_view
@@ -246,7 +259,7 @@ module Rubee
246
259
  ERB
247
260
 
248
261
  File.open(view_file, 'w') { |file| file.write(content) }
249
- color_puts "View #{@plural_name}_#{@action_name} created", color: :green
262
+ color_puts("View #{@plural_name}_#{@action_name} created", color: :green)
250
263
  end
251
264
 
252
265
  def generate_db_file
@@ -264,8 +277,7 @@ module Rubee
264
277
  RUBY
265
278
 
266
279
  File.open(db_file, 'w') { |file| file.write(content) }
267
- color_puts "DB file for #{@plural_name} created", color: :green
280
+ color_puts("DB file for #{@plural_name} created", color: :green)
268
281
  end
269
282
  end
270
283
  end
271
-
@@ -3,16 +3,16 @@ require_relative 'test_helper'
3
3
  describe 'Account model' do
4
4
  describe 'holds :user' do
5
5
  after do
6
- Account.destroy_all cascade: true
6
+ Account.destroy_all(cascade: true)
7
7
  end
8
8
 
9
9
  describe 'when it holds user_id' do
10
10
  it 'returns associated User record' do
11
- user = User.new(email: "ok-test@test.com", password: "123")
11
+ user = User.new(email: 'ok-test@test.com', password: '123')
12
12
  user.save
13
- account = Account.new(user_id: user.id, addres: "test")
13
+ account = Account.new(user_id: user.id, addres: 'test')
14
14
  account.save
15
- _(account.user.id).must_equal user.id
15
+ _(account.user.id).must_equal(user.id)
16
16
  end
17
17
  end
18
18
  end
@@ -13,17 +13,17 @@ class RubeeAppTest < Minitest::Test
13
13
 
14
14
  def teardown
15
15
  # detach auth methods
16
- if WelcomeController.instance_variable_defined?(:@auth_methods)
17
- WelcomeController.send(:remove_instance_variable, :@auth_methods)
18
- end
16
+ return unless WelcomeController.instance_variable_defined?(:@auth_methods)
17
+
18
+ WelcomeController.send(:remove_instance_variable, :@auth_methods)
19
19
  end
20
20
 
21
21
  def test_welcome_controller_included_auth_tokenable
22
22
  WelcomeController.include(Rubee::AuthTokenable)
23
- WelcomeController.auth_methods :show
23
+ WelcomeController.auth_methods(:show)
24
24
 
25
- get '/'
25
+ get('/')
26
26
 
27
- assert_equal last_response.status, 401
27
+ assert_equal(last_response.status, 401)
28
28
  end
29
29
  end
@@ -3,32 +3,32 @@ require_relative 'test_helper'
3
3
  describe 'Comment model' do
4
4
  describe 'owns_many :users, over: :posts' do
5
5
  before do
6
- comment = Comment.new(text: "test")
6
+ comment = Comment.new(text: 'test')
7
7
  comment.save
8
- user = User.new(email: "ok-test@test.com", password: "123")
8
+ user = User.new(email: 'ok-test@test.com', password: '123')
9
9
  user.save
10
10
  post = Post.new(user_id: user.id, comment_id: comment.id)
11
11
  post.save
12
12
  end
13
13
 
14
14
  after do
15
- Comment.destroy_all cascade: true
15
+ Comment.destroy_all(cascade: true)
16
16
  end
17
17
 
18
18
  describe 'when there are associated comment records' do
19
19
  it 'returns all records' do
20
- _(Comment.where(text: "test").last.users.count).must_equal 1
21
- _(Comment.where(text: "test").last.users.first.email).must_equal "ok-test@test.com"
20
+ _(Comment.where(text: 'test').last.users.count).must_equal(1)
21
+ _(Comment.where(text: 'test').last.users.first.email).must_equal('ok-test@test.com')
22
22
  end
23
23
  end
24
24
 
25
25
  describe 'sequel dataset query' do
26
26
  it 'returns all records' do
27
27
  result = Comment.dataset.join(:posts, comment_id: :id)
28
- .where(comment_id: Comment.where(text: "test").last.id)
28
+ .where(comment_id: Comment.where(text: 'test').last.id)
29
29
  .then { |dataset| Comment.serialize(dataset) }
30
30
 
31
- _(result.first.text).must_equal "test"
31
+ _(result.first.text).must_equal('test')
32
32
  end
33
33
  end
34
34
  end
@@ -1,4 +1,5 @@
1
1
  class Account < Rubee::SequelObject
2
2
  attr_accessor :id, :addres, :user_id
3
+
3
4
  holds :user
4
5
  end
@@ -1,4 +1,5 @@
1
1
  class Comment < Rubee::SequelObject
2
2
  attr_accessor :id, :text, :user_id
3
+
3
4
  owns_many :users, over: :posts
4
5
  end
@@ -1,5 +1,6 @@
1
1
  class Post < Rubee::SequelObject
2
2
  attr_accessor :id, :user_id, :comment_id
3
+
3
4
  holds :comment
4
5
  holds :user
5
6
  end
@@ -1,4 +1,5 @@
1
1
  class User < Rubee::SequelObject
2
2
  attr_accessor :id, :email, :password
3
+
3
4
  owns_many :accounts, cascade: true
4
5
  end
@@ -7,18 +7,16 @@ class RubeeAppTest < Minitest::Test
7
7
  Rubee::Application.instance
8
8
  end
9
9
 
10
-
11
10
  def test_welcome_route
12
- get '/'
11
+ get('/')
13
12
 
14
- assert_equal 200, last_response.status, "Unexpected response: #{last_response.body}"
15
- assert_includes last_response.body, 'All set up and running!'
13
+ assert_equal(200, last_response.status, "Unexpected response: #{last_response.body}")
14
+ assert_includes(last_response.body, 'All set up and running!')
16
15
  end
17
16
 
18
-
19
17
  def test_not_found_route
20
- get '/random'
18
+ get('/random')
21
19
 
22
- assert_equal 404, last_response.status
20
+ assert_equal(404, last_response.status)
23
21
  end
24
22
  end
data/lib/tests/test.db CHANGED
Binary file
@@ -1,4 +1,4 @@
1
- require "bundler/setup"
1
+ require 'bundler/setup'
2
2
  Bundler.require(:test)
3
3
 
4
4
  require 'minitest/autorun'
@@ -6,9 +6,7 @@ require 'rack/test'
6
6
  require_relative '../../lib/rubee'
7
7
 
8
8
  Rubee::Autoload.call
9
- Rubee::Configuration.setup(env=:test) do |config|
10
- config.database_url = { url: "sqlite://lib/tests/test.db", env: }
9
+ Rubee::Configuration.setup(env = :test) do |config|
10
+ config.database_url = { url: 'sqlite://lib/tests/test.db', env: }
11
11
  end
12
12
  Rubee::SequelObject.reconnect!
13
-
14
-