aspector 0.12.2 → 0.12.3

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode # JRuby in 1.8 mode
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ #- rbx-18mode
9
+ #- rbx-19mode
10
+ # uncomment this line if your project needs to run something other than `rake`:
11
+ script: bundle exec rspec spec
12
+ #gemfile: Gemfile.travis
13
+
data/Gemfile CHANGED
@@ -4,13 +4,13 @@ group :development do
4
4
  gem "rspec"
5
5
  gem "jeweler", "~> 1.6.4"
6
6
 
7
- gem 'ruby-prof'
7
+ gem 'ruby-prof', :platforms => [:mri]
8
8
 
9
9
  gem "guard", "~> 0.8.4"
10
10
  gem "guard-bundler", "~> 0.1.3"
11
11
  gem "guard-rspec", "~> 0.5.2"
12
12
  gem "guard-shell", "~> 0.1.1"
13
- gem 'rb-fsevent', "~> 0.4.3.1"
13
+ gem 'rb-fsevent', "~> 0.4.3.1", :platforms => [:mri]
14
14
  gem 'growl', "~> 1.0.3"
15
15
 
16
16
  gem 'awesome_print'
data/Gemfile.lock CHANGED
@@ -2,7 +2,7 @@ GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
4
  awesome_print (1.0.2)
5
- coderay (1.0.5)
5
+ coderay (1.0.7)
6
6
  diff-lcs (1.1.3)
7
7
  git (1.2.5)
8
8
  growl (1.0.3)
@@ -20,25 +20,32 @@ GEM
20
20
  git (>= 1.2.5)
21
21
  rake
22
22
  method_source (0.7.1)
23
- pry (0.9.8.4)
23
+ pry (0.9.9.6)
24
24
  coderay (~> 1.0.5)
25
25
  method_source (~> 0.7.1)
26
26
  slop (>= 2.4.4, < 3)
27
+ pry (0.9.9.6-java)
28
+ coderay (~> 1.0.5)
29
+ method_source (~> 0.7.1)
30
+ slop (>= 2.4.4, < 3)
31
+ spoon (~> 0.0)
27
32
  rake (0.9.2.2)
28
33
  rb-fsevent (0.4.3.1)
29
- rspec (2.8.0)
30
- rspec-core (~> 2.8.0)
31
- rspec-expectations (~> 2.8.0)
32
- rspec-mocks (~> 2.8.0)
33
- rspec-core (2.8.0)
34
- rspec-expectations (2.8.0)
35
- diff-lcs (~> 1.1.2)
36
- rspec-mocks (2.8.0)
37
- ruby-prof (0.10.8)
34
+ rspec (2.10.0)
35
+ rspec-core (~> 2.10.0)
36
+ rspec-expectations (~> 2.10.0)
37
+ rspec-mocks (~> 2.10.0)
38
+ rspec-core (2.10.1)
39
+ rspec-expectations (2.10.0)
40
+ diff-lcs (~> 1.1.3)
41
+ rspec-mocks (2.10.1)
42
+ ruby-prof (0.11.2)
38
43
  slop (2.4.4)
44
+ spoon (0.0.1)
39
45
  thor (0.14.6)
40
46
 
41
47
  PLATFORMS
48
+ java
42
49
  ruby
43
50
 
44
51
  DEPENDENCIES
data/README.rdoc CHANGED
@@ -1,4 +1,5 @@
1
1
  = aspector
