inspector_gadget 0.0.2 → 0.1.0

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.
@@ -1,37 +1,51 @@
1
1
  module InspectorGadget
2
+
2
3
  def self.included(base)
3
- base.class_eval do
4
+ # log all instance methods
5
+ Brain.log_all_instance_method_calls(base)
6
+
7
+ # log all class methods
8
+ singleton_class = class << base; self; end
9
+ Brain.log_all_instance_method_calls(singleton_class)
10
+ end
11
+
12
+ module Brain
13
+ # obj either a class (instance methods) or a class singleton (class methods)
14
+ def self.log_all_instance_method_calls(obj)
4
15
  # setting up alias_method_chain
5
- (instance_methods - Object.methods).each do |method_name|
6
- method_name = method_name.to_s # convert symbol to string
7
- if ['?', '='].include?(method_name[-1])
8
- special_character = method_name[-1]
9
- bare_method_name = method_name[0..-2] # get rid of trailing "=" or "?"
10
- method_with_logging_name = bare_method_name + '_with_logging' + special_character
11
- method_without_logging_name = bare_method_name + '_without_logging' + special_character
12
- else
13
- method_with_logging_name = method_name + '_with_logging'
14
- method_without_logging_name = method_name + '_without_logging'
15
- end
16
- define_method(method_with_logging_name) do |*args|
17
- args_to_log = args.dup
18
- filter_args(method_name, args_to_log)
19
- Rails.logger.info "#{self.class.name} LOG: method name: #{method_name}, args: #{args_to_log.inspect}"
20
- result = send(method_without_logging_name, *args)
21
- result_to_log = result.dup rescue result
22
- filter_result(method_name, result_to_log)
23
- Rails.logger.info "#{self.class.name} LOG: method name: #{method_name}, args: #{args_to_log.inspect} returned #{result_to_log.inspect}"
24
- result
25
- end
16
+ obj.class_eval do
17
+ (instance_methods - Object.methods).each do |method_name|
18
+ method_name = method_name.to_s # convert symbol to string
19
+ aliased_target, punctuation = method_name.to_s.sub(/([?!=])$/, ''), $1
20
+ method_with_logging_name, method_without_logging_name = "#{aliased_target}_with_logging#{punctuation}", "#{aliased_target}_without_logging#{punctuation}"
26
21
 
27
- alias_method_chain method_name, :logging
22
+ define_method(method_with_logging_name) do |*args, &block|
23
+ args_to_log = args.dup
24
+ filter_args_in_log(method_name, args_to_log)
25
+ if self.is_a?(Class)
26
+ method_type = 'class'
27
+ class_name = self.name
28
+ else
29
+ method_type = 'instance'
30
+ class_name = self.class.name
31
+ end
32
+ Rails.logger.info("#{class_name} LOG: #{method_type} method name: #{method_name}, args: #{args_to_log.inspect}")
33
+ result = send(method_without_logging_name, *args, &block)
34
+ result_to_log = result.dup rescue result
35
+ filter_result_in_log(method_name, result_to_log)
36
+ Rails.logger.info("#{class_name} LOG: #{method_type} method name: #{method_name}, args: #{args_to_log.inspect} returned #{result_to_log.inspect}")
37
+ result
38
+ end
28
39
 
29
- # override this method to hide passwords etc
30
- def filter_args(method_name, args)
31
- end
40
+ alias_method_chain method_name, :logging
41
+
42
+ # override this method to hide passwords etc
43
+ def filter_args_in_log(method_name, args)
44
+ end
32
45
 
33
- # override this method to hide passwords etc
34
- def filter_result(method_name, result)
46
+ # override this method to hide passwords etc
47
+ def filter_result_in_log(method_name, result)
48
+ end
35
49
  end
36
50
  end
37
51
  end
