event_train 0.2.1

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: 8aabf11a90dd8a08cc0f3d71b074610ea7f3ad16
4
+ data.tar.gz: 5e8e65714bb2f169a0fa2c88057faaaa5ca81146
5
+ SHA512:
6
+ metadata.gz: f12d2c19067f7533b1c0f2dbdcaa617522956cdb1241ae5184853ea2e6f86ab4f5ca3bb0a8d22ed3d585510d5bf017fc60ce239a6bcb27832de9547ae150a930
7
+ data.tar.gz: 90dd532f0decc0dfdf5420ff7f9c29142fdbade0fd5818d4f953a4e8ed9615de859558f3d7b7e017793f5bb0f8e018254c8a551736b79ec7863e50aaad2f6fc5
@@ -0,0 +1,10 @@
1
+ engines:
2
+ rubocop:
3
+ enabled: true
4
+ ratings:
5
+ paths:
6
+ - config/**
7
+ - lib/**
8
+ - "**.rb"
9
+ exclude_paths:
10
+ - spec/**/*
@@ -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,12 @@
1
+ Style/LineLength:
2
+ Max: 120
3
+ Style/StringLiterals:
4
+ EnforcedStyle: double_quotes
5
+ Style/EmptyLinesAroundBody:
6
+ Enabled: false
7
+ Style/EmptyLinesAroundModuleBody:
8
+ Enabled: false
9
+ Style/EmptyLinesAroundClass:
10
+ Enabled: false
11
+ Style/AlignParameters:
12
+ EnforcedStyle: with_fixed_indentation
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.1
4
+ before_install: gem install bundler -v 1.11.2
5
+ after_success:
6
+ - bundle exec codeclimate-test-reporter
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at pro@rchampourlier.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+
4
+ # Specify your gem"s dependencies in event_train.gemspec
5
+ gemspec
6
+
7
+ group :development do
8
+ gem "guard"
9
+ gem "guard-rspec"
10
+ end
11
+
12
+ group :test do
13
+ gem "simplecov", require: false
14
+ gem "terminal-notifier-guard"
15
+ gem "codeclimate-test-reporter"
16
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ ignore %r{^coverage/}
3
+ notification :terminal_notifier, activate: "com.googlecode.iterm2"
4
+
5
+ group :test do
6
+ # Note: The cmd option is now required due to the increasing number of ways
7
+ # rspec may be run, below are examples of the most common uses.
8
+ # * bundler: "bundle exec rspec"
9
+ # * bundler binstubs: "bin/rspec"
10
+ # * spring: "bin/rsspec" (This will use spring if running and you have
11
+ # installed the spring binstubs per the docs)
12
+ # * zeus: "zeus rspec" (requires the server to be started separetly)
13
+ # * "just" rspec: "rspec"
14
+ guard :rspec, cmd: "bundle exec rspec" do
15
+ watch(%r{^spec/.+_spec\.rb$})
16
+ watch(%r{^lib/(.+)\.rb$}) { "spec" }
17
+ watch("spec/spec_helper.rb") { "spec" }
18
+ watch(%r{spec/support/(.+)\.rb}) { "spec" }
19
+ end
20
+ end
@@ -0,0 +1,48 @@
1
+ # EventTrain
2
+
3
+ [![Build Status](https://travis-ci.org/rchampourlier/event_train.svg?branch=master)](https://travis-ci.org/rchampourlier/event_train)
4
+ [![Code Climate](https://codeclimate.com/github/rchampourlier/event_train/badges/gpa.svg)](https://codeclimate.com/github/rchampourlier/event_train)
5
+ [![Test Coverage](https://codeclimate.com/github/rchampourlier/event_train/badges/coverage.svg)](https://codeclimate.com/github/rchampourlier/event_train/coverage)
6
+ [![Issue Count](https://codeclimate.com/github/rchampourlier/event_train/badges/issue_count.svg)](https://codeclimate.com/github/rchampourlier/event_train)
7
+
8
+ EventTrain is a really simple implementation of an EventTrain in Ruby. It enables you to start implementing your application using an event bus pattern without relying on some specific pub/sub service or component (like Redis, RabbitMQ). This will enable to move to such future-proof implementation just by using the same interface, thus reducing the need for refactoring or rewrites!
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem "event_train"
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install event_train
25
+
26
+ ## Usage
27
+
28
+ ```ruby
29
+ # Subscribe and publish to the :global channel
30
+ EventTrain.subscribe do |event_name:, event_data:|
31
+ puts "Received event named #{event_name} with data #{event_data}"
32
+ end
33
+ EventTrain.publish(event_name: :my_first_event, event_data: true)
34
+ # => "Received event named my_first_event with data true"
35
+
36
+ # To subscribe / publish to a specific channel, just add the :channel_name parameter:
37
+ EventTrain.publish(channel_name: :my_first_channel, ...)
38
+ ```
39
+
40
+ ## Development
41
+
42
+ 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.
43
+
44
+ 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).
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rchampourlier/event_train. 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.
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
8
+ task test: :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.1
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ #!/usr/bin/env ruby
3
+
4
+ require "bundler/setup"
5
+ require "event_train"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ 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,34 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "event_train/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "event_train"
9
+ spec.version = EventTrain::VERSION
10
+ spec.authors = ["Romain Champourlier"]
11
+ spec.email = ["public@rchampourlier.com"]
12
+
13
+ spec.summary = "A really simple implementation of an event bus in Ruby, mostly for my own projects."
14
+ spec.description = "This provides a simple implementation of an event bus. It makes easy to use the abstraction" \
15
+ "without needing dedicated components, like RabbitMQ. Using this will however make it easy" \
16
+ "to rewrite your code to use more solid foundations for the future."
17
+ spec.homepage = "https://github.com/rchampourlier/event_train"
18
+
19
+ # Prevent pushing this gem to RubyGems.org by setting "allowed_push_host", or
20
+ # delete this section to allow pushing this gem to any host.
21
+ unless spec.respond_to?(:metadata)
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+ #spec.metadata["allowed_push_host"] = 'TODO: Set to "http://mygemserver.com"'
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.11"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+ require "event_train/version"
3
+ require "event_train/channel"
4
+
5
+ # A basic pub/sub event bus.
6
+ #
7
+ # How to use
8
+ # ==========
9
+ #
10
+ # EventTrain.subscribe { |event_name:, event_data:| puts "received #{event_name}"}
11
+ # EventTrain.publish(event_name: :some_event, event_data: true)
12
+ module EventTrain
13
+ @channels = {}
14
+
15
+ # Publishes a new event on the specified channel.
16
+ def publish(event_name:, event_data:, channel_name: :global)
17
+ channel = get_channel(name: channel_name)
18
+ channel.publish(event_name: event_name, event_data: event_data)
19
+ end
20
+ module_function :publish
21
+
22
+ def subscribe(channel_name: :global, &block)
23
+ channel = get_channel(name: channel_name)
24
+ channel.subscribe(&block)
25
+ end
26
+ module_function :subscribe
27
+
28
+ def get_channel(name:)
29
+ @channels[name] ||= build_channel(name: name)
30
+ end
31
+ module_function :get_channel
32
+
33
+ def build_channel(name:)
34
+ Channel.new(name: name)
35
+ end
36
+ module_function :build_channel
37
+
38
+ # Reset all channels, removing all subscribed blocks.
39
+ def reset_all_channels
40
+ @channels = {}
41
+ end
42
+ module_function :reset_all_channels
43
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+ require "logger"
3
+
4
+ module EventTrain
5
+
6
+ # A channel where events may be published and to which consumers
7
+ # may subscribe.
8
+ #
9
+ # Interacting with a channel directly is not mandatory, you may
10
+ # use the global EventTrain module to access it without the channel
11
+ # instance. The top-level module acts as a singleton.
12
+ class Channel
13
+ attr_reader :name, :logger
14
+
15
+ def initialize(name:, logger: default_logger)
16
+ @name = name
17
+ @logger = logger
18
+ @subscribed_blocks = []
19
+ end
20
+
21
+ def publish(event_name:, event_data:)
22
+ raise ArgumentError, "event_name must be a Symbol" unless event_name.is_a?(Symbol)
23
+ subscribed_blocks.map do |block|
24
+ block.call(event_name: event_name, event_data: event_data)
25
+ end
26
+ end
27
+
28
+ def subscribe(&block)
29
+ unless block_given?
30
+ raise ArgumentError, "A block must be passed to subscribe"
31
+ end
32
+ add_subscribed_block(&block)
33
+ end
34
+
35
+ private
36
+
37
+ attr_reader :subscribed_blocks
38
+
39
+ def add_subscribed_block(&block)
40
+ subscribed_blocks.push(block)
41
+ end
42
+
43
+ def default_logger
44
+ logger = Logger.new(STDOUT)
45
+ logger.level = Logger::DEBUG
46
+ logger
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ # Top-level module.
3
+ module EventTrain
4
+ VERSION = File.read(File.expand_path("../../../VERSION", __FILE__)).strip
5
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: event_train
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Romain Champourlier
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-25 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
+ description: This provides a simple implementation of an event bus. It makes easy
56
+ to use the abstractionwithout needing dedicated components, like RabbitMQ. Using
57
+ this will however make it easyto rewrite your code to use more solid foundations
58
+ for the future.
59
+ email:
60
+ - public@rchampourlier.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".codeclimate.yml"
66
+ - ".gitignore"
67
+ - ".rspec"
68
+ - ".rubocop.yml"
69
+ - ".travis.yml"
70
+ - CODE_OF_CONDUCT.md
71
+ - Gemfile
72
+ - Guardfile
73
+ - README.md
74
+ - Rakefile
75
+ - VERSION
76
+ - bin/console
77
+ - bin/setup
78
+ - event_train.gemspec
79
+ - lib/event_train.rb
80
+ - lib/event_train/channel.rb
81
+ - lib/event_train/version.rb
82
+ homepage: https://github.com/rchampourlier/event_train
83
+ licenses: []
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.5.1
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: A really simple implementation of an event bus in Ruby, mostly for my own
105
+ projects.
106
+ test_files: []