2
+ {<img src="https://secure.travis-ci.org/gcao/aspector.png" />}[http://travis-ci.org/gcao/aspector]
2
3
 
3
4
  aspector = ASPECT Oriented Ruby programming
4
5
 
@@ -50,6 +51,18 @@ gem install aspector
50
51
  # do_that
51
52
  # test
52
53
 
54
+ == Notes
55
+ To use a different logger, e.g. Logem::Logger, run below command
56
+
57
+ ASPECTOR_LOGGER=Logem::Logger LOGEM_LOG_LEVEL=trace ruby -rlogem examples/cache_aspect.rb
58
+
59
+ Explanation:
60
+ ASPECTOR_LOGGER=Logem::Logger => set logger to Logem::Logger class
61
+ LOGEM_LOG_LEVEL=trace => set logem log level to trace
62
+ -rlogem => require logem otherwise it might not be available
63
+ when the logger is initialized. This could be handled by
64
+ bundler etc though.
65
+
53
66
  == Contributing to aspector
54
67
 
55
68
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.12.2
1
+ 0.12.3
data/aspector.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "aspector"
8
- s.version = "0.12.2"
8
+ s.version = "0.12.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Guoliang Cao"]
12
- s.date = "2012-07-03"
12
+ s.date = "2012-10-09"
13
13
  s.description = ""
14
14
  s.email = "gcao99@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
21
21
  ".irbrc",
22
22
  ".rspec",
23
23
  ".rvmrc",
24
+ ".travis.yml",
24
25
  "Gemfile",
25
26
  "Gemfile.lock",
26
27
  "Guardfile",
@@ -34,6 +35,7 @@ Gem::Specification.new do |s|
34
35
  "examples/aspector_apply_example.rb",
35
36
  "examples/aspector_example.rb",
36
37
  "examples/cache_aspect.rb",
38
+ "examples/design_by_contract.rb",
37
39
  "examples/exception_handler.rb",
38
40
  "examples/implicit_method_option_test.rb",
39
41
  "examples/logging_aspect.rb",
@@ -47,6 +49,7 @@ Gem::Specification.new do |s|
47
49
  "lib/aspector/deferred_logic.rb",
48
50
  "lib/aspector/deferred_option.rb",
49
51
  "lib/aspector/logger.rb",
52
+ "lib/aspector/logging.rb",
50
53
  "lib/aspector/method_matcher.rb",
51
54
  "lib/aspector/module_extension.rb",
52
55
  "lib/aspector/object_extension.rb",
@@ -0,0 +1,71 @@
1
+ # Design by contract example
2
+
3
+ class A
4
+
5
+ def initialize
6
+ @transactions = []
7
+ @total = 0
8
+ end
9
+
10
+ def buy price
11
+ @transactions << price
12
+ @total += price
13
+ end
14
+
15
+ def sell price
16
+ @transactions << price # Wrong
17
+ @total -= price
18
+ end
19
+
20
+ end
21
+
22
+ ##############################
23
+
24
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
25
+
26
+ require 'aspector'
27
+
28
+ class Object
29
+ def assert bool, message = 'Assertion failure'
30
+ unless bool
31
+ $stderr.puts message
32
+ $stderr.puts caller
33
+ end
34
+ end
35
+ end
36
+
37
+ class ContractExample < Aspector::Base
38
+
39
+ before do |price, &block|
40
+ assert price > 0, "Price is #{price}, should be greater than 0"
41
+ end
42
+
43
+ after :result_arg => false do |*_, &block|
44
+ sum = @transactions.reduce(&:+)
45
+ assert @total == sum, "Total(#{@total}) and sum of transactions(#{sum}) do not match"
46
+ end
47
+
48
+ end
49
+
50
+ ##############################
51
+
52
+ ContractExample.apply A, :methods => %w[buy sell]
53
+
54
+ a = A.new
55
+ a.buy 10
56
+ a.sell 10
57
+ a.sell -10
58
+
59
+ ##############################
60
+
61
+ class A
62
+ include DesignByContract
63
+
64
+ precond { |price| assert price < 0, "Price is less than 0" }
65
+ postcond { }
66
+ # invariant block will be executed before and after the method
67
+ invariant { assert @total != @transactions.reduce(&:sum), "Total and sum of transactions do not equal" }
68
+ def buy price
69
+ end
70
+ end
71
+
@@ -18,7 +18,6 @@ class ImplicitMethodOptionTest < Aspector::Base
18
18
  end
