simple_tools 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b41f5e2bc4aae37adeeafe28d802074c6275a2f2ca6f4adb8a45536f3b41e362
4
- data.tar.gz: e56eb6bb951d112b7c31367ffc80b5b50e31d26b362047afa84d43ac0e33f1fb
3
+ metadata.gz: 22b2635dbf11b1b9b7351f49acd3aa871b7894f09c429f776bf8e8eabeeb3ec1
4
+ data.tar.gz: be3960f0d89e35d1a636739760f3d6c506c6aad05d3f47933a54d60ece2d6715
5
5
  SHA512:
6
- metadata.gz: fc6fa20a9d3e7f7b1d8e2ed06e1ca91d49c02174acdc8a2d7ce93ca375135a521e4d1fa21b4dc7011509f6f60f2e77a4b90928a2e2a669efcab212aa95fb32f9
7
- data.tar.gz: 6ee47fd86e0b0e7823ddc01b6d0aa56538782f5afb174693603b8964b8d985f3051d4abda719c3aa61e825d5ee78a9ddca918c21115b5e3496e9d21ac1871769
6
+ metadata.gz: b62bf8cde989ece7c08fd7cc7282bd9dafd2dc1ef8609a6414676ee495251fc921328da3e16b7aa843589c3ca5089cfab9b54b69a830bed83495674394a6d435
7
+ data.tar.gz: 742bef145d4c517de4c8a6a2458cc336ead4b241e8332419fb61e911226465a96395b95fdb23ed784284be238fdc9c6209483f96b83120dd427080602d2e50d6
data/Gemfile CHANGED
@@ -1,6 +1,8 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
6
 
5
7
  # Specify your gem's dependencies in simple_tools.gemspec
