kitty_events 0.1.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: 3d19addf46fc6a9e07c54d606f8d1b9b74c61cd7
4
+ data.tar.gz: 619aa7703318720ce799cf6da904ef24572a6936
5
+ SHA512:
6
+ metadata.gz: 8e941134a1b2733f7d9182b72e22a236466ea3a4d85ba18df30ffca0beeff91ebb470c75296e664a76d2413646a162af12669be062401153501ddf389c122aa1
7
+ data.tar.gz: 1fddb6183fa7f8d783ba8fae13694dc4f9a59fea1a38cde9902139b91698891515d6c15d2f60e39056a41759f50b0dc9d8150d0d758bcfed5063b5b181d88598
data/.gitignore ADDED
@@ -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
data/.rubocop.yml ADDED
@@ -0,0 +1,12 @@
1
+ Documentation:
2
+ Enabled: false
3
+ FrozenStringLiteralComment:
4
+ Enabled: false
5
+ ClassVars:
6
+ Enabled: false
7
+ Metrics/LineLength:
8
+ Max: 140
9
+ AllCops:
10
+ TargetRubyVersion: 2.3
11
+ Exclude:
12
+ - 'bin/*'
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.13.6
6
+ script:
7
+ - bundle exec rspec
8
+ - bundle exec rubocop --config .rubocop.yml
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at coutermarsh.mike@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kitty_events.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Mike Coutermarsh
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,95 @@
1
+ # :heart_eyes_cat: KittyEvents
2
+
3
+ Super simple event system built on top of ActiveJob.
4
+
5
+ KittyEvents implements the [publish/subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) pattern using ActiveJob. You setup your events and list the subscribers for them. When an event is triggered, KittyEvents will fanout the event to each of your subscribers.
6
+
7
+ ### Why use this
8
+ - Uses ActiveJob. No need to add a new dependency for pub/sub.
9
+ - Reduce complexity/establish patterns. Can be used to replace `after_commit`'s. This creates easier to follow/read code. Less surprises = good!
10
+ - Replace several `perform_later`'s with a single event `trigger`. Reducing the amount of I/O happening in request (less I/O = faster response times)
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'kitty_events'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle install
23
+
24
+ ## Usage
25
+
26
+ In Rails, setup your events and subscribers in an initializer.
27
+
28
+ ```Ruby
29
+ # config/initializers/application_events.rb
30
+ module ApplicationEvents
31
+ extend KittyEvents
32
+
33
+ event :user_signup, [
34
+ WelcomeEmailWorker,
35
+ WelcomeTweetWorker,
36
+ SyncProfileImageWorker,
37
+ ExampleWorker.set(wait: 5.minutes), # standard ActiveJob settings work as well!
38
+ ]
39
+
40
+ event :user_upvote, [
41
+ SomeWorker,
42
+ AnotherWorker,
43
+ ]
44
+ end
45
+ ```
46
+
47
+ Each subscriber must be an ActiveJob and respond to `perform_later(object)`.
48
+
49
+ ```Ruby
50
+ class ExampleWorker < ActiveJob::Base
51
+ def perform(user)
52
+ # do work
53
+ end
54
+ end
55
+ ```
56
+
57
+ Then in your application, to trigger an event. Do the following.
58
+
59
+ ```Ruby
60
+ ApplicationEvents.trigger(:user_signup, user)
61
+ ```
62
+
63
+ Using the above example, triggering this event would pass `user` to each of the subscribers defined in our initializer:
64
+ ```
65
+ WelcomeEmailWorker,
66
+ WelcomeTweetWorker,
67
+ SyncProfileImageWorker,
68
+ ExampleWorker.set(wait: 5.minutes)
69
+ ```
70
+
71
+ ## Development
72
+
73
+ 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.
74
+
75
+ 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).
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/producthunt/kittyevents. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
80
+
81
+
82
+ ## License
83
+
84
+ [![Product Hunt](http://i.imgur.com/dtAr7wC.png)](https://www.producthunt.com)
85
+
86
+ ```
87
+ _________________
88
+ < The MIT License >
89
+ -----------------
90
+ \ ^__^
91
+ \ (oo)\_______
92
+ (__)\ )\/\
93
+ ||----w |
94
+ || ||
95
+ ```
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'kitty_events'
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
@@ -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,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kitty_events/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'kitty_events'
8
+ spec.version = KittyEvents::VERSION
9
+ spec.authors = ['Mike Coutermarsh', 'Radoslav Stankov']
10
+ spec.email = ['coutermarsh.mike@gmail.com', 'rstankov@gmail.com']
11
+
12
+ spec.summary = 'Super simple event system on top of ActiveJob'
13
+ spec.description = 'Super simple event system on top of ActiveJob'
14
+ spec.homepage = 'https://github.com/producthunt/kittyevents'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'activejob', '>= 4.2'
25
+ spec.add_development_dependency 'bundler', '~> 1.13'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'rubocop', '~> 0.35'
29
+ end
@@ -0,0 +1,60 @@
1
+ require 'kitty_events/version'
2
+ require 'kitty_events/handle_worker'
3
+
4
+ # Super simple event system on top of ActiveJob
5
+ #
6
+ # # Create event emitter
7
+ # module ApplicationEvents
8
+ # extends KittyEvents
9
+ #
10
+ # # Subscribe to event
11
+ # # An event handler is ActiveJob worker that implements `.perform(object)`
12
+ # event :upvote, [
13
+ # SpamDetector::UpvoteEventHandler,
14
+ # Achievements::UpvoteEventHandler,
15
+ # CacheCleaner::UpvoteEventHandler
16
+ # ]
17
+ # end
18
+ #
19
+ # # Trigger event
20
+ # # When an event is triggered, It will fan out to all handlers via ActiveJob
21
+ # ApplicationEvents.trigger :upvote, vote
22
+ module KittyEvents
23
+ def self.extended(mod)
24
+ mod.class_variable_set :@@handlers, {}
25
+ mod.mattr_reader :handlers
26
+ mod.const_set 'HandleWorker', Class.new(::KittyEvents::HandleWorker)
27
+ end
28
+
29
+ def handle_worker
30
+ self::HandleWorker
31
+ end
32
+
33
+ def event(event, event_handlers)
34
+ raise ArgumentError, "#{event} already registered" if handlers[event.to_sym]
35
+
36
+ handlers[event.to_sym] = validate_handlers(event_handlers)
37
+ end
38
+
39
+ def trigger(event, object)
40
+ raise ArgumentError, "#{event} is not registered" unless handlers[event.to_sym]
41
+
42
+ handle_worker.perform_later(event.to_s, object)
43
+ end
44
+
45
+ def handle(event, object)
46
+ (handlers[event.to_sym] || []).each do |handler|
47
+ handler.perform_later(object)
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def validate_handlers(handlers)
54
+ Array(handlers).each do |handler|
55
+ unless handler.respond_to? :perform_later
56
+ raise ArgumentError, "#{handler} has to respond to `perform_later`"
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,10 @@
1
+ require 'active_job'
2
+ require 'active_support/core_ext/module/introspection'
3
+
4
+ module KittyEvents
5
+ class HandleWorker < ActiveJob::Base
6
+ def perform(event, object)
7
+ self.class.parent.handle(event, object)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module KittyEvents
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kitty_events
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Coutermarsh
8
+ - Radoslav Stankov
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2016-12-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activejob
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '4.2'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '4.2'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.13'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.13'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '10.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rubocop
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0.35'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '0.35'
84
+ description: Super simple event system on top of ActiveJob
85
+ email:
86
+ - coutermarsh.mike@gmail.com
87
+ - rstankov@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".rubocop.yml"
95
+ - ".travis.yml"
96
+ - CODE_OF_CONDUCT.md
97
+ - Gemfile
98
+ - LICENSE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - bin/console
102
+ - bin/setup
103
+ - kitty_events.gemspec
104
+ - lib/kitty_events.rb
105
+ - lib/kitty_events/handle_worker.rb
106
+ - lib/kitty_events/version.rb
107
+ homepage: https://github.com/producthunt/kittyevents
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.8
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Super simple event system on top of ActiveJob
131
+ test_files: []