durable_decorator 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,7 +22,7 @@ module DurableDecorator
22
22
  return unless old_method = existing_method(clazz, method_name, meta, &block)
23
23
 
24
24
  alias_original clazz, method_name
25
- alias_definitions clazz, method_name, method_sha(old_method)
25
+ alias_definitions clazz, method_name, Util.method_sha(old_method)
26
26
  redefine_method clazz, method_name, &block
27
27
 
28
28
  store_redefinition clazz, method_name, old_method, block
@@ -42,14 +42,10 @@ module DurableDecorator
42
42
  def validate_decoration_meta clazz, method_name, old_method, meta
43
43
  return unless meta
44
44
 
45
- chill_meta = {}
46
- meta.each do |k,v|
47
- chill_meta[k.to_sym] = v
48
- end
45
+ chill_meta = Util.symbolized_hash(meta)
49
46
 
50
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?
51
-
52
- raise TamperedDefinitionError, "Method SHA mismatch, the definition has been tampered with" unless method_sha(old_method) == chill_meta[:sha]
48
+ raise TamperedDefinitionError, "Method SHA mismatch, the definition has been tampered with" unless Util.method_sha(old_method) == chill_meta[:sha]
53
49
  end
54
50
 
55
51
  def validate_method_arity clazz, method_name, old_method, &block
@@ -90,63 +86,35 @@ module DurableDecorator
90
86
  end
91
87
  end
92
88
 
93
- def class_name clazz
94
- name = clazz.name || ''
95
- name = clazz.to_s if name.empty?
96
- name.to_sym
97
- end
98
-
99
- def full_method_name clazz, method_name
100
- "#{class_name(clazz)}##{method_name}"
101
- end
102
89
 
103
90
  def original_redefinition? clazz, method_name
104
- !REDEFINITIONS[full_method_name(clazz, method_name)]
91
+ !REDEFINITIONS[Util.full_method_name(clazz, method_name)]
105
92
  end
106
93
 
107
94
  def store_redefinition clazz, name, old_method, new_method
108
- methods = REDEFINITIONS[full_method_name(clazz, name)] ||= []
95
+ methods = REDEFINITIONS[Util.full_method_name(clazz, name)] ||= []
109
96
 
110
97
  to_store = [new_method]
111
98
  to_store.unshift(old_method) if original_redefinition?(clazz, name)
112
99
 
113
100
  to_store.each do |method|
114
- methods << method_hash(name, method)
101
+ methods << Util.method_hash(name, method)
115
102
  end
116
103
 
117
104
  true
118
105
  end
119
106
 
120
- def method_hash name, method
121
- {
122
- :name => name,
123
- :sha => method_sha(method)
124
- }
125
- end
126
-
127
- def method_sha method
128
- Digest::SHA1.hexdigest(method.source.gsub(/\s+/, ' '))
129
- end
130
-
131
107
  def redefined? clazz, method_name, &block
132
108
  begin
133
109
  result =
134
110
  overrides = REDEFINITIONS[clazz][method_name] and
135
- overrides.select{|o| o == method_hash(method_name)}.first and
111
+ overrides.select{|o| o == Util.method_hash(method_name)}.first and
136
112
  true
137
113
  rescue
138
114
  false
139
115
  end
140
116
  end
141
117
 
142
- def logger
143
- return @logger if @logger
144
-
145
- @logger = Logging.logger(STDOUT)
146
- @logger.level = :warn
147
- @logger
148
- end
149
-
150
118
  def determine_sha target