19
19
  end
20
20
 
21
- # ImplicitMethodOptionTest.apply A, :method => 'test'
22
21
  ImplicitMethodOptionTest.apply A, :method => "test"
23
22
 
24
23
  ##############################
data/lib/aspector.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'aspector/logging'
1
2
  require 'aspector/logger'
2
3
 
3
4
  require 'aspector/object_extension'
data/lib/aspector/base.rb CHANGED
@@ -64,7 +64,11 @@ module Aspector
64
64
  end
65
65
 
66
66
  def aop_logger
67
- @aop_logger ||= Logger.new(self, self.class.logger.level)
67
+ return @aop_logger if @aop_logger
68
+
69
+ @aop_logger = Logging.get_logger(self)
70
+ @aop_logger.level = self.class.logger.level
71
+ @aop_logger
68
72
  end
69
73
  alias logger aop_logger
70
74
 
@@ -120,7 +124,7 @@ module Aspector
120
124
  advices = aop_filter_advices advices, method
121
125
  return if advices.empty?
122
126
 
123
- aop_logger.log Logger::APPLY_TO_METHOD, method
127
+ aop_logger.log Logging::DEBUG, 'apply-to-method', method
124
128
  before_apply_to_method method, advices
125
129
 
126
130
  scope ||=
@@ -255,7 +259,7 @@ module Aspector
255
259
  rescue
256
260
  # ignore undefined method error
257
261
  if @aop_options[:old_methods_only]
258
- aop_logger.log Logger::METHOD_NOT_FOUND, method
262
+ aop_logger.log Logging::WARN, 'method-not-found', method
259
263
  end
260
264
 
261
265
  return
@@ -281,7 +285,7 @@ module Aspector
281
285
  aspect = self
282
286
 
283
287
  code = METHOD_TEMPLATE.result(binding)
284
- aspect.aop_logger.log Logger::GENERATE_CODE, method, code
288
+ aspect.aop_logger.log Logging::DEBUG, 'generate-code', method, code
285
289
  @aop_context.class_eval code, __FILE__, __LINE__ + 4
286
290
  end
287
291
 
@@ -293,116 +297,116 @@ module Aspector
293
297
  % end
294
298
 
295
299
  define_method :<%= method %> do |*args, &block|
296
- % if aop_logger.visible?(Logger::TRACE)
297
- aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::ENTER_GENERATED_METHOD
300
+ % if aop_logger.visible?(Logging::TRACE)
301
+ aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'enter-generated-method'
298
302
  % end
299
303
 
300
304
  if aspect.aop_disabled?
301
- % if aop_logger.visible?(Logger::TRACE)
302
- aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::EXIT_BECAUSE_DISABLED
305
+ % if aop_logger.visible?(Logging::TRACE)
306
+ aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'exit--generated-method'
303
307
  % end
304
308
  return orig_method.bind(self).call(*args, &block)
305
309
  end
306
310
 
307
311
  % if is_outermost
308
- result = catch(:aop_returns) do
312
+ result = catch(:aop_returns) do
309
313
  % end
310
314
 
311
315
  % unless before_advices.empty?
312
- # Before advices
316
+ # Before advices
313
317
  % end
314
318
  % before_advices.each do |advice|
315
- % if aop_logger.visible?(Logger::TRACE)
316
- aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::BEFORE_INVOKE_ADVICE, '<advice <%= advice.index %>>'
319
+ % if aop_logger.visible?(Logging::TRACE)
320
+ aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'before-invoke-advice', '<advice <%= advice.index %>>'
317
321
  % end
