after_do 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0ef93b03d163368d4323b3b457a104fbcbf8dde4
4
+ data.tar.gz: c3efe779604f230849918900543f1a4a2d68d6f7
5
+ SHA512:
6
+ metadata.gz: 384930d2ac61d25b48cd269db52679d8d0860ef0bfa5c13621c55ba26af30bb83b12aeb5f2ad0f58065d9c1d71dada4d5a784d876c09374e3ed6c17de75f8ec1
7
+ data.tar.gz: 2bf8dd100317b5a6ec541ddd514288eae869b4b0930d3fe6a28ef08bda302837e8c84e32f1722fd1021edec6c100de846a46cd4a53213d128e900a7fdf2c90ee
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1 @@
1
+ after_do
@@ -0,0 +1 @@
1
+ ruby-2.0.0-p195
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0
5
+ - jruby-19mode
6
+ - rbx-19mode
7
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in after_do.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tobias Pfeiffer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,67 @@
1
+ # AfterDo [![Build Status](https://travis-ci.org/PragTob/after_do.png?branch=master)](https://travis-ci.org/PragTob/after_do)[![Code Climate](https://codeclimate.com/github/PragTob/after_do.png)](https://codeclimate.com/github/PragTob/after_do)
2
+
3
+ AfterDo is simple gem, that allows you to execute a specified block after specified method of a class are called. If the class extends `AfterDo` you can simply do this by `MyClass.after :some_method do puts 'whatever you want?' end`
4
+
5
+ This shall not be done to to alter behavior or something but rather to fight cross-cutting concerns such as logging. E.g. with logging you litter all your code wit logging statements - that concern is spread over many files. With AfterDo you could put all the logging in one file.
6
+
7
+ AfterDo has no external runtime dependencies and the code is just a bit more than 60 lines (blank lines included).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'after_do'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install after_do
22
+
23
+ ## Usage
24
+
25
+ With AfterDo you can do simple things like putting something out everything a method is called as in this example:
26
+
27
+ ```ruby
28
+ class Dog
29
+ def bark
30
+ puts 'Woooof'
31
+ end
32
+
33
+ def eat
34
+ puts 'yummie!'
35
+ end
36
+ end
37
+
38
+ Dog.extend AfterDo
39
+ Dog.after :bark do puts 'I just heard a dog bark!' end
40
+
41
+ dog = Dog.new
42
+ dog2 = Dog.new
43
+
44
+ dog.bark
45
+ dog.eat
46
+ dog2.bark
47
+
48
+ # Output is:
49
+ # Woooof
50
+ # I just heard a dog bark!
51
+ # yummie!
52
+ # Woooof
53
+ # I just heard a dog bark!
54
+
55
+ ```
56
+
57
+ ## Is there a before method?
58
+
59
+ No not yet, I didn't have a use case for it yet. If you have one please let me know, it is relatively easy to add.
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'after_do/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "after_do"
8
+ spec.version = AfterDo::VERSION
9
+ spec.authors = ["Tobias Pfeiffer"]
10
+ spec.email = ["pragtob@gmail.com"]
11
+ spec.description = %q{after_do is a gem that let's you execute a block of your choice after a specific method was called on a class.}
12
+ spec.summary = %q{after_do allows you to do simple after hooks on methods}
13
+ spec.homepage = "https://github.com/PragTob/after_do"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "simplecov"
24
+ spec.add_development_dependency "coveralls"
25
+ end
@@ -0,0 +1,62 @@
1
+ module AfterDo
2
+ ALIAS_PREFIX = '__after_do_orig_'
3
+
4
+ def _after_do_callbacks
5
+ @_after_do_callbacks
6
+ end
7
+
8
+ def after(*methods, &block)
9
+ @_after_do_callbacks ||= Hash.new([])
10
+ methods.flatten! #in case someone used an Array
11
+ if methods.empty?
12
+ raise ArgumentError, 'after takes at least one method name!'
13
+ end
14
+ methods.each do |method|
15
+ if _after_do_method_already_renamed?(method)
16
+ _after_do_make_after_do_version_of_method(method)
17
+ end
18
+ @_after_do_callbacks[method] << block
19
+ end
20
+ end
21
+
22
+ def remove_all_callbacks
23
+ if @_after_do_callbacks
24
+ @_after_do_callbacks.keys.each do |key| @_after_do_callbacks[key] = [] end
25
+ end
26
+ end
27
+
28
+ private
29
+ def _after_do_make_after_do_version_of_method(method)
30
+ @_after_do_callbacks[method] = []
31
+ alias_name = _after_do_aliased_name method
32
+ _after_do_rename_old_method(method, alias_name)
33
+ _after_do_redefine_method_with_callback(method, alias_name)
34
+ end
35
+
36
+ def _after_do_aliased_name(symbol)
37
+ (ALIAS_PREFIX + symbol.to_s).to_sym
38
+ end
39
+
40
+ def _after_do_rename_old_method(old_name, new_name)
41
+ class_eval do
42
+ alias_method new_name, old_name
43
+ private new_name
44
+ end
45
+ end
46
+
47
+ def _after_do_redefine_method_with_callback(method, alias_name)
48
+ class_eval do
49
+ define_method method do |*args|
50
+ return_value = send(alias_name, *args)
51
+ self.class._after_do_callbacks[method].each do |block|
52
+ block.call *args, self
53
+ end
54
+ return_value
55
+ end
56
+ end
57
+ end
58
+
59
+ def _after_do_method_already_renamed?(method)
60
+ !private_method_defined? _after_do_aliased_name(method)
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module AfterDo
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,21 @@
1
+ require File.expand_path('../../lib/after_do', __FILE__)
2
+
3
+ class Dog
4
+ def bark
5
+ puts 'Woooof'
6
+ end
7
+
8
+ def eat
9
+ puts 'yummie!'
10
+ end
11
+ end
12
+
13
+ Dog.extend AfterDo
14
+ Dog.after :bark do puts 'I just heard a dog bark!' end
15
+
16
+ dog = Dog.new
17
+ dog2 = Dog.new
18
+
19
+ dog.bark
20
+ dog.eat
21
+ dog2.bark
@@ -0,0 +1,143 @@
1
+ require 'spec_helper'
2
+
3
+ describe AfterDo do
4
+ let(:dummy_instance) {@dummy_class.new}
5
+ let(:mockie) {double}
6
+
7
+ before :each do
8
+ redefine_dummy_class
9
+ end
10
+
11
+ def redefine_dummy_class
12
+ @dummy_class = Class.new do
13
+ extend AfterDo
14
+ def zero
15
+ 0
16
+ end
17
+
18
+ def one(param)
19
+ param
20
+ end
21
+
22
+ def two(param1, param2)
23
+ param2
24
+ end
25
+ end
26
+ end
27
+
28
+
29
+ it 'responds to after' do
30
+ @dummy_class.should respond_to :after
31
+ end
32
+
33
+ it 'calls a method on the injected mockie' do
34
+ mockie.should_receive :call_method
35
+ @dummy_class.after :zero do mockie.call_method end
36
+ dummy_instance.zero
37
+ end
38
+
39
+ it 'does not change the return value' do
40
+ before = dummy_instance.zero
41
+ @dummy_class.after :zero do 42 end
42
+ after = dummy_instance.zero
43
+ after.should eq before
44
+ end
45
+
46
+ it 'marks the copied method as private' do
47
+ @dummy_class.after :zero do end
48
+ copied_method_name = (AfterDo::ALIAS_PREFIX + 'zero').to_sym
49
+ dummy_instance.respond_to?(copied_method_name).should be_false
50
+ end
51
+
52
+ it 'can add multiple call backs' do
53
+ mockie.should_receive :call_method
54
+ mock2 = double
55
+ mock2.should_receive :call_another_method
56
+ mock3 = double
57
+ mock3.should_receive :bla
58
+ @dummy_class.after :zero do mockie.call_method end
59
+ @dummy_class.after :zero do mock2.call_another_method end
60
+ @dummy_class.after :zero do mock3.bla end
61
+ dummy_instance.zero
62
+ end
63
+
64
+ it 'can remove all callbacks' do
65
+ mockie.should_not_receive :call_method
66
+ @dummy_class.after :zero do mockie.call_method end
67
+ @dummy_class.remove_all_callbacks
68
+ dummy_instance.zero
69
+ end
70
+
71
+ describe 'with parameters' do
72
+
73
+ before :each do
74
+ mockie.should_receive :call_method
75
+ end
76
+
77
+ it 'can handle methods with a parameter' do
78
+ @dummy_class.after :one do mockie.call_method end
79
+ dummy_instance.one 5
80
+ end
81
+
82
+ it 'can handle methods with 2 parameters' do
83
+ @dummy_class.after :two do mockie.call_method end
84
+ dummy_instance.two 5, 8
85
+ end
86
+ end
87
+
88
+ describe 'with parameters for the given block' do
89
+ it 'can handle one block parameter' do
90
+ mockie.should_receive(:call_method).with(5)
91
+ @dummy_class.after :one do |i| mockie.call_method i end
92
+ dummy_instance.one 5
93
+ end
94
+
95
+ it 'can handle two block parameters' do
96
+ mockie.should_receive(:call_method).with(5, 8)
97
+ @dummy_class.after :two do |i, j| mockie.call_method i, j end
98
+ dummy_instance.two 5, 8
99
+ end
100
+ end
101
+
102
+ describe 'multiple methods' do
103
+ def call_all_3_methods
104
+ dummy_instance.zero
105
+ dummy_instance.one 4
106
+ dummy_instance.two 4, 5
107
+ end
108
+
109
+ it 'can take multiple method names as arguments' do
110
+ mockie.should_receive(:call_method).exactly(3).times
111
+ @dummy_class.after :zero, :one, :two do mockie.call_method end
112
+ call_all_3_methods
113
+ end
114
+
115
+ it 'can get the methods as an Array' do
116
+ mockie.should_receive(:call_method).exactly(3).times
117
+ @dummy_class.after [:zero, :one, :two] do mockie.call_method end
118
+ call_all_3_methods
119
+ end
120
+
121
+ it 'raises an error when no method is specified' do
122
+ expect do
123
+ @dummy_class.after do mockie.call_method end
124
+ end.to raise_error ArgumentError
125
+ end
126
+ end
127
+
128
+ describe 'it can get a hold of self, if needbe' do
129
+ it 'works for a method without arguments' do
130
+ mockie.should_receive(:call_method).with(dummy_instance)
131
+ @dummy_class.after :zero do |object| mockie.call_method(object) end
132
+ dummy_instance.zero
133
+ end
134
+
135
+ it 'works for a method with 2 arguments' do
136
+ mockie.should_receive(:call_method).with(1, 2, dummy_instance)
137
+ @dummy_class.after :two do |first, second, object|
138
+ mockie.call_method(first, second, object)
139
+ end
140
+ dummy_instance.two 1, 2
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,8 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
4
+ Coveralls::SimpleCov::Formatter,
5
+ SimpleCov::Formatter::HTMLFormatter
6
+ ]
7
+ SimpleCov.start
8
+ require 'after_do'
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: after_do
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tobias Pfeiffer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-10 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: after_do is a gem that let's you execute a block of your choice after
84
+ a specific method was called on a class.
85
+ email:
86
+ - pragtob@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - .ruby-gemset
93
+ - .ruby-version
94
+ - .travis.yml
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - after_do.gemspec
100
+ - lib/after_do.rb
101
+ - lib/after_do/version.rb
102
+ - samples/dog.rb
103
+ - spec/after_do_spec.rb
104
+ - spec/spec_helper.rb
105
+ homepage: https://github.com/PragTob/after_do
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.0.3
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: after_do allows you to do simple after hooks on methods
129
+ test_files:
130
+ - spec/after_do_spec.rb
131
+ - spec/spec_helper.rb