block_registry 1.0.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: 0368cee336a9e621f5ca76133d3fbac94b78ce79
4
+ data.tar.gz: 14be337ed79624ec9c4bd267ca44eacf82a1b7ef
5
+ SHA512:
6
+ metadata.gz: fcfa395825ef64785556969a7854cba56111a21af831ef36cd7e04443b1b58d622fc13b01623d14061c5b026f7e6b5a5dedfa93e8461ee97eb1f8c52d3be0b52
7
+ data.tar.gz: 0bebe41bfb48455d7893359a26bd6556862ddb44c5de0e3c74489f37b4dad4ddfd75d490d0282316d1133cf7e5729ae44b3cf9fb9807e674a4a6b8597f9bd5d5
@@ -0,0 +1,9 @@
1
+ ---
2
+ engines:
3
+ rubocop:
4
+ enabled: true
5
+ ratings:
6
+ paths:
7
+ - "**.rb"
8
+ exclude_paths:
9
+ - spec/**/*
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.idea/
3
+ /.ruby-version
4
+ /.yardoc
5
+ /Gemfile.lock
6
+ /_yardoc/
7
+ /coverage/
8
+ /doc/
9
+ /pkg/
10
+ /spec/reports/
11
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ block_registry
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in block_registry.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 mhuggins
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,126 @@
1
+ # BlockRegistry
2
+
3
+ Register event handlers to key lookups.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'block_registry'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install block_registry
20
+
21
+ ## Usage
22
+
23
+ Create a `BlockRegistry` instance:
24
+
25
+ ```ruby
26
+ registry = BlockRegistry.new
27
+ ```
28
+
29
+ Register keys with a processing block:
30
+
31
+ ```ruby
32
+ registry.register('user-login') do |email|
33
+ user = User.find_by(email: email)
34
+ puts "User #{user.email} logged in" if user
35
+ end
36
+
37
+ registry.register('user-register') do |email, password|
38
+ user = User.create(email: email, password: password)
39
+ puts "User #{user.email} registered" if user.persisted?
40
+ end
41
+ ```
42
+
43
+ Call the handler with appropriate arguments:
44
+
45
+ ```ruby
46
+ registry.handle('user-register', 'joe@example.com', 'MyPassword123')
47
+ registry.handle('user-login', 'joe@example.com')
48
+ ```
49
+
50
+ ### `BlockRegistry#register`
51
+
52
+ Registers a handler to a key. Expects a key name and a block whose arity matches that used by
53
+ `BlockRegistry#handle`. Multiple handlers can be defined for a single key.
54
+
55
+ ```ruby
56
+ registry.register('private-message') do |sender_id, recipient_id, message|
57
+ Message.create!(sender_id: sender_id, recipient_id: recipient_id, message: message)
58
+ end
59
+
60
+ registry.register('private-message') do |sender_id, recipient_id, message|
61
+ NewRelic::Agent.notify('private-message', sender_id: sender_id, recipient_id: recipient_id, message: message)
62
+ end
63
+ ```
64
+
65
+ ### `BlockRegistry#registered?`
66
+
67
+ Returns true or false to signify whether any handlers exist for a given key.
68
+
69
+ ```ruby
70
+ registry.register('user-register', &block)
71
+ registry.registered?('user-register') # => true
72
+ registry.registered?('user-login') # => false
73
+ ```
74
+
75
+ ### `BlockRegistry#unregister`
76
+
77
+ Unregisters one or all handlers for a key. If a block is given, the matching handler is
78
+ unregistered. If no block is given, then all handlers for the key are unregistered.
79
+
80
+ ```ruby
81
+ block = ->(email) do
82
+ puts "#{email} signed up"
83
+ end
84
+
85
+ registry.register('user-register', &block)
86
+ registry.register('user-register') do |email|
87
+ puts 'another handler'
88
+ end
89
+
90
+ registry.unregister('user-register', &block)
91
+ registry.registered?('user-register') # => true
92
+
93
+ registry.unregister('user-register')
94
+ registry.registered?('user-register') # => false
95
+ ```
96
+
97
+ ### `BlockRegistry#handles?`
98
+
99
+ Alias for [`BlockRegistry#registered?`](#BlockRegistry_registered)
100
+
101
+ ### `BlockRegistry#handle`
102
+
103
+ Processes a message across all handlers for the given key.
104
+
105
+ ```ruby
106
+ registry.register('user-register') { |email| puts "#{email} signed up" }
107
+ registry.register('user-register') { |email| puts "hi, #{email}" }
108
+
109
+ registry.handle('user-register', 'joe@example.com')
110
+ # => joe@example.com signed up
111
+ # => hi, joe@example.com
112
+ ```
113
+
114
+ ## Development
115
+
116
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
117
+
118
+ 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).
119
+
120
+ ## Contributing
121
+
122
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kapost/block_registry.
123
+
124
+ ## License
125
+
126
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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 'block_registry'
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -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 'block_registry/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'block_registry'
8
+ spec.version = BlockRegistry::VERSION
9
+ spec.authors = ['Matt Huggins']
10
+ spec.email = ['matt.huggins@kapost.com']
11
+
12
+ spec.summary = 'Simple event handler registry'
13
+ spec.description = 'Register event handlers to key lookups.'
14
+ spec.homepage = 'https://github.com/kapost/block_registry'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.10'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec'
25
+ end
@@ -0,0 +1,7 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.2.3
4
+
5
+ dependencies:
6
+ pre:
7
+ - gem install bundler -v 1.10.6
@@ -0,0 +1,42 @@
1
+ require 'block_registry/errors'
2
+ require 'block_registry/version'
3
+ require 'set'
4
+
5
+ class BlockRegistry
6
+ def initialize
7
+ self.mappings = {}
8
+ end
9
+
10
+ def register(key, &block)
11
+ mappings[key] ||= Set.new
12
+ mappings[key] << block
13
+ end
14
+
15
+ def unregister(key, &block)
16
+ raise UnregisteredError, "\"#{key}\" is not registered" unless registered?(key)
17
+
18
+ if block_given?
19
+ mappings[key].delete?(block)
20
+ mappings.delete(key) if mappings[key].empty?
21
+ else
22
+ mappings.delete(key)
23
+ end
24
+ end
25
+
26
+ def registered?(key)
27
+ mappings.has_key?(key)
28
+ end
29
+ alias_method :handles?, :registered?
30
+
31
+ def handle(key, *args)
32
+ raise UnregisteredError, "\"#{key}\" is not registered" unless registered?(key)
33
+
34
+ mappings[key].each do |mapping|
35
+ mapping.call(*args)
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ attr_accessor :mappings
42
+ end
@@ -0,0 +1,4 @@
1
+ class BlockRegistry
2
+ class Error < StandardError; end
3
+ class UnregisteredError < Error; end
4
+ end
@@ -0,0 +1,3 @@
1
+ class BlockRegistry
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: block_registry
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Huggins
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-02 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '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
+ description: Register event handlers to key lookups.
56
+ email:
57
+ - matt.huggins@kapost.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".codeclimate.yml"
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".ruby-gemset"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - block_registry.gemspec
74
+ - circle.yml
75
+ - lib/block_registry.rb
76
+ - lib/block_registry/errors.rb
77
+ - lib/block_registry/version.rb
78
+ homepage: https://github.com/kapost/block_registry
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.5.1
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Simple event handler registry
102
+ test_files: []