durable_decorator 0.0.8 → 0.0.9
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/README.md +24 -0
- data/durable_decorator.gemspec +1 -1
- data/lib/durable_decorator/base.rb +4 -25
- data/lib/durable_decorator/validator.rb +29 -0
- data/lib/durable_decorator/version.rb +1 -1
- data/lib/durable_decorator.rb +1 -0
- metadata +3 -18
data/README.md
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
|
5
5
|
[](https://travis-ci.org/jumph4x/durable_decorator)
|
6
6
|
[](https://codeclimate.com/github/jumph4x/durable_decorator)
|
7
|
+
[](https://gemnasium.com/jumph4x/durable_decorator)
|
7
8
|
|
8
9
|
This is a project for modifying the behavior of gems outside of your reach. You may be using a large Rails Engine and be wanting to simply decorate some existing behavior, but at the same time you want to inherit original behavior.
|
9
10
|
|
@@ -77,6 +78,29 @@ DurableDecorator::TamperedDefinitionError: Method SHA mismatch, the definition h
|
|
77
78
|
|
78
79
|
DurableDecorator also maintains explicit versions of each method overriden by creating aliases with appended SHAs of the form ```some_method_1234abcd``` so you can always target explicit method versions without relying on ```some_method_original```.
|
79
80
|
|
81
|
+
DurableDecorator maintains 3 versions of aliases to previous method versions, 2 of which are short-SHA versions, akin to Github:
|
82
|
+
```ruby
|
83
|
+
DurableDecorator::Base.determine_sha('ExampleClass#no_param_method')
|
84
|
+
# => 'ba3114b2d46caa684b3f7ba38d6f74b2'
|
85
|
+
|
86
|
+
ExampleClass.class_eval do
|
87
|
+
durably_decorate :string_method do
|
88
|
+
"new"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# 3 explicit aliases preserve access to the original method based on it's original SHA:
|
93
|
+
# 4-char SHA, 6-char SHA and the full SHA suffix
|
94
|
+
|
95
|
+
instance = ExampleClass.new
|
96
|
+
instance.string_method_ba31
|
97
|
+
# => "original"
|
98
|
+
instance.string_method_ba3114
|
99
|
+
# => "original"
|
100
|
+
instance.string_method_ba3114b2d46caa684b3f7ba38d6f74b2
|
101
|
+
# => "original"
|
102
|
+
```
|
103
|
+
|
80
104
|
### No more suprise monkey patching
|
81
105
|
Once you decorate the method and seal it with its SHA, if some gem tries to come in and overwrite your work **BEFORE** decorate-time, DurableDecorator will warn you. Similarly, expect to see an exception bubble up if the definition of the original method has changed and requires a review and a re-hash.
|
82
106
|
|
data/durable_decorator.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_dependency "method_source"
|
22
22
|
spec.add_dependency "logging"
|
23
|
-
spec.add_dependency "activesupport"
|
23
|
+
#spec.add_dependency "activesupport"
|
24
24
|
|
25
25
|
spec.add_development_dependency "bundler", "~> 1.3"
|
26
26
|
spec.add_development_dependency "rake"
|
@@ -3,7 +3,6 @@ require 'digest/sha1'
|
|
3
3
|
module DurableDecorator
|
4
4
|
class Base
|
5
5
|
class << self
|
6
|
-
DECORATION_MODES = ['strict']
|
7
6
|
REDEFINITIONS = {}
|
8
7
|
|
9
8
|
def reset!
|
@@ -32,33 +31,13 @@ module DurableDecorator
|
|
32
31
|
def existing_method clazz, method_name, meta, &block
|
33
32
|
return if redefined? clazz, method_name, &block
|
34
33
|
|
35
|
-
old_method = validate_existing_definition clazz, method_name
|
36
|
-
validate_method_arity clazz, method_name, old_method, &block
|
37
|
-
validate_decoration_meta clazz, method_name, old_method, meta
|
34
|
+
old_method = Validator.validate_existing_definition clazz, method_name
|
35
|
+
Validator.validate_method_arity clazz, method_name, old_method, &block
|
36
|
+
Validator.validate_decoration_meta clazz, method_name, old_method, meta
|
38
37
|
|
39
38
|
old_method
|
40
39
|
end
|
41
40
|
|
42
|
-
def validate_decoration_meta clazz, method_name, old_method, meta
|
43
|
-
return unless meta
|
44
|
-
|
45
|
-
chill_meta = Util.symbolized_hash(meta)
|
46
|
-
|
47
|
-
raise InvalidDecorationError, "The hash provided to the decorator is invalid" unless DECORATION_MODES.include? chill_meta[:mode] and chill_meta[:sha] and !chill_meta[:sha].empty?
|
48
|
-
raise TamperedDefinitionError, "Method SHA mismatch, the definition has been tampered with" unless Util.method_sha(old_method) == chill_meta[:sha]
|
49
|
-
end
|
50
|
-
|
51
|
-
def validate_method_arity clazz, method_name, old_method, &block
|
52
|
-
raise BadArityError, "Attempting to override #{clazz}'s #{method_name} with incorrect arity." if block.arity != old_method.arity and block.arity > 0 # See the #arity behavior disparity between 1.8- and 1.9+
|
53
|
-
end
|
54
|
-
|
55
|
-
def validate_existing_definition clazz, method_name
|
56
|
-
begin
|
57
|
-
clazz.instance_method(method_name)
|
58
|
-
rescue NameError => e
|
59
|
-
raise UndefinedMethodError, "#{clazz}##{method_name} is not defined."
|
60
|
-
end
|
61
|
-
end
|
62
41
|
|
63
42
|
def redefine_method clazz, method_name, &block
|
64
43
|
clazz.class_eval do
|
@@ -116,7 +95,7 @@ module DurableDecorator
|
|
116
95
|
end
|
117
96
|
|
118
97
|
def determine_sha target
|
119
|
-
raise "Please provide a fully qualified method name: Module::Clazz#instance_method or ::clazz_method" unless target.match(/\.|#/)
|
98
|
+
raise "Please provide a fully qualified method name: Module::Clazz#instance_method or ::clazz_method" unless target && target.match(/\.|#/)
|
120
99
|
|
121
100
|
class_name, separator, method_name = target.match(/(.*)(\.|#)(.*)/)[1..3]
|
122
101
|
clazz = Constantizer.constantize(class_name)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module DurableDecorator
|
2
|
+
class Validator
|
3
|
+
class << self
|
4
|
+
DECORATION_MODES = ['strict']
|
5
|
+
|
6
|
+
def validate_decoration_meta clazz, method_name, old_method, meta
|
7
|
+
return unless meta
|
8
|
+
|
9
|
+
chill_meta = Util.symbolized_hash(meta)
|
10
|
+
|
11
|
+
raise InvalidDecorationError, "The hash provided to the decorator is invalid" unless DECORATION_MODES.include? chill_meta[:mode] and chill_meta[:sha] and !chill_meta[:sha].empty?
|
12
|
+
raise TamperedDefinitionError, "Method SHA mismatch, the definition has been tampered with" unless Util.method_sha(old_method) == chill_meta[:sha]
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate_method_arity clazz, method_name, old_method, &block
|
16
|
+
raise BadArityError, "Attempting to override #{clazz}'s #{method_name} with incorrect arity." if block.arity != old_method.arity and block.arity > 0 # See the #arity behavior disparity between 1.8- and 1.9+
|
17
|
+
end
|
18
|
+
|
19
|
+
def validate_existing_definition clazz, method_name
|
20
|
+
begin
|
21
|
+
clazz.instance_method(method_name)
|
22
|
+
rescue NameError => e
|
23
|
+
raise UndefinedMethodError, "#{clazz}##{method_name} is not defined."
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
data/lib/durable_decorator.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: durable_decorator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
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-
|
12
|
+
date: 2013-07-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: method_source
|
@@ -43,22 +43,6 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
-
- !ruby/object:Gem::Dependency
|
47
|
-
name: activesupport
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
|
-
requirements:
|
51
|
-
- - ! '>='
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: '0'
|
54
|
-
type: :runtime
|
55
|
-
prerelease: false
|
56
|
-
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
|
-
requirements:
|
59
|
-
- - ! '>='
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
46
|
- !ruby/object:Gem::Dependency
|
63
47
|
name: bundler
|
64
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -130,6 +114,7 @@ files:
|
|
130
114
|
- lib/durable_decorator/tampered_definition_error.rb
|
131
115
|
- lib/durable_decorator/undefined_method_error.rb
|
132
116
|
- lib/durable_decorator/util.rb
|
117
|
+
- lib/durable_decorator/validator.rb
|
133
118
|
- lib/durable_decorator/version.rb
|
134
119
|
- spec/durable_decorator_spec.rb
|
135
120
|
- spec/example_class.rb
|