318
- result = <%= advice.with_method %> <%
319
- if advice.options[:aspect_arg] %>aspect, <% end %><%
320
- if advice.options[:method_arg] %>'<%= method %>', <% end
321
- %>*args
322
- % if aop_logger.visible?(Logger::TRACE)
323
- aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::AFTER_INVOKE_ADVICE, '<advice <%= advice.index %>>'
322
+ result = <%= advice.with_method %> <%
323
+ if advice.options[:aspect_arg] %>aspect, <% end %><%
324
+ if advice.options[:method_arg] %>'<%= method %>', <% end
325
+ %>*args
326
+ % if aop_logger.visible?(Logging::TRACE)
327
+ aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'after--invoke-advice', '<advice <%= advice.index %>>'
324
328
  % end
325
329
  % if advice.options[:skip_if_false]
326
- unless result
327
- % if aop_logger.visible?(Logger::TRACE)
328
- aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::EXIT_FROM_BEFORE_FILTER, '<advice <%= advice.index %>>'
330
+ unless result
331
+ % if aop_logger.visible?(Logging::TRACE)
332
+ aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'exit-method-due-to-before-filter', '<advice <%= advice.index %>>'
329
333
  % end
330
- return
334
+ return
331
335
  end
332
336
  % end
333
337
  % end
334
338
 
335
339
  % if around_advice
336
- # Around advice
337
- % if aop_logger.visible?(Logger::TRACE)
338
- aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::BEFORE_INVOKE_ADVICE, '<advice <%= around_advice.index %>>'
340
+ # Around advice
341
+ % if aop_logger.visible?(Logging::TRACE)
342
+ aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'before-invoke-advice', '<advice <%= around_advice.index %>>'
339
343
  % end
340
- % if aop_logger.visible?(Logger::TRACE)
341
- proxy = lambda do |*args, &block|
342
- aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::BEFORE_INVOKE_PROXY
343
- res = wrapped_method.bind(self).call *args, &block
344
- aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::AFTER_INVOKE_PROXY
345
- res
346
- end
347
- result = <%= around_advice.with_method %> <%
348
- if around_advice.options[:aspect_arg] %>aspect, <% end %><%
349
- if around_advice.options[:method_arg] %>'<%= method %>', <% end
350
- %>proxy, *args, &block
344
+ % if aop_logger.visible?(Logging::TRACE)
345
+ proxy = lambda do |*args, &block|
346
+ aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'before-invoke-proxy'
347
+ res = wrapped_method.bind(self).call *args, &block
348
+ aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'after--invoke-proxy'
349
+ res
350
+ end
351
+ result = <%= around_advice.with_method %> <%
352
+ if around_advice.options[:aspect_arg] %>aspect, <% end %><%
353
+ if around_advice.options[:method_arg] %>'<%= method %>', <% end
354
+ %>proxy, *args, &block
351
355
  % else
352
- result = <%= around_advice.with_method %> <%
353
- if around_advice.options[:aspect_arg] %>aspect, <% end %><%
354
- if around_advice.options[:method_arg] %>'<%= method %>', <% end
355
- %>wrapped_method.bind(self), *args, &block
356
+ result = <%= around_advice.with_method %> <%
357
+ if around_advice.options[:aspect_arg] %>aspect, <% end %><%
358
+ if around_advice.options[:method_arg] %>'<%= method %>', <% end
359
+ %>wrapped_method.bind(self), *args, &block
356
360
  % end
357
- % if aop_logger.visible?(Logger::TRACE)
358
- aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::AFTER_INVOKE_ADVICE, '<advice <%= around_advice.index %>>'
361
+ % if aop_logger.visible?(Logging::TRACE)
362
+ aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'after--invoke-advice', '<advice <%= around_advice.index %>>'
359
363
  % end
360
364
 
361
365
  % else
362
366
 
363
- # Invoke original method
364
- % if aop_logger.visible?(Logger::TRACE)
365
- aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::BEFORE_WRAPPED_METHOD
367
+ # Invoke original method
368
+ % if aop_logger.visible?(Logging::TRACE)
369
+ aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'before-wrapped-method'
366
370
  % end
