durable_decorator_rails 0.0.4 → 0.0.5

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.
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "durable_decorator", "~> 0.1.0"
21
+ spec.add_dependency "durable_decorator", "~> 0.1.2"
22
22
  spec.add_dependency "rails"
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
@@ -1,3 +1,3 @@
1
1
  module DurableDecoratorRails
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,3 +1,4 @@
1
+ require 'durable_decorator'
1
2
  require "durable_decorator_rails/version"
2
3
  require "durable_decorator_rails/railtie"
3
4
  require "durable_decorator_rails/durable_decorator_ext"
@@ -0,0 +1,115 @@
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
@@ -0,0 +1,3 @@
1
+ <%= full_class_name %>.class_eval do
2
+
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: durable_decorator_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-02 00:00:00.000000000 Z
12
+ date: 2013-08-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: durable_decorator
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 0.1.0
21
+ version: 0.1.2
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: 0.1.0
29
+ version: 0.1.2
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: rails
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -111,6 +111,8 @@ files:
111
111
  - lib/durable_decorator_rails/durable_decorator_ext.rb
112
112
  - lib/durable_decorator_rails/railtie.rb
113
113
  - lib/durable_decorator_rails/version.rb
114
+ - lib/generators/durable_decorator/decorator/decorator_generator.rb
115
+ - lib/generators/durable_decorator/decorator/templates/decorator.rb.erb
114
116
  - lib/tasks/durable_decorator_rails.tasks
115
117
  - spec/durable_decorator_rails_spec.rb
116
118
  - spec/spec_helper.rb