151
119
  raise "Please provide a fully qualified method name: Module::Clazz#instance_method or ::clazz_method" unless target.match(/\.|#/)
152
120
 
@@ -158,7 +126,7 @@ module DurableDecorator
158
126
  clazz.method(method_name)
159
127
  end
160
128
 
161
- method_sha(method)
129
+ Util.method_sha(method)
162
130
  end
163
131
  end
164
132
  end
@@ -0,0 +1,44 @@
1
+ module DurableDecorator
2
+ class Util
3
+ class << self
4
+ def symbolized_hash hash
5
+ chill_hash = {}
6
+
7
+ hash.each do |k,v|
8
+ chill_hash[k.to_sym] = v
9
+ end
10
+
11
+ chill_hash
12
+ end
13
+
14
+ def class_name clazz
15
+ name = clazz.name || ''
16
+ name = clazz.to_s if name.empty?
17
+ name.to_sym
18
+ end
19
+
20
+ def full_method_name clazz, method_name
21
+ "#{class_name(clazz)}##{method_name}"
22
+ end
23
+
24
+ def method_hash name, method
25
+ {
26
+ :name => name,
27
+ :sha => method_sha(method)
28
+ }
29
+ end
30
+
31
+ def method_sha method
32
+ Digest::SHA1.hexdigest(method.source.gsub(/\s+/, ' '))
33
+ end
34
+
35
+ def logger
36
+ return @logger if @logger
37
+
38
+ @logger = Logging.logger(STDOUT)
39
+ @logger.level = :warn
40
+ @logger
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,3 +1,3 @@
1
1
  module DurableDecorator
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -8,6 +8,7 @@ require 'logging'
8
8
  require "durable_decorator/version"
9
9
  require "durable_decorator/constantizer"
10
10
  require "durable_decorator/base"
11
+ require "durable_decorator/util"
11
12
 
12
13
  # errors
13
14
  require "durable_decorator/bad_arity_error"
@@ -41,32 +41,32 @@ describe DurableDecorator::Base do
41
41
 
42
42
  context 'for double decorations' do
43
43
  before do
44
- @original_sha = DurableDecorator::Base.determine_sha("ExampleClass#one_param_method")
45
-
46
44
  ExampleClass.class_eval do
47
45
  durably_decorate :one_param_method do |another_string|
48
46
  "#{one_param_method_935888f04d9e132be458591d5755cb8131fec457('check')} and #{another_string}"
49
47
  end
50
48
  end
51
-
52
- @redef_sha = DurableDecorator::Base.determine_sha("ExampleClass#one_param_method")
53
49
  end
54
50
 
55
51
  it 'works with explicit method version invocation' do
56
52
  ExampleClass.class_eval do
57
53
  durably_decorate :one_param_method do |boolean|
58
54
  if boolean
59
- one_param_method_original("check and ")
55
+ one_param_method_original("older")
60
56
  else
61
- "latest"
57
+ # ugly hack due to inconsistent method_source behavior on 1.8.7
58
+ if /^ruby 1\.8\.7/ =~ RUBY_DESCRIPTION
59
+ one_param_method_0ec3("newer")
60
+ else
61
+ one_param_method_3c39("newer")
62
+ end
62
63
  end
63
64
  end
64
65
  end
65
66
 
66
67
  instance = ExampleClass.new
67
- instance.one_param_method(true).should == "original: check and "
68
- instance.one_param_method(false).should == 'latest'
69
- @original_sha.should_not == @redef_sha
68
+ instance.one_param_method(true).should == "original: older"
69
+ instance.one_param_method(false).should == 'original: check and newer'
70
70
  end
71
71
 
72
72
  it 'work with short explicit method version invocation' do
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.6
4
+ version: 0.0.8
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-06-19 00:00:00.000000000 Z
12
+ date: 2013-06-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: method_source
@@ -129,6 +129,7 @@ files:
129
129
  - lib/durable_decorator/invalid_decoration_error.rb
130
130
  - lib/durable_decorator/tampered_definition_error.rb
131
131
  - lib/durable_decorator/undefined_method_error.rb
132
+ - lib/durable_decorator/util.rb
132
133
  - lib/durable_decorator/version.rb
133
134
  - spec/durable_decorator_spec.rb
134
135
  - spec/example_class.rb