367
- result = orig_method.bind(self).call *args, &block
368
- % if aop_logger.visible?(Logger::TRACE)
369
- aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::AFTER_WRAPPED_METHOD
371
+ result = orig_method.bind(self).call *args, &block
372
+ % if aop_logger.visible?(Logging::TRACE)
373
+ aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'after--wrapped-method'
370
374
  % end
371
375
 
372
376
  % end
373
377
 
374
378
  % unless after_advices.empty?
375
- # After advices
379
+ # After advices
376
380
  % after_advices.each do |advice|
377
- % if aop_logger.visible?(Logger::TRACE)
378
- aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::BEFORE_INVOKE_ADVICE, '<advice <%= advice.index %>>'
381
+ % if aop_logger.visible?(Logging::TRACE)
382
+ aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'before-invoke-advice', '<advice <%= advice.index %>>'
379
383
  % end
380
384
  % if advice.options[:result_arg]
381
- result = <%= advice.with_method %> <%
382
- if advice.options[:aspect_arg] %>aspect, <% end %><%
383
- if advice.options[:method_arg] %>'<%= method %>', <% end %><%
384
- if advice.options[:result_arg] %>result, <% end
385
- %>*args
385
+ result = <%= advice.with_method %> <%
386
+ if advice.options[:aspect_arg] %>aspect, <% end %><%
387
+ if advice.options[:method_arg] %>'<%= method %>', <% end %><%
388
+ if advice.options[:result_arg] %>result, <% end
389
+ %>*args
386
390
  % else
387
- <%= advice.with_method %> <%
388
- if advice.options[:aspect_arg] %>aspect, <% end %><%
389
- if advice.options[:method_arg] %>'<%= method %>', <% end
390
- %>*args
391
+ <%= advice.with_method %> <%
392
+ if advice.options[:aspect_arg] %>aspect, <% end %><%
393
+ if advice.options[:method_arg] %>'<%= method %>', <% end
394
+ %>*args
391
395
  % end
392
- % if aop_logger.visible?(Logger::TRACE)
393
- aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::AFTER_INVOKE_ADVICE, '<advice <%= advice.index %>>'
396
+ % if aop_logger.visible?(Logging::TRACE)
397
+ aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'after--invoke-advice', '<advice <%= advice.index %>>'
394
398
  % end
395
399
  % end
396
400
  % end
397
401
 
398
402
  % if is_outermost
399
- result
403
+ result
400
404
 
401
- end # end of catch
405
+ end # end of catch
402
406
  % end
403
407
 
404
- % if aop_logger.visible?(Logger::TRACE)
405
- aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::EXIT_GENERATED_METHOD
408
+ % if aop_logger.visible?(Logging::TRACE)
409
+ aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'exit--generated-method'
406
410
  % end
407
411
  result
408
412
  end
@@ -4,7 +4,7 @@ module Aspector
4
4
  ::Aspector::Base.extend(self)
5
5
 
6
6
  def aop_enable
7
- aop_logger.log Logger::ENABLE_ASPECT
7
+ aop_logger.log Logging::INFO, 'enable-aspect'
8
8
  send :define_method, :aop_disabled? do
9
9
  end
10
10
 
@@ -13,7 +13,7 @@ module Aspector
13
13
  alias enable aop_enable
14
14
 
15
15
  def aop_disable
16
- aop_logger.log Logger::DISABLE_ASPECT
16
+ aop_logger.log Logging::INFO, 'disable-aspect'
17
17
  send :define_method, :aop_disabled? do
18
18
  true
19
19
  end
@@ -22,11 +22,19 @@ module Aspector
22
22
  end
23
23
  alias disable aop_disable
24
24
 
25
+ # if ENV["ASPECTOR_LOGGER"] is set, use it
26
+ # else try to load logem and use Logem::Logger
27
+ # else use built in logger
25
28
  def aop_logger
26
- @aop_logger ||= Logger.new(self)
29
+ @aop_logger ||= Logging.get_logger(self)
27
30
  end