@@ -1,3 +1,3 @@
1
1
  module InspectorGadget
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
Binary file
@@ -0,0 +1,83 @@
1
+ require 'active_support/all'
2
+ require './lib/inspector_gadget.rb'
3
+
4
+ describe InspectorGadget do
5
+ before :all do
6
+ module Rails;end
7
+
8
+ def Rails.logger
9
+ @logger ||= Object.new
10
+ end unless Rails.respond_to?(:logger)
11
+
12
+ class Test
13
+ def test_instance_method
14
+ 'instance_method'
15
+ end
16
+
17
+ def self.test_class_method
18
+ 'class_method'
19
+ end
20
+
21
+ def test_instance_method_with_arg(arg)
22
+ arg
23
+ end
24
+
25
+ def self.test_class_method_with_arg(arg)
26
+ arg
27
+ end
28
+
29
+ def test_instance_method_with_block(&block)
30
+ block.call
31
+ end
32
+
33
+ def self.test_class_method_with_block(&block)
34
+ block.call
35
+ end
36
+
37
+ include InspectorGadget
38
+ end
39
+
40
+ end
41
+
42
+ it 'should log instance methods' do
43
+ Rails.logger.should_receive(:info).with('Test LOG: instance method name: test_instance_method, args: []')
44
+ Rails.logger.should_receive(:info).with('Test LOG: instance method name: test_instance_method, args: [] returned "instance_method"')
45
+ Test.new.test_instance_method.should == 'instance_method'
46
+ end
47
+
48
+ it 'should log class methods' do
49
+ Rails.logger.should_receive(:info).with('Test LOG: class method name: test_class_method, args: []')
50
+ Rails.logger.should_receive(:info).with('Test LOG: class method name: test_class_method, args: [] returned "class_method"')
51
+ Test.test_class_method.should == 'class_method'
52
+ end
53
+
54
+ it 'should log instance methods with args' do
55
+ Rails.logger.should_receive(:info).with('Test LOG: instance method name: test_instance_method_with_arg, args: ["foo"]')
56
+ Rails.logger.should_receive(:info).with('Test LOG: instance method name: test_instance_method_with_arg, args: ["foo"] returned "foo"')
57
+ Test.new.test_instance_method_with_arg('foo').should == 'foo'
58
+ end
59
+
60
+ it 'should log class methods with args' do
61
+ Rails.logger.should_receive(:info).with('Test LOG: class method name: test_class_method_with_arg, args: ["foo"]')
62
+ Rails.logger.should_receive(:info).with('Test LOG: class method name: test_class_method_with_arg, args: ["foo"] returned "foo"')
63
+ Test.test_class_method_with_arg('foo').should == 'foo'
64
+ end
65
+
66
+ it 'should log instance methods with block' do
67
+ Rails.logger.should_receive(:info).with('Test LOG: instance method name: test_instance_method_with_block, args: []')
68
+ Rails.logger.should_receive(:info).with('Test LOG: instance method name: test_instance_method_with_block, args: [] returned 2')
69
+ Test.new.test_instance_method_with_block do
70
+ 1 + 1
71
+ end.should == 2
72
+ end
73
+
74
+ it 'should log class methods with block' do
75
+ Rails.logger.should_receive(:info).with('Test LOG: class method name: test_class_method_with_block, args: []')
76
+ Rails.logger.should_receive(:info).with('Test LOG: class method name: test_class_method_with_block, args: [] returned 2')
77
+ Test.test_class_method_with_block do
78
+ 1 + 1
79
+ end.should == 2
80
+ end
81
+
82
+ end
83
+
metadata CHANGED
@@ -1,90 +1,64 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: inspector_gadget
3
- version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 0
9
- - 2
10
- version: 0.0.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - mdobrota@tribune.com
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-10-29 00:00:00 -05:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2012-10-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: bundler
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &2153917080 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 23
30
- segments:
31
- - 1
32
- - 0
33
- - 0
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
34
21
  version: 1.0.0
35
22
  type: :development
36
- version_requirements: *id001
23
+ prerelease: false
24
+ version_requirements: *2153917080
37
25
  description: Inspect any object's methods
38
- email:
26
+ email:
39
27
  - mdobrota@tribune.com
40
28
  executables: []
41
-
42
29
  extensions: []
43
-
44
30
  extra_rdoc_files: []
45
-
46
- files:
31
+ files:
47
32
  - .gitignore
48
33
  - Gemfile
49
34
  - Rakefile
50
35
  - inspector_gadget.gemspec
51
36
  - lib/inspector_gadget.rb
52
37
  - lib/inspector_gadget/version.rb
53
- has_rdoc: true
38
+ - spec/.inspector_gadget_spec.rb.swp
39
+ - spec/inspector_gadget_spec.rb
54
40
  homepage: http://rubygems.org/gems/inspector_gadget
55
41
  licenses: []
56
-
57
42
  post_install_message:
58
43
  rdoc_options: []
59
-
60
- require_paths:
44
+ require_paths:
61
45
  - lib
62
- required_ruby_version: !ruby/object:Gem::Requirement
46
+ required_ruby_version: !ruby/object:Gem::Requirement
63
47
  none: false
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- hash: 3
68
- segments:
69
- - 0
70
- version: "0"
71
- required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
53
  none: false
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- hash: 23
77
- segments:
78
- - 1
79
- - 3
80
- - 6
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
81
57
  version: 1.3.6
82
58
  requirements: []
83
-
84
59
  rubyforge_project: inspector_gadget
85
- rubygems_version: 1.3.7
60
+ rubygems_version: 1.8.10
86
61
  signing_key:
87
62
  specification_version: 3
88
63
  summary: Inspect any object's methods by including the module
89
64
  test_files: []
90
-