method_observer 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 20cefcde11572c489d96dcf6c831e5c8bd327de6
4
+ data.tar.gz: b6b9051cce0d45c2ede7cd7c7832c1043710af1e
5
+ SHA512:
6
+ metadata.gz: 3631407a89405b4ed1e872c921b7e30eb4ccc17a5a5f993c5130ef345b2c8a55adcff6a2c8cc403e11bd1df189db24513c28c96812bc3552619aea5b1df49a49
7
+ data.tar.gz: 9c3846a9fcbd96a5c139e2997257ea0890dd58df87b5ccc6bb5ae7f38f9b5fe6edf07efd2a21e3175402265b0374038a206dfb2eda15b02ba03d7066b61074b9
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1 @@
1
+ method_observer
@@ -0,0 +1 @@
1
+ ruby 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in method_observer.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Gabriel Naiman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,32 @@
1
+ # MethodObserver
2
+
3
+ Observe class and instance methods
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'method_observer'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install method_observer
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gabynaiman/method_observer.
28
+
29
+ ## License
30
+
31
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
32
+
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task :console do
4
+ require 'pry'
5
+ require 'method_observer'
6
+ ARGV.clear
7
+ Pry.start
8
+ end
@@ -0,0 +1,44 @@
1
+ require_relative '../lib/method_observer'
2
+
3
+ class Dummy
4
+ def foo
5
+ puts 'Running foo'
6
+ bar
7
+ end
8
+
9
+ def bar
10
+ puts 'Running bar'
11
+ end
12
+ end
13
+
14
+ class Profiler
15
+
16
+ def initialize
17
+ @nesting_level = 0
18
+ end
19
+
20
+ def track(klass, target, method, *args, &block)
21
+ @nesting_level += 1
22
+ nesting = ' ' * (@nesting_level - 1)
23
+ puts "#{nesting}Before: #{klass}#{target == :class ? '.' : '#'}#{method}"
24
+ result = block.call
25
+ puts "#{nesting}After: #{klass}#{target == :class ? '.' : '#'}#{method}"
26
+ @nesting_level -= 1
27
+ result
28
+ end
29
+
30
+ end
31
+
32
+
33
+ MethodObserver.configure do |config|
34
+ profiler = Profiler.new
35
+
36
+ config.around do |klass, target, method, *args, &block|
37
+ profiler.track klass, target, method, *args, &block
38
+ end
39
+
40
+ config.observe Dummy, instance: [:foo, :bar]
41
+ end
42
+
43
+ dummy = Dummy.new
44
+ dummy.foo
@@ -0,0 +1,38 @@
1
+ require_relative '../lib/method_observer'
2
+
3
+ class Dummy
4
+ def self.foo
5
+ puts 'Running foo'
6
+ end
7
+
8
+ def bar
9
+ puts 'Running bar'
10
+ end
11
+ end
12
+
13
+
14
+ MethodObserver.configure do |config|
15
+ config.before do |klass, target, method, *args|
16
+ puts "Before: #{klass}#{target == :class ? '.' : '#'}#{method}"
17
+ end
18
+
19
+ config.after do |klass, target, method, *args|
20
+ puts "After: #{klass}#{target == :class ? '.' : '#'}#{method}"
21
+ puts
22
+ end
23
+
24
+ config.around do |klass, target, method, *args, &block|
25
+ puts "Around before: #{klass}#{target == :class ? '.' : '#'}#{method}"
26
+ block.call
27
+ puts "Around after: #{klass}#{target == :class ? '.' : '#'}#{method}"
28
+ end
29
+
30
+ config.observe Dummy, class: [:foo], instance: [:bar]
31
+ end
32
+
33
+ Dummy.foo
34
+ Dummy.name
35
+
36
+ dummy = Dummy.new
37
+ dummy.bar
38
+ dummy.to_s
@@ -0,0 +1,74 @@
1
+ class MethodObserver
2
+
3
+ VERSION = '0.0.1'
4
+
5
+ @before = ->(klass, target, method, *args, &block) {}
6
+ @after = ->(klass, target, method, *args, &block) {}
7
+ @around = ->(klass, target, method, *args, &block) { block.call }
8
+
9
+ class << self
10
+
11
+ alias_method :configure, :tap
12
+
13
+ def before(&block)
14
+ @before = block
15
+ end
16
+
17
+ def after(&block)
18
+ @after = block
19
+ end
20
+
21
+ def around(&block)
22
+ @around = block
23
+ end
24
+
25
+ def observe(klass, options)
26
+ options.fetch(:instance, []).each do |method|
27
+ observe_instance_method klass, method
28
+ end
29
+
30
+ options.fetch(:class, []).each do |method|
31
+ observe_class_method klass, method
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def observe_instance_method(klass, method)
38
+ observe_before = @before
39
+ observe_after = @after
40
+ observe_around = @around
41
+
42
+ observed_method = "__#{method}_observed__"
43
+ klass.send :alias_method, observed_method, method
44
+
45
+ klass.send(:define_method, method) do |*args, &block|
46
+ observe_before.call klass, :instance, method, args
47
+ result = observe_around.call klass, :instance, method, args do
48
+ send observed_method, *args, &block
49
+ end
50
+ observe_after.call klass, :instance, method, args
51
+ result
52
+ end
53
+ end
54
+
55
+ def observe_class_method(klass, method)
56
+ observe_before = @before
57
+ observe_after = @after
58
+ observe_around = @around
59
+
60
+ observed_method = "__#{method}_observed__"
61
+ klass.singleton_class.send :alias_method, observed_method, method
62
+
63
+ klass.send(:define_singleton_method, method) do |*args, &block|
64
+ observe_before.call klass, :class, method, args
65
+ result = observe_around.call klass, :class, method, args do
66
+ send observed_method, *args, &block
67
+ end
68
+ observe_after.call klass, :class, method, args
69
+ result
70
+ end
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'method_observer'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'method_observer'
8
+ spec.version = MethodObserver::VERSION
9
+ spec.authors = ['Gabriel Naiman']
10
+ spec.email = ['gabynaiman@gmail.com']
11
+ spec.description = 'Observe class and instance methods'
12
+ spec.summary = 'Observe class and instance methods'
13
+ spec.homepage = 'https://github.com/gabynaiman/method_observer'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.5'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'pry-nav'
24
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: method_observer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Naiman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-nav
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Observe class and instance methods
56
+ email:
57
+ - gabynaiman@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".ruby-gemset"
64
+ - ".ruby-version"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - examples/nesting.rb
70
+ - examples/puts.rb
71
+ - lib/method_observer.rb
72
+ - method_observer.gemspec
73
+ homepage: https://github.com/gabynaiman/method_observer
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Observe class and instance methods
97
+ test_files: []