28
31
  alias logger aop_logger
29
32
 
33
+ def aop_logger= logger
34
+ @aop_logger = logger
35
+ end
36
+ alias logger= aop_logger=
37
+
30
38
  def aop_advices
31
39
  @aop_advices ||= []
32
40
  end
@@ -42,7 +50,7 @@ module Aspector
42
50
 
43
51
  targets = rest.unshift target
44
52
  result = targets.map do |target|
45
- aop_logger.log Logger::APPLY, target, options.inspect
53
+ aop_logger.log Logging::INFO, 'apply', target, options.inspect
46
54
  aspect_instance = new(target, options)
47
55
  aspect_instance.send :aop_apply
48
56
  aspect_instance
@@ -64,7 +72,7 @@ module Aspector
64
72
  def aop_before *methods, &block
65
73
  aop_advices << advice = aop_create_advice(Aspector::AdviceMetadata::BEFORE, self, methods, &block)
66
74
  advice.index = aop_advices.size
67
- aop_logger.log Logger::DEFINE_ADVICE, advice
75
+ aop_logger.log Logging::INFO, 'define-advice', advice
68
76
  advice
69
77
  end
70
78
  alias before aop_before
@@ -72,7 +80,7 @@ module Aspector
72
80
  def aop_before_filter *methods, &block
73
81
  aop_advices << advice = aop_create_advice(Aspector::AdviceMetadata::BEFORE_FILTER, self, methods, &block)
74
82
  advice.index = aop_advices.size
75
- aop_logger.log Logger::DEFINE_ADVICE, advice
83
+ aop_logger.log Logging::INFO, 'define-advice', advice
76
84
  advice
77
85
  end
78
86
  alias before_filter aop_before_filter
@@ -80,7 +88,7 @@ module Aspector
80
88
  def aop_after *methods, &block
81
89
  aop_advices << advice = aop_create_advice(Aspector::AdviceMetadata::AFTER, self, methods, &block)
82
90
  advice.index = aop_advices.size
83
- aop_logger.log Logger::DEFINE_ADVICE, advice
91
+ aop_logger.log Logging::INFO, 'define-advice', advice
84
92
  advice
85
93
  end
86
94
  alias after aop_after
@@ -88,7 +96,7 @@ module Aspector
88
96
  def aop_around *methods, &block
89
97
  aop_advices << advice = aop_create_advice(Aspector::AdviceMetadata::AROUND, self, methods, &block)
90
98
  advice.index = aop_advices.size
91
- aop_logger.log Logger::DEFINE_ADVICE, advice
99
+ aop_logger.log Logging::INFO, 'define-advice', advice
92
100
  advice
93
101
  end
94
102
  alias around aop_around
@@ -96,7 +104,7 @@ module Aspector
96
104
  def aop_raw *methods, &block
97
105
  aop_advices << advice = aop_create_advice(Aspector::AdviceMetadata::RAW, self, methods, &block)
98
106
  advice.index = aop_advices.size
99
- aop_logger.log Logger::DEFINE_ADVICE, advice
107
+ aop_logger.log Logging::INFO, 'define-advice', advice
100
108
  advice
101
109
  end
102
110
  alias raw aop_raw
@@ -1,67 +1,23 @@
1
1
  module Aspector
2
2
  class Logger
