hollaback 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/.rubocop.yml +14 -0
- data/.travis.yml +6 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +68 -0
- data/Rakefile +10 -0
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/hollaback.gemspec +29 -0
- data/lib/hollaback.rb +4 -0
- data/lib/hollaback/callback.rb +19 -0
- data/lib/hollaback/chain.rb +46 -0
- data/lib/hollaback/sequence.rb +35 -0
- data/lib/hollaback/version.rb +3 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f738463a8b1b0abeb5b51456d41365d7315cc59d
|
4
|
+
data.tar.gz: 5ef3503097683cfd440c0646725245e57fc4aa79
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4a574a01e9818b717949b1c3473ff5f2081508e0853f5c797e060922a6cf8b9a98ab2c619c287469e6363f7b94eb97d2bbaf8b4981b5ddb6e0b245b829d000ed
|
7
|
+
data.tar.gz: e050207bbc9b865f01400558ffc88f696d7f0c3f2ff0120e47eaa2ca4acb52e0a2b811c459f7530d24ea262f71bddae18245c330ac08bbcf5296dc0653096f60
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Localytics http://www.localytics.com
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Hollaback
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/localytics/hollaback.svg?branch=master)](https://travis-ci.org/localytics/hollaback)
|
4
|
+
[![Coverage Status](https://coveralls.io/repos/github/localytics/hollaback/badge.svg?branch=master)](https://coveralls.io/github/localytics/hollaback?branch=master)
|
5
|
+
|
6
|
+
Builds method chains such that you can execute functions inside of a predefined set of callbacks.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'hollaback'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install hollaback
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Sometimes you want to execute code in the context of hooks. With this gem you can build `before`, `after`, and `around` callbacks that are executed in the context of the given object. For `before`s and `after`s you can use either symbols are procs. For `around`s you can use symbols.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class Callbacker
|
30
|
+
def say_hello
|
31
|
+
puts 'Hello!'
|
32
|
+
end
|
33
|
+
|
34
|
+
def say_goodbye
|
35
|
+
puts 'Goodbye!'
|
36
|
+
end
|
37
|
+
|
38
|
+
def say(&block)
|
39
|
+
puts 'speaking... '
|
40
|
+
puts yield
|
41
|
+
puts '...done.'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
require 'hollaback'
|
46
|
+
chain = Hollaback::Chain.new
|
47
|
+
|
48
|
+
chain.before :say_hello
|
49
|
+
chain.before { puts 'How are you?' }
|
50
|
+
chain.after :say_goodbye
|
51
|
+
chain.around :say
|
52
|
+
|
53
|
+
chain.compile { '- Hollaback' }.call(Callbacker.new)
|
54
|
+
```
|
55
|
+
|
56
|
+
## Development
|
57
|
+
|
58
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
59
|
+
|
60
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/localytics/hollaback.
|
65
|
+
|
66
|
+
## License
|
67
|
+
|
68
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/hollaback.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hollaback/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'hollaback'
|
8
|
+
spec.version = Hollaback::VERSION
|
9
|
+
spec.authors = ['Localytics']
|
10
|
+
spec.email = ['oss@localytics.com']
|
11
|
+
|
12
|
+
spec.summary = 'Add callbacks to your methods'
|
13
|
+
spec.homepage = 'https://github.com/localytics/hollaback'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.files = files.reject { |f| f.match(%r{^test/}) }
|
18
|
+
|
19
|
+
spec.bindir = 'exe'
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
26
|
+
spec.add_development_dependency 'simplecov', '~> 0.11'
|
27
|
+
spec.add_development_dependency 'coveralls', '~> 0.8'
|
28
|
+
spec.add_development_dependency 'rubocop', '~> 0.39'
|
29
|
+
end
|
data/lib/hollaback.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Hollaback
|
2
|
+
class Callback
|
3
|
+
attr_reader :type, :executable
|
4
|
+
|
5
|
+
def initialize(type, execute = nil, &block)
|
6
|
+
@type = type
|
7
|
+
@executable = (execute ? execute : block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def build
|
11
|
+
case executable
|
12
|
+
when Symbol
|
13
|
+
-> (target, &block) { target.send(executable, &block) }
|
14
|
+
when Proc
|
15
|
+
-> (target) { target.instance_eval(&executable) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Hollaback
|
2
|
+
class Chain
|
3
|
+
attr_reader :callbacks
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@callbacks = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def +(other)
|
10
|
+
@callbacks += other.callbacks
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def before(execute = nil, &block)
|
15
|
+
build(:before, execute, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def after(execute = nil, &block)
|
19
|
+
build(:after, execute, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def around(execute = nil, &block)
|
23
|
+
build(:around, execute, &block)
|
24
|
+
end
|
25
|
+
|
26
|
+
def empty?
|
27
|
+
callbacks.empty?
|
28
|
+
end
|
29
|
+
|
30
|
+
def compile(&block)
|
31
|
+
if empty?
|
32
|
+
block
|
33
|
+
else
|
34
|
+
callbacks.inject(Sequence.new(&block)) do |sequence, callback|
|
35
|
+
sequence.send(callback.type, &callback.build)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def build(type, execute, &block)
|
43
|
+
callbacks << Callback.new(type, execute, &block)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Hollaback
|
2
|
+
class Sequence
|
3
|
+
attr_reader :befores, :afters, :main
|
4
|
+
|
5
|
+
def initialize(args = {}, &main)
|
6
|
+
@main = main
|
7
|
+
@befores = args.fetch(:befores, [])
|
8
|
+
@afters = args.fetch(:afters, [])
|
9
|
+
end
|
10
|
+
|
11
|
+
def before(&before)
|
12
|
+
befores << before
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
def after(&after)
|
17
|
+
afters << after
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
# rubocop:disable Performance/RedundantBlockCall
|
22
|
+
def around(&around)
|
23
|
+
Sequence.new do |target|
|
24
|
+
around.call(target) { call(target) }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def call(target)
|
29
|
+
befores.each { |before| before.call(target) }
|
30
|
+
value = main.call(target)
|
31
|
+
afters.each { |after| after.call(target) }
|
32
|
+
value
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hollaback
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Localytics
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-11 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: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.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.11'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.11'
|
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.8'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.8'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.39'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.39'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- oss@localytics.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rubocop.yml"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- hollaback.gemspec
|
114
|
+
- lib/hollaback.rb
|
115
|
+
- lib/hollaback/callback.rb
|
116
|
+
- lib/hollaback/chain.rb
|
117
|
+
- lib/hollaback/sequence.rb
|
118
|
+
- lib/hollaback/version.rb
|
119
|
+
homepage: https://github.com/localytics/hollaback
|
120
|
+
licenses:
|
121
|
+
- MIT
|
122
|
+
metadata: {}
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
requirements: []
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 2.5.1
|
140
|
+
signing_key:
|
141
|
+
specification_version: 4
|
142
|
+
summary: Add callbacks to your methods
|
143
|
+
test_files: []
|