6
8
  gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ simple_tools (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ rake (10.1.0)
11
+ rspec (3.1.0)
12
+ rspec-core (~> 3.1.0)
13
+ rspec-expectations (~> 3.1.0)
14
+ rspec-mocks (~> 3.1.0)
15
+ rspec-core (3.1.7)
16
+ rspec-support (~> 3.1.0)
17
+ rspec-expectations (3.1.2)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.1.0)
20
+ rspec-mocks (3.1.3)
21
+ rspec-support (~> 3.1.0)
22
+ rspec-support (3.1.2)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler (~> 1.16)
29
+ rake (~> 10.0)
30
+ rspec
31
+ simple_tools!
32
+
33
+ BUNDLED WITH
34
+ 1.16.0
data/README.md CHANGED
@@ -20,6 +20,10 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
+ * [Operation](#Operation)
24
+ * [Pub/Sub (Events)](#pubsub)
25
+
26
+
23
27
  ### Operation
24
28
  Inspired by trailblaizer operation.
25
29
 
@@ -55,7 +59,7 @@ Example of calling operation:
55
59
  `SimpleTools::Operation` respond to `.call` and can receive hash of parameters.
56
60
  Inside operation to share variables between steps use `update_context(:any_key, 'any value')` setter and `context[:any_key]` getter.
57
61
 
58
- `.call` returns object which respond to `.success?` and returns boolean value.
62
+ `.call` returns object which respond to `.success?` and returns boolean value.
59
63
 
60
64
  Also `.context` method is available which returns setted in operation values.
61
65
 
@@ -85,7 +89,7 @@ class FailedOperation < SimpleTools::Operation
85
89
  end
86
90
  end
87
91
  ```
88
- `error!`, `errors!` - add new item(s) to list of errors. It do not raise exception and dont break execution of current method.
92
+ `error!`, `errors!` - add new item(s) to list of errors. It do not raise exception and dont break execution of current method.
89
93
 
90
94
  Example of calling failed operation:
91
95
  ```ruby
@@ -98,8 +102,57 @@ Example of calling failed operation:
98
102
  => result.errors
99
103
  # {name: ['error description', 'one more error', 'and another error']}
100
104
  ```
105
+ ------
106
+
101
107
 
108
+ ### Pub/Sub
109
+ Publish/subscribe messaging, or pub/sub messaging, is a form of service-to-service communication. In a pub/sub model, any message published to a topic is immediately received by all of the subscribers to the topic. Pub/sub messaging can be used to enable event-driven architectures, or to decouple applications in order to increase performance, reliability and scalability.
110
+
111
+ Write subscriber which will be waiting for event:
112
+ ```ruby
113
+ class NewSubscriber < SimpleTools::Events::Subscriber
114
+ def handle
115
+ p 'notification about event received!'
116
+ p event_name
117
+ p payload
118
+ end
119
+ end
120
+ ```
121
+ `payload`, `event_name` methods available in instance of `SimpleTools::Events::Subscriber`.
122
+
123
+ Subscribe to event by name:
124
+ ```ruby
125
+ SimpleTools::Events.subscribe('some_event_name', NewSubscriber)
126
+ ```
102
127
 
128
+ Publish event:
129
+ ```ruby
130
+ SimpleTools::Events.publish('some_event_name')
131
+ ```
132
+ Output example:
133
+ ```
134
+ "notification about event received!"
135
+ "some_event_name"
136
+ nil
137
+ ```
138
+ Or publish event with payload if required:
139
+ ```ruby
140
+ payload = {
141
+ id: 11,
142
+ type: 'description of type',
143
+ some_values: [1,2,3]
144
+ }
145
+
146
+ SimpleTools::Events.publish('some_event_name', payload)
147
+ ```
148
+
149
+ Output example:
150
+ ```
151
+ "notification about event received!"
152
+ "some_event_name"
153
+ {:id=>11, :type=>"description of type", :some_values=>[1, 2, 3]}
154
+ ```
155
+ ------
103
156
  ## Development
104
157
 
105
158
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/Rakefile CHANGED
@@ -1,2 +1,4 @@
1
- require "bundler/gem_tasks"
2
- task :default => :spec
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: :spec
data/bin/console CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "simple_tools"
4
+ require 'bundler/setup'
5
+ require 'simple_tools'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "simple_tools"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- require "irb"
14
+ require 'irb'
14
15
  IRB.start(__FILE__)
data/lib/simple_tools.rb CHANGED
@@ -1,5 +1,8 @@
1
- require "simple_tools/version"
2
- require "simple_tools/operation"
1
+ # frozen_string_literal: true
2
+
3
+ require 'simple_tools/version'
4
+ require 'simple_tools/operation'
5
+ require 'simple_tools/events'
3
6
 
4
7
  module SimpleTools
5
8
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+ require 'singleton'
5
+ require 'simple_tools/events/subscriber'
6
+ require 'simple_tools/events/subscribers'
7
+
8
+ module SimpleTools
9
+ module Events
10
+ def self.subscribers
11
+ Subscribers.instance
12
+ end
13
+
14
+ def self.subscribe(event_name, subscriber_class)
15
+ subscribers.add(event_name, subscriber_class)
16
+ end
17
+
18
+ def self.publish(event_name, payload = nil)
19
+ subscribers.select(event_name).each do |subscriber|
20
+ subscriber[:subscriber_class].notify(event_name, payload)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleTools
4
+ module Events
5
+ class Subscriber
6
+ BehaviourNotDefined = Class.new(StandardError)
7
+
8
+ attr_reader :event_name, :payload
9
+
10
+ def self.notify(event_name, payload)
11
+ new(event_name, payload).call
12
+ end
13
+
14
+ def initialize(event_name, payload)
15
+ @event_name = event_name
16
+ @payload = payload
17
+ end
18
+
19
+ def call
20
+ handle
21
+ end
22
+
23
+ private
24
+
25
+ def handle
26
+ raise(BehaviourNotDefined, 'Implement :handle method in your subscriber class')
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleTools
4
+ module Events
5
+ class Subscribers
6
+ include Singleton
7
+
8
+ def add(event_name, subscriber_class)
9
+ !!list.add?(event_name: event_name, subscriber_class: subscriber_class)
10
+ end
11
+
12
+ def remove_by_event!(event_name)
13
+ !!list.reject! { |item| item[:event_name] == event_name }
14
+ end
15
+
16
+ def remove_by_subscriber!(subscriber_class)
17
+ !!list.reject! { |item| item[:subscriber_class] == subscriber_class }
18
+ end
19
+
20
+ def select(event_name)
21
+ list.select { |item| item[:event_name] == event_name }
22
+ end
23
+
24
+ def to_a
25
+ list.to_a
26
+ end
27
+
28
+ private
29
+
30
+ def list
31
+ @list ||= Set[]
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SimpleTools
2
- VERSION = "0.1.0"
4
+ VERSION = '0.2.0'
3
5
  end
data/simple_tools.gemspec CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  lib = File.expand_path('lib', __dir__)
2
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
5
  require 'simple_tools/version'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - isidzukuri
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-12 00:00:00.000000000 Z
11
+ date: 2019-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -61,11 +61,15 @@ extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
63
  - Gemfile
64
+ - Gemfile.lock
64
65
  - README.md
65
66
  - Rakefile
66
67
  - bin/console
67
68
  - bin/setup
68
69
  - lib/simple_tools.rb
70
+ - lib/simple_tools/events.rb
71
+ - lib/simple_tools/events/subscriber.rb
72
+ - lib/simple_tools/events/subscribers.rb
69
73
  - lib/simple_tools/operation.rb
70
74
  - lib/simple_tools/version.rb
71
75
  - simple_tools.gemspec