3
- # Log levels
4
- ERROR = 50
5
- WARN = 40
6
- INFO = 30
7
- DEBUG = 20
8
- TRACE = 10
9
-
10
- DEFAULT_VISIBLE_LEVEL = INFO
11
-
12
- # Actions
13
- DEFINE_ADVICE = ["define-advice" , INFO]
14
- APPLY = ["apply" , INFO]
15
- APPLY_TO_METHOD = ["apply-to-method", DEBUG]
16
- ENABLE_ASPECT = ["enable-aspect" , INFO]
17
- DISABLE_ASPECT = ["disable-aspect" , INFO]
18
- GENERATE_CODE = ["generate-code" , DEBUG]
19
-
20
- ENTER_GENERATED_METHOD = ["enter-generated-method", TRACE]
21
- EXIT_GENERATED_METHOD = ["exit--generated-method", TRACE]
22
- EXIT_BECAUSE_DISABLED = ["exit--because-disabled", TRACE]
23
- BEFORE_INVOKE_ADVICE = ["before-invoke-advice" , TRACE]
24
- AFTER_INVOKE_ADVICE = ["after--invoke-advice" , TRACE]
25
- BEFORE_WRAPPED_METHOD = ["before-wrapped-method" , TRACE]
26
- AFTER_WRAPPED_METHOD = ["after--wrapped-method" , TRACE]
27
- BEFORE_INVOKE_PROXY = ["before-invoke-proxy" , TRACE]
28
- AFTER_INVOKE_PROXY = ["after--invoke-proxy" , TRACE]
29
-
30
- # Unexpected behaviors
31
- METHOD_NOT_FOUND = ["method-not-found" , WARN]
32
3
 
33
4
  attr_reader :context
34
- attr_writer :level
5
+ attr_accessor :level
35
6
 
36
- def initialize context, level = nil
7
+ def initialize context
37
8
  @context = context
38
- @level = level
39
- end
40
-
41
- def level
42
- return @level if @level
43
9
 
44
10
  if (level_string = ENV['ASPECTOR_LOG_LEVEL'])
45
11
  @level = string_to_level(level_string)
46
12
  else
47
- @level = DEFAULT_VISIBLE_LEVEL
13
+ @level = Logging::DEFAULT_VISIBLE_LEVEL
48
14
  end
49
15
  end
50
16
 
51
- def log action_level, *args
52
- action, level = *action_level
53
-
54
- return if self.level > level
55
-
56
- puts log_prefix(level) << action << " | " << args.join(" | ")
57
- end
58
-
59
- def log_method_call method, action_level, *args
60
- action, level = *action_level
61
-
17
+ def log level, *args
62
18
  return if self.level > level
63
19
 
64
- puts log_prefix(level) << method << " | " << action << " | " << args.join(" | ")
20
+ puts log_prefix(level) << args.join(" | ")
65
21
  end
66
22
 
67
23
  def visible? level
@@ -71,7 +27,7 @@ module Aspector
71
27
  private
72
28
 
73
29
  def log_prefix level
74
- s = "Aspector | " << level_to_string(level) << " | "
30
+ s = "#{Time.now} | Aspector | " << level_to_string(level) << " | "
75
31
  if context.is_a? Aspector::Base
76
32
  s << context.class.to_s << " | " << context.aop_target.to_s << " | "
77
33
  else
@@ -81,24 +37,25 @@ module Aspector
81
37
 
82
38
  def level_to_string level
83
39
  case level
84
- when ERROR then "ERROR"
85
- when WARN then "WARN "
86
- when INFO then "INFO "
87
- when DEBUG then "DEBUG"
88
- when TRACE then "TRACE"
40
+ when Logging::ERROR then "ERROR"
41
+ when Logging::WARN then "WARN "
42
+ when Logging::INFO then "INFO "
43
+ when Logging::DEBUG then "DEBUG"
44
+ when Logging::TRACE then "TRACE"
89
45
  else level.to_s
90
46
  end
91
47
  end
92
48
 
93
49
  def string_to_level level_string
94
- return DEFAULT_VISIBLE_LEVEL if level_string.nil? or level_string.strip == ''
50
+ return Logging::DEFAULT_VISIBLE_LEVEL if level_string.nil? or level_string.strip == ''
95
51
 
96
52
  case level_string.downcase
