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 +4 -4
- data/Gemfile +4 -2
- data/Gemfile.lock +34 -0
- data/README.md +55 -2
- data/Rakefile +4 -2
- data/bin/console +4 -3
- data/lib/simple_tools.rb +5 -2
- data/lib/simple_tools/events.rb +24 -0
- data/lib/simple_tools/events/subscriber.rb +30 -0
- data/lib/simple_tools/events/subscribers.rb +35 -0
- data/lib/simple_tools/version.rb +3 -1
- data/simple_tools.gemspec +2 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 22b2635dbf11b1b9b7351f49acd3aa871b7894f09c429f776bf8e8eabeeb3ec1
|
4
|
+
data.tar.gz: be3960f0d89e35d1a636739760f3d6c506c6aad05d3f47933a54d60ece2d6715
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b62bf8cde989ece7c08fd7cc7282bd9dafd2dc1ef8609a6414676ee495251fc921328da3e16b7aa843589c3ca5089cfab9b54b69a830bed83495674394a6d435
|
7
|
+
data.tar.gz: 742bef145d4c517de4c8a6a2458cc336ead4b241e8332419fb61e911226465a96395b95fdb23ed784284be238fdc9c6209483f96b83120dd427080602d2e50d6
|
data/Gemfile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
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
data/bin/console
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
|
-
require
|
4
|
-
require
|
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
|
14
|
+
require 'irb'
|
14
15
|
IRB.start(__FILE__)
|
data/lib/simple_tools.rb
CHANGED
@@ -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
|
data/lib/simple_tools/version.rb
CHANGED
data/simple_tools.gemspec
CHANGED
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.
|
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-
|
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
|