durable_decorator_rails 0.1.1 → 0.2.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 +2 -2
- data/lib/durable_decorator_rails.rb +1 -0
- data/lib/durable_decorator_rails/class_type_extractor.rb +49 -0
- data/lib/durable_decorator_rails/version.rb +1 -1
- data/lib/generators/decorator/decorator_generator.rb +93 -0
- data/lib/generators/{durable_decorator/decorator → decorator}/templates/decorator.rb.erb +0 -0
- data/spec/class_type_extractor_spec.rb +38 -0
- data/spec/example/app/models/example_model.rb +7 -0
- data/spec/example/lib/example_lib.rb +7 -0
- data/spec/example/lib/namespaced/and_again/example_lib.rb +11 -0
- metadata +13 -6
- data/lib/generators/durable_decorator/decorator/decorator_generator.rb +0 -115
- data/spec/durable_decorator_rails_spec.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36b186cdfc6de77521d61625035d028f63ff1aee
|
4
|
+
data.tar.gz: 173de9339c8f7a676ae600e45c205778af3a6a68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf7fc9a3712f138bda6cc9b96227b3310ee8e296b0e952bdb233c2e16caa844d804bd1f02e34a27bc883adc30d2dfd4a0a6d331ced0133dbe6e73c31e492f7d4
|
7
|
+
data.tar.gz: 792b6f3cbeb9ad34f2cb66107245d28c7bd9f41a67efe9900d480a3490da4613ee20b13b151db1c9065dd5ace49dbd20a76f3f531eb8172b4f23a942f57fd1a8
|
data/README.md
CHANGED
@@ -19,12 +19,12 @@ Or install it yourself as:
|
|
19
19
|
## Usage
|
20
20
|
This gem will generate the correct file structure, place (or append to) the correct decorator and even insert the correct seal for you. Use:
|
21
21
|
```shell
|
22
|
-
rails g
|
22
|
+
rails g decorator [FULL_METHOD_NAME]
|
23
23
|
```
|
24
24
|
|
25
25
|
For example, let us decorate the an existing ```#deleted?``` instance method from the gem that is namespaced under ```Spree``` in a file that is a *model* called ```Product```. Just run:
|
26
26
|
```shell
|
27
|
-
rails g
|
27
|
+
rails g decorator Spree::Product#deleted?
|
28
28
|
```
|
29
29
|
|
30
30
|
The gem will create the ```app/models/spree/product_decorator.rb``` file for you to start with, and it will have the following contents:
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module DurableDecoratorRails
|
2
|
+
class ClassTypeExtractor
|
3
|
+
class << self
|
4
|
+
def decorator_path method_name
|
5
|
+
type = determine_class_type method_name
|
6
|
+
|
7
|
+
dir = if type == 'lib'
|
8
|
+
type
|
9
|
+
else
|
10
|
+
"app/#{type}"
|
11
|
+
end
|
12
|
+
|
13
|
+
suffix = namespace method_name
|
14
|
+
dir += "/#{suffix}" if suffix
|
15
|
+
|
16
|
+
dir
|
17
|
+
end
|
18
|
+
|
19
|
+
def determine_class_type method_name
|
20
|
+
class_location_directory method_name
|
21
|
+
end
|
22
|
+
|
23
|
+
def class_location_directory method_name
|
24
|
+
method_source(method_name).gsub(class_location(method_name), '').split('/').last
|
25
|
+
end
|
26
|
+
|
27
|
+
def method_source name
|
28
|
+
DurableDecorator::Base.extract_method(name).source_location[0]
|
29
|
+
end
|
30
|
+
|
31
|
+
def class_name method_name
|
32
|
+
method_name.match(receiver_separator)[1]
|
33
|
+
end
|
34
|
+
|
35
|
+
def class_location method_name
|
36
|
+
"/#{class_name(method_name).underscore}.rb"
|
37
|
+
end
|
38
|
+
|
39
|
+
def namespace method_name
|
40
|
+
namespace = method_name.match(receiver_separator)[1].split('::')[0..-2].join('/').underscore
|
41
|
+
namespace.presence
|
42
|
+
end
|
43
|
+
|
44
|
+
def receiver_separator
|
45
|
+
/(.*)(\.|#)(.*)/
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
class DecoratorGenerator < Rails::Generators::Base
|
2
|
+
source_root File.expand_path('../templates', __FILE__)
|
3
|
+
argument :full_method_name, :type => :string
|
4
|
+
|
5
|
+
def ensure_decorator_existence
|
6
|
+
ensure_file
|
7
|
+
ensure_method
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def filesystem_parts
|
13
|
+
full_method_name.split('::')
|
14
|
+
end
|
15
|
+
|
16
|
+
def target
|
17
|
+
filesystem_parts.last
|
18
|
+
end
|
19
|
+
|
20
|
+
def parts
|
21
|
+
target.match(DurableDecoratorRails::ClassTypeExtractor.receiver_separator)[1..3]
|
22
|
+
end
|
23
|
+
|
24
|
+
def full_class_name
|
25
|
+
full_method_name.match(DurableDecoratorRails::ClassTypeExtractor.receiver_separator)[1]
|
26
|
+
end
|
27
|
+
|
28
|
+
def class_name
|
29
|
+
parts[0]
|
30
|
+
end
|
31
|
+
|
32
|
+
def method_name
|
33
|
+
parts[2]
|
34
|
+
end
|
35
|
+
|
36
|
+
def filename
|
37
|
+
"#{class_name.underscore}_decorator.rb"
|
38
|
+
end
|
39
|
+
|
40
|
+
def full_path
|
41
|
+
"#{DurableDecoratorRails::ClassTypeExtractor.decorator_path(full_method_name)}/#{filename}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def ensure_file
|
45
|
+
template "decorator.rb.erb", full_path unless File.exists?(full_path)
|
46
|
+
end
|
47
|
+
|
48
|
+
def ensure_method
|
49
|
+
append_decoration unless method_decorated?
|
50
|
+
end
|
51
|
+
|
52
|
+
def append_decoration
|
53
|
+
inject_into_file full_path, after: "class_eval do" do
|
54
|
+
redefinition_template
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def redefinition_template
|
59
|
+
" \n\n" +
|
60
|
+
redefinition_body +
|
61
|
+
" \n"
|
62
|
+
end
|
63
|
+
|
64
|
+
def redefinition_body
|
65
|
+
" durably_decorate :#{method_name}, mode: 'strict', sha: '#{existing_sha}' do #{decoration_args}\n" +
|
66
|
+
" end"
|
67
|
+
end
|
68
|
+
|
69
|
+
def existing_sha
|
70
|
+
DurableDecorator::Base.determine_sha full_method_name
|
71
|
+
end
|
72
|
+
|
73
|
+
def decoration_args
|
74
|
+
"|#{minimum_arity.times.map{|n| "arg#{n}"}.join(', ')}|" if minimum_arity > 0
|
75
|
+
end
|
76
|
+
|
77
|
+
def minimum_arity
|
78
|
+
arity = DurableDecorator::Base.determine_arity(full_method_name)
|
79
|
+
if arity < 0
|
80
|
+
(arity+1).abs
|
81
|
+
else
|
82
|
+
arity
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def method_decorated?
|
87
|
+
existing_contents.match /durably_decorate :#{method_name}/
|
88
|
+
end
|
89
|
+
|
90
|
+
def existing_contents
|
91
|
+
File.read(full_path)
|
92
|
+
end
|
93
|
+
end
|
File without changes
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'example/app/models/example_model.rb'
|
3
|
+
require 'example/lib/example_lib.rb'
|
4
|
+
require 'example/lib/namespaced/and_again/example_lib.rb'
|
5
|
+
|
6
|
+
describe DurableDecoratorRails::ClassTypeExtractor do
|
7
|
+
let(:singleton){ DurableDecoratorRails::ClassTypeExtractor }
|
8
|
+
|
9
|
+
context 'on .decorator_path' do
|
10
|
+
context 'when given the fully qualified method name' do
|
11
|
+
it 'returns the correct Rails decorator path' do
|
12
|
+
singleton.decorator_path("ExampleModel#methud").should == 'app/models'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when given a namespaced file' do
|
17
|
+
it 'takes into account the namespace' do
|
18
|
+
singleton.decorator_path("Namespaced::AndAgain::ExampleLib.methud").should == 'lib/namespaced/and_again'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'on .determine_class_type' do
|
24
|
+
context 'when given the fully qualified method name' do
|
25
|
+
it 'returns the correct Rails class type' do
|
26
|
+
singleton.determine_class_type("ExampleModel.methud").should == 'models'
|
27
|
+
singleton.determine_class_type("ExampleModel#methud").should == 'models'
|
28
|
+
singleton.determine_class_type("ExampleLib#methud").should == 'lib'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'when given a namespaced file' do
|
33
|
+
it 'pulls through' do
|
34
|
+
singleton.determine_class_type("Namespaced::AndAgain::ExampleLib.methud").should == 'lib'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: durable_decorator_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Denis Ivanov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-09-
|
11
|
+
date: 2013-09-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: durable_decorator
|
@@ -97,13 +97,17 @@ files:
|
|
97
97
|
- Rakefile
|
98
98
|
- durable_decorator_rails.gemspec
|
99
99
|
- lib/durable_decorator_rails.rb
|
100
|
+
- lib/durable_decorator_rails/class_type_extractor.rb
|
100
101
|
- lib/durable_decorator_rails/durable_decorator_ext.rb
|
101
102
|
- lib/durable_decorator_rails/railtie.rb
|
102
103
|
- lib/durable_decorator_rails/version.rb
|
103
|
-
- lib/generators/
|
104
|
-
- lib/generators/
|
104
|
+
- lib/generators/decorator/decorator_generator.rb
|
105
|
+
- lib/generators/decorator/templates/decorator.rb.erb
|
105
106
|
- lib/tasks/durable_decorator_rails.tasks
|
106
|
-
- spec/
|
107
|
+
- spec/class_type_extractor_spec.rb
|
108
|
+
- spec/example/app/models/example_model.rb
|
109
|
+
- spec/example/lib/example_lib.rb
|
110
|
+
- spec/example/lib/namespaced/and_again/example_lib.rb
|
107
111
|
- spec/spec_helper.rb
|
108
112
|
homepage: https://github.com/jumph4x/durable_decorator_rails
|
109
113
|
licenses:
|
@@ -130,5 +134,8 @@ signing_key:
|
|
130
134
|
specification_version: 4
|
131
135
|
summary: Rails integration for durable_decorator
|
132
136
|
test_files:
|
133
|
-
- spec/
|
137
|
+
- spec/class_type_extractor_spec.rb
|
138
|
+
- spec/example/app/models/example_model.rb
|
139
|
+
- spec/example/lib/example_lib.rb
|
140
|
+
- spec/example/lib/namespaced/and_again/example_lib.rb
|
134
141
|
- spec/spec_helper.rb
|
@@ -1,115 +0,0 @@
|
|
1
|
-
module DurableDecorator
|
2
|
-
class DecoratorGenerator < Rails::Generators::Base
|
3
|
-
source_root File.expand_path('../templates', __FILE__)
|
4
|
-
argument :subdirectory, :type => :string
|
5
|
-
argument :full_method_name, :type => :string
|
6
|
-
|
7
|
-
def ensure_decorator_existence
|
8
|
-
ensure_file
|
9
|
-
ensure_method
|
10
|
-
end
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def namespaces
|
15
|
-
filesystem_parts[0..-2]
|
16
|
-
end
|
17
|
-
|
18
|
-
def parts
|
19
|
-
target.match(receiver_separator_regex)[1..3]
|
20
|
-
end
|
21
|
-
|
22
|
-
def receiver_separator_regex
|
23
|
-
/(.*)(\.|#)(.*)/
|
24
|
-
end
|
25
|
-
|
26
|
-
def target
|
27
|
-
filesystem_parts.last
|
28
|
-
end
|
29
|
-
|
30
|
-
def class_name
|
31
|
-
parts[0]
|
32
|
-
end
|
33
|
-
|
34
|
-
def full_class_name
|
35
|
-
full_method_name.match(receiver_separator_regex)[1]
|
36
|
-
end
|
37
|
-
|
38
|
-
def separator
|
39
|
-
parts[1]
|
40
|
-
end
|
41
|
-
|
42
|
-
def method_name
|
43
|
-
parts[2]
|
44
|
-
end
|
45
|
-
|
46
|
-
def namespace_dirs
|
47
|
-
namespaces.map(&:underscore)
|
48
|
-
end
|
49
|
-
|
50
|
-
def filename
|
51
|
-
"#{class_name.underscore}_decorator.rb"
|
52
|
-
end
|
53
|
-
|
54
|
-
def filesystem_parts
|
55
|
-
full_method_name.split('::')
|
56
|
-
end
|
57
|
-
|
58
|
-
def dir_structure
|
59
|
-
"app/#{subdirectory}/#{namespace_dirs.join('/')}"
|
60
|
-
end
|
61
|
-
|
62
|
-
def full_path
|
63
|
-
"#{dir_structure}/#{filename}"
|
64
|
-
end
|
65
|
-
|
66
|
-
def ensure_file
|
67
|
-
template "decorator.rb.erb", full_path unless File.exists?(full_path)
|
68
|
-
end
|
69
|
-
|
70
|
-
def ensure_method
|
71
|
-
append_decoration unless method_decorated?
|
72
|
-
end
|
73
|
-
|
74
|
-
def append_decoration
|
75
|
-
contents = File.read(full_path).sub("\n", redefinition_template)
|
76
|
-
File.open(full_path, 'wb') { |file| file.write(contents) }
|
77
|
-
end
|
78
|
-
|
79
|
-
def redefinition_template
|
80
|
-
" \n\n" +
|
81
|
-
redefinition_body +
|
82
|
-
" \n"
|
83
|
-
end
|
84
|
-
|
85
|
-
def redefinition_body
|
86
|
-
" durably_decorate :#{method_name}, mode: 'strict', sha: '#{existing_sha}' do #{decoration_args}\n" +
|
87
|
-
" end"
|
88
|
-
end
|
89
|
-
|
90
|
-
def existing_sha
|
91
|
-
DurableDecorator::Base.determine_sha full_method_name
|
92
|
-
end
|
93
|
-
|
94
|
-
def decoration_args
|
95
|
-
"|#{minimum_arity.times.map{|n| "arg#{n}"}.join(', ')}|" if minimum_arity > 0
|
96
|
-
end
|
97
|
-
|
98
|
-
def minimum_arity
|
99
|
-
arity = DurableDecorator::Base.determine_arity(full_method_name)
|
100
|
-
if arity < 0
|
101
|
-
(arity+1).abs
|
102
|
-
else
|
103
|
-
arity
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
def method_decorated?
|
108
|
-
existing_contents.match /durably_decorate :#{method_name}/
|
109
|
-
end
|
110
|
-
|
111
|
-
def existing_contents
|
112
|
-
File.read(full_path)
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|