97
- when 'error' then ERROR
98
- when 'warn' then WARN
99
- when 'info' then INFO
100
- when 'debug' then DEBUG
101
- when 'trace' then TRACE
53
+ when 'error' then Logging::ERROR
54
+ when 'warn' then Logging::WARN
55
+ when 'info' then Logging::INFO
56
+ when 'debug' then Logging::DEBUG
57
+ when 'trace' then Logging::TRACE
58
+ when 'none' then Logging::NONE
102
59
  end
103
60
  end
104
61
  end
@@ -0,0 +1,38 @@
1
+ module Aspector
2
+ module Logging
3
+ # Log levels
4
+ NONE = 10000
5
+ ERROR = 50
6
+ WARN = 40
7
+ INFO = 30
8
+ DEBUG = 20
9
+ TRACE = 10
10
+
11
+ DEFAULT_VISIBLE_LEVEL = INFO
12
+
13
+ def self.get_logger context
14
+ if logger_class_name = ENV["ASPECTOR_LOGGER"]
15
+ begin
16
+ logger_class = constanize(logger_class_name)
17
+ logger_class.new(context)
18
+ rescue => e
19
+ $stderr.puts e.message
20
+
21
+ Logger.new(context)
22
+ end
23
+ else
24
+ Logger.new(context)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def self.constanize class_name
31
+ unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)+)\z/ =~ class_name
32
+ raise NameError, "#{class_name} is not a valid constant name!"
33
+ end
34
+
35
+ Object.module_eval("::#{$1}", __FILE__, __LINE__)
36
+ end
37
+ end
38
+ end
@@ -20,7 +20,7 @@ module Aspector
20
20
 
21
21
  yield if block_given?
22
22
  ensure
23
- remove_instance_variable aop_applied_flag
23
+ remove_instance_variable aop_applied_flag if instance_variable_defined? aop_applied_flag
24
24
  end
25
25
  end
26
26
 
@@ -44,7 +44,9 @@ module Aspector
44
44
 
45
45
  yield if block_given?
46
46
  ensure
47
- eigen_class.send :remove_instance_variable, aop_applied_flag
47
+ if eigen_class.instance_variable_defined? aop_applied_flag
48
+ eigen_class.send :remove_instance_variable, aop_applied_flag
49
+ end
48
50
  end
49
51
  end
50
52
 
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,7 @@ ENV["ASPECTOR_LOG_LEVEL"] ||= "warn"
2
2
 
3
3
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
  $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ require 'rubygems'
5
6
  require 'rspec'
6
7
  require 'rspec/autorun'
7
8
  require 'aspector'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aspector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.2
4
+ version: 0.12.3
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: 2012-07-03 00:00:00.000000000 Z
12
+ date: 2012-10-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -199,6 +199,7 @@ files:
199
199
  - .irbrc
200
200
  - .rspec
201
201
  - .rvmrc
202
+ - .travis.yml
202
203
  - Gemfile
203
204
  - Gemfile.lock
204
205
  - Guardfile
@@ -212,6 +213,7 @@ files:
212
213
  - examples/aspector_apply_example.rb
213
214
  - examples/aspector_example.rb
214
215
  - examples/cache_aspect.rb
216
+ - examples/design_by_contract.rb
215
217
  - examples/exception_handler.rb
216
218
  - examples/implicit_method_option_test.rb
217
219
  - examples/logging_aspect.rb
@@ -225,6 +227,7 @@ files:
225
227
  - lib/aspector/deferred_logic.rb
226
228
  - lib/aspector/deferred_option.rb
227
229
  - lib/aspector/logger.rb
230
+ - lib/aspector/logging.rb
228
231
  - lib/aspector/method_matcher.rb
229
232
  - lib/aspector/module_extension.rb
230
233
  - lib/aspector/object_extension.rb
@@ -268,7 +271,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
268
271
  version: '0'
269
272
  segments:
270
273
  - 0
271
- hash: 2407230077532113164
274
+ hash: 117622238641443179
272
275
  required_rubygems_version: !ruby/object:Gem::Requirement
273
276
  none: false
274
277
  requirements: