YouAreDaChef 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.ruby-version +1 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/Makefile +20 -0
- data/README.md +63 -0
- data/Rakefile +2 -0
- data/YouAreDaChef.gemspec +25 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/YouAreDaChef/version.rb +3 -0
- data/lib/YouAreDaChef.rb +92 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 52790511fc47a2da48427782f385dfdb74c70dc7
|
4
|
+
data.tar.gz: 4c59807c3b94d71bd458d3ebdd9a833530214ed0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0e7db716e828d9c154d81aa3577c99394d8b962a94d45f841a6bf9fe55e9111b7e2894daa7c5d69cfa8d674d6a51e60051781f2e891dfbe5199afc4ae5214e19
|
7
|
+
data.tar.gz: 9539e796437725278d66d77d3f1e76a36292c5eca08493b17daee83f9926d9e8cf3ce356a03004d65a32c03a365486ab703b295fd2f4ecfadf92e077df1732fd
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Robert Krzysztoforski
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/Makefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
install-bundler: ## Install gem dependencies
|
2
|
+
@echo "Installing gem dependencies"
|
3
|
+
@bundle install
|
4
|
+
|
5
|
+
install: install-bundler ## Prepare current development environment
|
6
|
+
|
7
|
+
test: ## Run tests
|
8
|
+
@echo "Running basic tests - beware: this won't guarantee build pass"
|
9
|
+
@bundle exec rspec
|
10
|
+
|
11
|
+
mutate: test ## Run mutation tests
|
12
|
+
@echo "Running mutation tests - only 100% free mutation will be accepted"
|
13
|
+
@bundle exec mutant --include lib --require YouAreDaChef --use rspec "YouAreDaChef*"
|
14
|
+
|
15
|
+
.PHONY: help
|
16
|
+
|
17
|
+
help:
|
18
|
+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
19
|
+
|
20
|
+
.DEFAULT_GOAL := help
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/brissenden/YouAreDaChef.svg?branch=master)](https://travis-ci.org/brissenden/YouAreDaChef)
|
2
|
+
[![Code Climate](https://codeclimate.com/github/brissenden/YouAreDaChef/badges/gpa.svg)](https://codeclimate.com/github/brissenden/YouAreDaChef)
|
3
|
+
[![Test Coverage](https://codeclimate.com/github/brissenden/YouAreDaChef/badges/coverage.svg)](https://codeclimate.com/github/brissenden/YouAreDaChef/coverage)
|
4
|
+
|
5
|
+
# YouAreDaChef
|
6
|
+
|
7
|
+
A Ruby implementation of an YouAreDaChef library inspired by https://github.com/raganwald/YouAreDaChef.
|
8
|
+
This library supports `before`, `after` and `around` methods.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'YouAreDaChef'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install YouAreDaChef
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
```
|
29
|
+
class Glue
|
30
|
+
extend YouAreDaChef
|
31
|
+
|
32
|
+
attr_reader :use_case
|
33
|
+
|
34
|
+
def initialize
|
35
|
+
@use_case = UseCase.new
|
36
|
+
end
|
37
|
+
|
38
|
+
def assigned
|
39
|
+
end
|
40
|
+
|
41
|
+
def assessed
|
42
|
+
end
|
43
|
+
|
44
|
+
around :assigned, proc { |method, args|
|
45
|
+
method.call(*args)
|
46
|
+
# some code
|
47
|
+
}
|
48
|
+
|
49
|
+
before :assessed, proc { |args, object|
|
50
|
+
object.use_case.clean_up_old_assessments
|
51
|
+
# some code
|
52
|
+
}
|
53
|
+
|
54
|
+
after :assessed, proc { |args, object, result|
|
55
|
+
object.use_case.send_email_notification
|
56
|
+
# some code
|
57
|
+
}
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/brissenden/YouAreDaChef.
|
data/Rakefile
ADDED
@@ -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 'YouAreDaChef/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "YouAreDaChef"
|
8
|
+
spec.version = YouAreDaChef::VERSION
|
9
|
+
spec.authors = ["Robert Krzysztoforski"]
|
10
|
+
spec.email = ["robert.krzysztoforski@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Simple AOP library for Ruby.}
|
13
|
+
spec.description = %q{Simple AOP library for Ruby.}
|
14
|
+
spec.homepage = 'https://github.com/brissenden/YouAreDaChef'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 3.2'
|
24
|
+
spec.add_development_dependency 'mutant-rspec', '~> 0.7.9'
|
25
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "YouAreDaChef"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/YouAreDaChef.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'YouAreDaChef/version'
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
module YouAreDaChef
|
5
|
+
CallbackNotDefined = Class.new(StandardError)
|
6
|
+
MultipleAroundCallbacks = Class.new(StandardError)
|
7
|
+
|
8
|
+
def callbacks
|
9
|
+
@callbacks ||= default_callbacks
|
10
|
+
end
|
11
|
+
|
12
|
+
def before(method, callback)
|
13
|
+
define_callback(:before, method, callback)
|
14
|
+
end
|
15
|
+
|
16
|
+
def after(method, callback)
|
17
|
+
define_callback(:after, method, callback)
|
18
|
+
end
|
19
|
+
|
20
|
+
def around(method, callback)
|
21
|
+
define_callback(:around, method, callback)
|
22
|
+
end
|
23
|
+
|
24
|
+
def remove_all_callbacks
|
25
|
+
@callbacks = default_callbacks
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def default_callbacks
|
30
|
+
{ before: {}, after: {}, around: {} }
|
31
|
+
end
|
32
|
+
|
33
|
+
def filter_callbacks(type, method)
|
34
|
+
callbacks[type][method] || []
|
35
|
+
end
|
36
|
+
|
37
|
+
def define_callback(type, method, block)
|
38
|
+
callbacks[type][method] ||= []
|
39
|
+
if callbacks[type][method].empty?
|
40
|
+
alias_name = fake_alias_name
|
41
|
+
alias_method alias_name, method
|
42
|
+
private alias_name
|
43
|
+
|
44
|
+
callback_klazz = self
|
45
|
+
define_method method do |*args|
|
46
|
+
callback_klazz.__send__(:execute_before_callbacks, method, self, *args)
|
47
|
+
result = callback_klazz.__send__(:execute_around_callbacks, method, alias_name, self, *args)
|
48
|
+
callback_klazz.__send__(:execute_after_callbacks, method, self, *args, result)
|
49
|
+
result
|
50
|
+
end
|
51
|
+
|
52
|
+
callbacks[type][method] << block
|
53
|
+
else
|
54
|
+
callbacks[type][method] << block
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def fake_alias_name
|
59
|
+
['__callbacks__', SecureRandom.hex].join
|
60
|
+
end
|
61
|
+
|
62
|
+
def execute_before_callbacks(method, object, *args)
|
63
|
+
filter_callbacks(:before, method).each do |callback|
|
64
|
+
verify_callback(callback)
|
65
|
+
callback.call(*args, object)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def execute_around_callbacks(method, alias_name, object, *args)
|
70
|
+
around_callbacks = filter_callbacks(:around, method)
|
71
|
+
if around_callbacks.empty?
|
72
|
+
object.__send__(alias_name, *args)
|
73
|
+
elsif around_callbacks.size === 1
|
74
|
+
callback = around_callbacks.first
|
75
|
+
verify_callback(callback)
|
76
|
+
callback.call(object.method(alias_name), args, object)
|
77
|
+
else
|
78
|
+
raise MultipleAroundCallbacks.new 'Only one around callback is allowed per method'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def execute_after_callbacks(method, object, *args, result)
|
83
|
+
filter_callbacks(:after, method).each do |callback|
|
84
|
+
verify_callback(callback)
|
85
|
+
callback.call(*args, object, result)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def verify_callback(callback)
|
90
|
+
raise CallbackNotDefined.new 'Callback should be a Proc function' unless callback
|
91
|
+
end
|
92
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: YouAreDaChef
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Krzysztoforski
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-26 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.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mutant-rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.7.9
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.7.9
|
69
|
+
description: Simple AOP library for Ruby.
|
70
|
+
email:
|
71
|
+
- robert.krzysztoforski@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".ruby-version"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE
|
81
|
+
- Makefile
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- YouAreDaChef.gemspec
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/YouAreDaChef.rb
|
88
|
+
- lib/YouAreDaChef/version.rb
|
89
|
+
homepage: https://github.com/brissenden/YouAreDaChef
|
90
|
+
licenses: []
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.4.5
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Simple AOP library for Ruby.
|
112
|
+
test_files: []
|