method_hooks 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a61c8959ce0b2aceec159ca529e2f3fde6b6e5ee
4
+ data.tar.gz: f6e790a079d9244183e69012fa720b88d4771e18
5
+ SHA512:
6
+ metadata.gz: 1ba49d70657b76e9e9ec0e90f322376e786784127fc21f147c75edcc53805258f9ebad9c1c05d76aea0d8cfbfe81ee11fa71be2f21b63e2e8873ad41b6245584
7
+ data.tar.gz: 1f43024320816a1de2139da56491110a692ce42bcd14333351fc1f8b0ab057b94f1603521c4492237053af6cb041c241c97a5527b296f32dc5cefa9815bfccf4
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in method_hooks.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Frank Bonetti
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.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # MethodHooks
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'method_hooks'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install method_hooks
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ class Model
25
+ extend Callbacks
26
+
27
+ before :save do
28
+ puts 'before'
29
+ end
30
+
31
+ around :save do |method|
32
+ puts 'before_around'
33
+ method.call
34
+ puts 'after_around'
35
+ end
36
+
37
+ after :save, :foo do
38
+ puts 'after'
39
+ end
40
+
41
+ def save
42
+ puts 'save'
43
+ end
44
+
45
+ def foo
46
+ puts 'foo'
47
+ end
48
+
49
+ end
50
+
51
+ model = Model.new
52
+ model.save
53
+ model.foo
54
+
55
+ =begin
56
+
57
+ Outputs the following:
58
+
59
+ before
60
+ before_around
61
+ save
62
+ after_around
63
+ after
64
+ foo
65
+ after
66
+
67
+ =end
68
+ ```
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it ( https://github.com/[my-github-username]/method_hooks/fork )
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/examples/model.rb ADDED
@@ -0,0 +1,44 @@
1
+ class Model
2
+ extend Callbacks
3
+
4
+ before :save do
5
+ puts 'before'
6
+ end
7
+
8
+ around :save do |method|
9
+ puts 'before_around'
10
+ method.call
11
+ puts 'after_around'
12
+ end
13
+
14
+ after :save, :foo do
15
+ puts 'after'
16
+ end
17
+
18
+ def save
19
+ puts 'save'
20
+ end
21
+
22
+ def foo
23
+ puts 'foo'
24
+ end
25
+
26
+ end
27
+
28
+ model = Model.new
29
+ model.save
30
+ model.foo
31
+
32
+ =begin
33
+
34
+ Outputs the following:
35
+
36
+ before
37
+ before_around
38
+ save
39
+ after_around
40
+ after
41
+ foo
42
+ after
43
+
44
+ =end
@@ -0,0 +1,3 @@
1
+ module MethodHooks
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,97 @@
1
+ require "method_hooks/version"
2
+
3
+ module MethodHooks
4
+ @new_method = true
5
+
6
+ def self.extended(base)
7
+ base.send :include, InstanceMethods
8
+ end
9
+
10
+ private
11
+
12
+ def method_added(method_name)
13
+ return if @new_method == false || [:call_before_callbacks, :call_around_callbacks, :call_after_callbacks].include?(method_name)
14
+
15
+ method = instance_method(method_name)
16
+ undef_method(method_name)
17
+
18
+ @new_method = false
19
+
20
+ define_method(method_name) do |*args, &block|
21
+ call_before_callbacks(method_name)
22
+ return_value = call_around_callbacks(method_name) { method.bind(self).call(*args, &block) }
23
+ call_after_callbacks(method_name)
24
+
25
+ return_value
26
+ end
27
+
28
+ @new_method = true
29
+ end
30
+
31
+ def before_callbacks
32
+ @before_callbacks ||= Hash.new { |hash, key| hash[key] = Array.new }
33
+ end
34
+
35
+ def around_callbacks
36
+ @around_callbacks ||= Hash.new { |hash, key| hash[key] = Array.new }
37
+ end
38
+
39
+ def after_callbacks
40
+ @after_callbacks ||= Hash.new { |hash, key| hash[key] = Array.new }
41
+ end
42
+
43
+ def before(*method_names, &block)
44
+ method_names.each do |method_name|
45
+ before_callbacks[method_name] << block
46
+ end
47
+ end
48
+
49
+ def around(*method_names, &block)
50
+ method_names.each do |method_name|
51
+ around_callbacks[method_name] << block
52
+ end
53
+ end
54
+
55
+ def after(*method_names, &block)
56
+ method_names.each do |method_name|
57
+ after_callbacks[method_name] << block
58
+ end
59
+ end
60
+
61
+ module InstanceMethods
62
+
63
+ private
64
+
65
+ def call_before_callbacks(method_name)
66
+ callbacks = self.class.send(:before_callbacks)[method_name]
67
+ callbacks.each do |callback|
68
+ instance_eval(&callback)
69
+ end
70
+ end
71
+
72
+ def call_around_callbacks(method_name, &block)
73
+ callbacks = self.class.send(:around_callbacks)[method_name]
74
+ return_value = nil
75
+
76
+ method = -> { return_value = block.call }
77
+
78
+ if callbacks.empty?
79
+ method.call
80
+ else
81
+ callbacks.each do |callback|
82
+ instance_exec(method, &callback)
83
+ end
84
+ end
85
+
86
+ return_value
87
+ end
88
+
89
+ def call_after_callbacks(method_name)
90
+ callbacks = self.class.send(:after_callbacks)[method_name]
91
+ callbacks.each do |callback|
92
+ instance_eval(&callback)
93
+ end
94
+ end
95
+
96
+ end
97
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'method_hooks/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "method_hooks"
8
+ spec.version = MethodHooks::VERSION
9
+ spec.authors = ["Frank Bonetti"]
10
+ spec.email = ["frank.r.bonetti@gmail.com"]
11
+ spec.summary = %q{Rails-style method hooks for plain old Ruby objects}
12
+ spec.description = %q{Rails-style method hooks for plain old Ruby objects}
13
+ spec.homepage = "https://github.com/fbonetti/method_hooks"
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.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+ require 'pry'
3
+
4
+ describe MethodHooks do
5
+
6
+ before do
7
+ Object.send(:remove_const, :Base) if Object.const_defined?(:Base)
8
+
9
+ class Base
10
+ extend MethodHooks
11
+
12
+ attr_reader :events
13
+
14
+ def initialize
15
+ @events = []
16
+ end
17
+
18
+ def save
19
+ @events << 'save'
20
+ end
21
+ end
22
+ end
23
+
24
+ describe '::before' do
25
+
26
+ it 'should call the before block' do
27
+ Base.instance_eval do
28
+ before(:save) { @events << 'before' }
29
+ end
30
+
31
+ base = Base.new
32
+ base.save
33
+
34
+ expect(base.events).to eq(['before', 'save'])
35
+ end
36
+
37
+ it 'should allow multiple before hooks on the same method' do
38
+ Base.instance_eval do
39
+ before(:save) { @events << 'first before' }
40
+ before(:save) { @events << 'second before' }
41
+ end
42
+
43
+ base = Base.new
44
+ base.save
45
+
46
+ expect(base.events).to eq(['first before', 'second before', 'save'])
47
+ end
48
+
49
+ end
50
+
51
+ describe '::around' do
52
+
53
+ it 'should call the around block' do
54
+ Base.instance_eval do
55
+ around(:save) do |method|
56
+ @events << 'before_around'
57
+ method.call
58
+ @events << 'after_around'
59
+ end
60
+ end
61
+
62
+ base = Base.new
63
+ base.save
64
+
65
+ expect(base.events).to eq(['before_around', 'save', 'after_around'])
66
+ end
67
+
68
+ end
69
+
70
+ describe '::after' do
71
+
72
+ it 'should call the after block' do
73
+ Base.instance_eval do
74
+ after(:save) { @events << 'after' }
75
+ end
76
+
77
+ base = Base.new
78
+ base.save
79
+
80
+ expect(base.events).to eq(['save', 'after'])
81
+ end
82
+
83
+ end
84
+
85
+ end
@@ -0,0 +1,91 @@
1
+ require 'method_hooks'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
6
+ # file to always be loaded, without a need to explicitly require it in any files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # will add to the boot time of your test suite on EVERY test run, even for an
11
+ # individual file that may not need all of that loaded. Instead, consider making
12
+ # a separate helper file that requires the additional dependencies and performs
13
+ # the additional setup, and require it from the spec files that actually need it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+ =begin
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
54
+ # For more details, see:
55
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
56
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
57
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
58
+ config.disable_monkey_patching!
59
+
60
+ # This setting enables warnings. It's recommended, but in some cases may
61
+ # be too noisy due to issues in dependencies.
62
+ config.warnings = true
63
+
64
+ # Many RSpec users commonly either run the entire suite or an individual
65
+ # file, and it's useful to allow more verbose output when running an
66
+ # individual spec file.
67
+ if config.files_to_run.one?
68
+ # Use the documentation formatter for detailed output,
69
+ # unless a formatter has already been configured
70
+ # (e.g. via a command-line flag).
71
+ config.default_formatter = 'doc'
72
+ end
73
+
74
+ # Print the 10 slowest examples and example groups at the
75
+ # end of the spec run, to help surface which specs are running
76
+ # particularly slow.
77
+ config.profile_examples = 10
78
+
79
+ # Run specs in random order to surface order dependencies. If you find an
80
+ # order dependency and want to debug it, you can fix the order by providing
81
+ # the seed, which is printed after each run.
82
+ # --seed 1234
83
+ config.order = :random
84
+
85
+ # Seed global randomization in this process using the `--seed` CLI option.
86
+ # Setting this allows you to use `--seed` to deterministically reproduce
87
+ # test failures related to randomization by passing the same `--seed` value
88
+ # as the one that triggered the failure.
89
+ Kernel.srand config.seed
90
+ =end
91
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: method_hooks
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Frank Bonetti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-07 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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
+ description: Rails-style method hooks for plain old Ruby objects
42
+ email:
43
+ - frank.r.bonetti@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - .rspec
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - examples/model.rb
55
+ - lib/method_hooks.rb
56
+ - lib/method_hooks/version.rb
57
+ - method_hooks.gemspec
58
+ - spec/lib/method_hooks_spec.rb
59
+ - spec/spec_helper.rb
60
+ homepage: https://github.com/fbonetti/method_hooks
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.0.14
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Rails-style method hooks for plain old Ruby objects
84
+ test_files:
85
+ - spec/lib/method_hooks_spec.rb
86
+ - spec/spec_helper.rb