rails_drivers 0.5.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -7
- data/lib/generators/driver/USAGE +1 -1
- data/lib/generators/driver/driver_generator.rb +1 -1
- data/lib/rails_drivers.rb +1 -1
- data/lib/rails_drivers/extensions.rb +46 -0
- data/lib/rails_drivers/version.rb +1 -1
- metadata +3 -3
- data/lib/rails_drivers/overrides.rb +0 -46
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b0f6e72ff1714747fe0c4a3b0d6868236099950af9d3261337d9a352a1d9e1b
|
4
|
+
data.tar.gz: 7f69a6821fefa1ac60c738df6523b5608c6d1575a80d9349f5bcb67d476feec5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a17961dfac581a7be80f473e1dcd1c3c53fc5d855b2452548a3e99571dbe45a9c092da8a27b125de9d1c37dbc2b2d97bb55bf313c3f977a733a26e4415651f49
|
7
|
+
data.tar.gz: 45b591eee5e366b08b72d331c22593221c5e93163e89b25761c13652f919fcf99ace3b9637d246d53464c95f1031efb6eee9f4d746bc2ecec9706c0df90ee28c
|
data/README.md
CHANGED
@@ -61,23 +61,23 @@ end
|
|
61
61
|
|
62
62
|
Can be executed using `rake driver:my_driver:my_namespace:task_name`.
|
63
63
|
|
64
|
-
###
|
64
|
+
### Extensions
|
65
65
|
|
66
|
-
Sometimes you want to add a method to a core class, but that method will only be used by one driver. This can be achieved by adding files to your driver's `
|
66
|
+
Sometimes you want to add a method to a core class, but that method will only be used by one driver. This can be achieved by adding files to your driver's `extensions` directory.
|
67
67
|
|
68
68
|
```ruby
|
69
69
|
# app/models/product.rb
|
70
70
|
# (doesn't have to be a model - can be anything)
|
71
71
|
class Product < ApplicationRecord
|
72
|
-
# When you include this, every driver's
|
72
|
+
# When you include this, every driver's product_extension.rb is loaded and
|
73
73
|
# included. Works correctly with autoloading during development.
|
74
|
-
include RailsDrivers::
|
74
|
+
include RailsDrivers::Extensions
|
75
75
|
end
|
76
76
|
|
77
77
|
|
78
|
-
# drivers/my_driver/
|
78
|
+
# drivers/my_driver/extensions/product_extension.rb
|
79
79
|
module MyDriver
|
80
|
-
module
|
80
|
+
module ProductExtension
|
81
81
|
extend ActiveSupport::Concern
|
82
82
|
|
83
83
|
def new_method
|
@@ -91,7 +91,7 @@ end
|
|
91
91
|
Product.new.new_method
|
92
92
|
```
|
93
93
|
|
94
|
-
For each
|
94
|
+
For each Extension, the accompanying class simply `includes` it, so any methods you define will be available throughout the whole app. To make sure your drivers don't change the core behavior of the app, see [Testing for coupling](#testing-for-coupling).
|
95
95
|
|
96
96
|
### Testing for coupling
|
97
97
|
|
data/lib/generators/driver/USAGE
CHANGED
@@ -10,7 +10,7 @@ class DriverGenerator < Rails::Generators::NamedBase
|
|
10
10
|
create_file "drivers/#{file_name}/spec/.keep", ''
|
11
11
|
create_file "drivers/#{file_name}/db/migrate/.keep", ''
|
12
12
|
create_file "drivers/#{file_name}/lib/tasks/.keep", ''
|
13
|
-
create_file "drivers/#{file_name}/
|
13
|
+
create_file "drivers/#{file_name}/extensions/.keep", ''
|
14
14
|
|
15
15
|
template 'routes.rb.erb', "drivers/#{file_name}/config/routes.rb"
|
16
16
|
template 'initializer.rb.erb', "drivers/#{file_name}/config/initializers/#{file_name}_feature.rb"
|
data/lib/rails_drivers.rb
CHANGED
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsDrivers
|
4
|
+
module Extensions
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
# Including this module results in all available extension modules being
|
8
|
+
# included.
|
9
|
+
included do
|
10
|
+
cattr_reader :driver_extensions
|
11
|
+
|
12
|
+
possible_extensions = Dir.glob(
|
13
|
+
Rails.root.join(
|
14
|
+
'drivers', '*', 'extensions',
|
15
|
+
"#{name.underscore}_extension.rb"
|
16
|
+
)
|
17
|
+
)
|
18
|
+
|
19
|
+
@@driver_extensions = possible_extensions.map do |path|
|
20
|
+
require_dependency path
|
21
|
+
%r{drivers/(?<driver_name>[^/]+)/extensions} =~ path
|
22
|
+
|
23
|
+
extension = "#{driver_name.classify}::#{name}Extension".constantize
|
24
|
+
include extension
|
25
|
+
extension
|
26
|
+
end.freeze
|
27
|
+
|
28
|
+
singleton_class.prepend CheckForShadowedMethods
|
29
|
+
end
|
30
|
+
|
31
|
+
# This module is prepended to the singleton class of the including class
|
32
|
+
# to detect when an extension is attempting to re-define any methods.
|
33
|
+
module CheckForShadowedMethods
|
34
|
+
def method_added(method_name)
|
35
|
+
driver_extensions.each do |extension|
|
36
|
+
next unless extension.instance_methods.include?(method_name)
|
37
|
+
|
38
|
+
Rails.logger.warn "Driver extension method #{extension.name}##{method_name} "\
|
39
|
+
"is shadowed by #{name}##{method_name} and will likely not do anything."
|
40
|
+
end
|
41
|
+
|
42
|
+
super(method_name)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_drivers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nigel Baillie
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -101,8 +101,8 @@ files:
|
|
101
101
|
- lib/generators/driver/templates/module.rb.erb
|
102
102
|
- lib/generators/driver/templates/routes.rb.erb
|
103
103
|
- lib/rails_drivers.rb
|
104
|
+
- lib/rails_drivers/extensions.rb
|
104
105
|
- lib/rails_drivers/files.rb
|
105
|
-
- lib/rails_drivers/overrides.rb
|
106
106
|
- lib/rails_drivers/railtie.rb
|
107
107
|
- lib/rails_drivers/routes.rb
|
108
108
|
- lib/rails_drivers/setup.rb
|
@@ -1,46 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RailsDrivers
|
4
|
-
module Overrides
|
5
|
-
extend ActiveSupport::Concern
|
6
|
-
|
7
|
-
# Including this module results in all available override modules being
|
8
|
-
# included.
|
9
|
-
included do
|
10
|
-
cattr_reader :driver_overrides
|
11
|
-
|
12
|
-
possible_overrides = Dir.glob(
|
13
|
-
Rails.root.join(
|
14
|
-
'drivers', '*', 'overrides',
|
15
|
-
"#{name.underscore}_override.rb"
|
16
|
-
)
|
17
|
-
)
|
18
|
-
|
19
|
-
@@driver_overrides = possible_overrides.map do |path|
|
20
|
-
require_dependency path
|
21
|
-
%r{drivers/(?<driver_name>[^/]+)/overrides} =~ path
|
22
|
-
|
23
|
-
override = "#{driver_name.classify}::#{name}Override".constantize
|
24
|
-
include override
|
25
|
-
override
|
26
|
-
end.freeze
|
27
|
-
|
28
|
-
singleton_class.prepend CheckForShadowedMethods
|
29
|
-
end
|
30
|
-
|
31
|
-
# This module is prepended to the singleton class of the including class
|
32
|
-
# to detect when an override is attempting to re-define any methods.
|
33
|
-
module CheckForShadowedMethods
|
34
|
-
def method_added(method_name)
|
35
|
-
driver_overrides.each do |override|
|
36
|
-
next unless override.instance_methods.include?(method_name)
|
37
|
-
|
38
|
-
Rails.logger.warn "Driver override method #{override.name}##{method_name} "\
|
39
|
-
"is shadowed by #{name}##{method_name} and will likely not do anything."
|
40
|
-
end
|
41
|
-
|
42
|
-
super(method_name)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|