hollerback 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: 21a8bd56b3df5ebea1b26455c0a5b6c2809fd511
4
+ data.tar.gz: a361d449b9348354535191074b42654278c7ff5c
5
+ SHA512:
6
+ metadata.gz: 820a8794f73386258597bacf2c2652b62307a889ddfed0070038532d8765892250f8a588dbcd85f1ef7f7d718dfd8295257bc74b9a4daa7c3062ff51d97fe9e2
7
+ data.tar.gz: a8e79fa74cf348fb6decd9d92834f6b2b16f7e62aa9c1102b6725bc7a3e23a2fee3dd89825d2496c370fcb624f63084337a3f943a790b6a24990b595a8cb8145
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ 2.3.0
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ matrix:
3
+ allow_failures:
4
+ - rvm: ruby-head
5
+ rvm:
6
+ - ruby-head
7
+ - 2.3.0
8
+ - 2.2.4
9
+ - 2.1.8
10
+ - 2.0.0
11
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Get local or master 'rspec-hollerback-mocks' gem
4
+ library_path = File.expand_path("../../rspec-hollerback-mocks", __FILE__)
5
+ if File.exist?(library_path)
6
+ gem 'rspec-hollerback-mocks', path: library_path
7
+ else
8
+ gem 'rspec-hollerback-mocks', git: "git://github.com/delner/rspec-hollerback-mocks.git",
9
+ branch: ENV.fetch('BRANCH',"master")
10
+ end
11
+
12
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 David Elner
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,257 @@
1
+ Hollerback
2
+ ==========
3
+
4
+ [![Build Status](https://travis-ci.org/delner/hollerback.svg?branch=master)](https://travis-ci.org/delner/hollerback) ![Gem Version](https://badge.fury.io/rb/hollerback.svg)
5
+ ###### *For Ruby 2+*
6
+
7
+ ### Introduction
8
+
9
+ Hollerback adds the callback pattern to your application, allowing you to easily implement DSL-like event handling to your application.
10
+
11
+ ```ruby
12
+ note = NoteApi.get_note("Grocery List") do |on|
13
+ on.found do |note|
14
+ puts "Found an existing note!"
15
+ end
16
+ on.not_found do |name|
17
+ puts "Note is missing! Creating a new one."
18
+ Note.create!(name: name)
19
+ end
20
+ on.error do |error|
21
+ puts "Failed to retrieve note!"
22
+ end
23
+ end
24
+ ```
25
+
26
+ ### Installation
27
+
28
+ ##### If you're not using Bundler...
29
+
30
+ Install the gem via:
31
+
32
+ ```
33
+ gem install hollerback
34
+ ```
35
+
36
+ Then require it into your application with:
37
+
38
+ ```
39
+ require 'hollerback'
40
+ ```
41
+
42
+ ##### If you're using Bundler...
43
+
44
+ Add the gem to your Gemfile:
45
+
46
+ ```
47
+ gem 'hollerback'
48
+ ```
49
+
50
+ And then `bundle install` to install the gem and its dependencies.
51
+
52
+ ### Usage
53
+
54
+ Enable the callback pattern by including the `Hollerback` module into the class/module you want to trigger callbacks.
55
+
56
+ ```ruby
57
+ class NoteApi
58
+ include Hollerback
59
+
60
+ def get_note(name)
61
+ # ...
62
+ end
63
+ end
64
+ ```
65
+
66
+ Then add a `&block` argument to the function you want to be callback enabled.
67
+
68
+ ```ruby
69
+ class NoteApi
70
+ include Hollerback
71
+
72
+ def get_note(name, &block)
73
+ # ...
74
+ end
75
+ end
76
+ ```
77
+
78
+ And use `hollerback_for` to get a set of callbacks you can invoke:
79
+
80
+ ```ruby
81
+ class NoteApi
82
+ include Hollerback
83
+
84
+ def get_note(name, &block)
85
+ hollerback_for(block) do |callbacks|
86
+ # ...
87
+ end
88
+ end
89
+ end
90
+ ```
91
+
92
+ Then trigger callbacks as you like, passing any arguments you need.
93
+
94
+ ```ruby
95
+ class NoteApi
96
+ include Hollerback
97
+
98
+ def get_note(name, &block)
99
+ # Creates Callbacks object from the block
100
+ hollerback_for(block) do |callbacks|
101
+ begin
102
+ # Retrieves a HTTP response
103
+ response = make_note_request(name: name)
104
+
105
+ # Invoke Callbacks
106
+ when response.status
107
+ case 200
108
+ callbacks.respond_with(:found, response.body)
109
+ case 404
110
+ callbacks.respond_with(:not_found, name)
111
+ end
112
+ rescue => e
113
+ callbacks.respond_with(:error, e)
114
+ end
115
+ end
116
+ end
117
+ end
118
+ ```
119
+
120
+ And finally use your newly callback-enabled function with your callback DSL:
121
+
122
+ ```ruby
123
+ def write_note(name, content)
124
+ note = NoteApi.new.get_note("Grocery List") do |on|
125
+ on.found do |note_json|
126
+ Note.from_json(note_json)
127
+ end
128
+ on.not_found do |name|
129
+ Note.create!(name: name)
130
+ end
131
+ on.error do |error|
132
+ raise "Failed to retrieve note! Reason: #{error.message}"
133
+ end
134
+ end
135
+
136
+ note.append(content)
137
+ end
138
+ ```
139
+
140
+ ### Features
141
+
142
+ ##### #hollerback_for
143
+
144
+ Converts an anonymous callback block into a `Hollerback::Callbacks` object that you can invoke callbacks from. Can be called as an instance or class method from any class that includes `Hollerback`.
145
+
146
+ ```ruby
147
+ class NoteApi
148
+ include Hollerback
149
+
150
+ def self.get_note(name, &block)
151
+ hollerback_for(block) do |callbacks|
152
+ # ...
153
+ end
154
+ end
155
+
156
+ def get_note(name, &block)
157
+ hollerback_for(block) do |callbacks|
158
+ # ...
159
+ end
160
+ end
161
+ end
162
+ ```
163
+
164
+ This is the equivalent of:
165
+
166
+ ```ruby
167
+ def get_note(name, &block)
168
+ callbacks = Hollerback::Callbacks.new(block)
169
+ end
170
+ ```
171
+
172
+ If you override the behavior of `Hollerback::Callbacks` in a subclass, you can use it as your callbacks object instead:
173
+
174
+ ```ruby
175
+ class NoteCallbacks < Hollerback::Callbacks
176
+ # ...
177
+ end
178
+
179
+ class NoteApi
180
+ include Hollerback
181
+
182
+ def get_note(name, &block)
183
+ hollerback_for(block, callback_class: NoteCallbacks) do |callbacks|
184
+ # ...
185
+ end
186
+ end
187
+ end
188
+ ```
189
+
190
+ ##### #respond_with
191
+
192
+ Triggers a callback, passing any arguments along. If the callback isn't defined, it raises a `NoMethodError`.
193
+
194
+ ```ruby
195
+ callbacks_block = Proc.new do |on|
196
+ on.no_args { "No args." }
197
+ on.with_args { |a, b| "#{a}, #{b}" }
198
+ on.with_arg_list { |*args| args }
199
+ on.with_arg_block { |&block| block.call }
200
+ end
201
+
202
+ callbacks = Hollerback::Callbacks.new(callbacks_block)
203
+
204
+ callbacks.respond_with(:no_args)
205
+ # => "No args."
206
+ callbacks.respond_with(:with_args, 1, 2)
207
+ # => "1, 2"
208
+ callbacks.respond_with(:with_arg_list, *[1,2,3])
209
+ # => [1,2,3]
210
+ callbacks.respond_with(:with_arg_block, &(Proc.new { "Block called." }))
211
+ # => "Block called."
212
+ callbacks.respond_with(:some_nonexisting_callback)
213
+ # => NoMethodError: No callback 'some_nonexisting_callback' is defined.
214
+ ```
215
+
216
+ ##### #try_respond_with
217
+
218
+ Triggers a callback like `respond_with`, passing any arguments along. If the callback isn't defined, it returns `nil`.
219
+
220
+ ```ruby
221
+ callbacks_block = Proc.new do |on|
222
+ on.no_args { "No args." }
223
+ end
224
+
225
+ callbacks = Hollerback::Callbacks.new(callbacks_block)
226
+
227
+ callbacks.try_respond_with(:no_args)
228
+ # => "No args."
229
+ callbacks.try_respond_with(:some_nonexisting_callback)
230
+ # => nil
231
+ ```
232
+
233
+ ### Testing
234
+
235
+ If you're writing RSpec tests around code that uses callbacks, you can mock callbacks using the `rspec-hollerback-mocks` gem.
236
+
237
+ ```ruby
238
+ it { expect(NoteApi).to receive(:get_note).with(name).and_callback(:found, note) }
239
+ ```
240
+
241
+ Check out the [`rspec-hollerback-mocks`](https://github.com/delner/rspec-hollerback-mocks) gem to learn more.
242
+
243
+ ## Development
244
+
245
+ Install dependencies using `bundle install`. Run tests using `bundle exec rspec`
246
+
247
+ 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).
248
+
249
+ ## Contributing
250
+
251
+ Bug reports and pull requests are welcome on GitHub at https://github.com/delner/rspec-hollerback-mocks.
252
+
253
+
254
+ ## License
255
+
256
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
257
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hollerback"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hollerback/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hollerback"
8
+ spec.version = Hollerback::VERSION
9
+ spec.authors = ["David Elner"]
10
+ spec.email = ["david@davidelner.com"]
11
+
12
+ spec.summary = %q{Simple DSL-like callback pattern for Ruby classes.}
13
+ spec.homepage = "https://github.com/delner/hollerback"
14
+ spec.license = "MIT"
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.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "rspec-hollerback-mocks", "~> 0.1"
25
+ spec.add_development_dependency "pry"
26
+ end
@@ -0,0 +1,3 @@
1
+ require "hollerback/version"
2
+
3
+ require 'hollerback/callbacks'
@@ -0,0 +1,46 @@
1
+ module Hollerback
2
+ def self.included(base)
3
+ base.send(:include, InstanceMethods)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module InstanceMethods
8
+ def hollerback_for(callback_block, callback_class: Callbacks, &block)
9
+ self.class.hollerback_for(callback_block, &block)
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ def hollerback_for(callback_block, callback_class: Callbacks, &block)
15
+ callbacks = callback_class.new(callback_block)
16
+ block.call(callbacks)
17
+ end
18
+ end
19
+
20
+ class Callbacks
21
+ def initialize(block)
22
+ self.tap { |proxy| block.call(proxy) if block }
23
+ end
24
+
25
+ def respond_with(callback, *args)
26
+ if callbacks.has_key?(callback)
27
+ callbacks[callback].call(*args)
28
+ else
29
+ raise NoMethodError.new("No callback '#{callback.to_s}'' is defined.")
30
+ end
31
+ end
32
+ def try_respond_with(callback, *args)
33
+ callbacks[callback].call(*args) if callbacks.has_key?(callback)
34
+ end
35
+
36
+ def method_missing(m, *args, &block)
37
+ block ? callbacks[m] = block : super
38
+ self
39
+ end
40
+
41
+ protected
42
+ def callbacks
43
+ @callbacks ||= {}
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Hollerback
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hollerback
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Elner
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-21 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-hollerback-mocks
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
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:
84
+ email:
85
+ - david@davidelner.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".ruby-version"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - hollerback.gemspec
101
+ - lib/hollerback.rb
102
+ - lib/hollerback/callbacks.rb
103
+ - lib/hollerback/version.rb
104
+ homepage: https://github.com/delner/hollerback
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.5.1
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Simple DSL-like callback pattern for Ruby classes.
128
+ test_files: []