nala 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0fb5349f94c5a8d5ba4117b425eed6e9e50d336
4
- data.tar.gz: 19c87de3c0cf10b66d7af408f6f999335cfe64a6
3
+ metadata.gz: 1de5be775b1e14835cc70a4e7a8b0068f13caa9d
4
+ data.tar.gz: 555a05e4158913607cc7b5ff1a8c54b734c89cf4
5
5
  SHA512:
6
- metadata.gz: 7cbaf45efbcde34c4a2b630cfd90b94a93bf497c5edc1fb759b25f85f923768a5e4de823d43f5c1a1b6bdd43a72a5983b9f1edaa64cb4d1d5632a9e3466b2ec7
7
- data.tar.gz: b51059c263359f5bfd0564fa44e3e64d7f7a1b93f5630a7c58351b1c8f440e1fcf080e99878a643d4bdb02c92968dbeea14820f576eb944addfb17a48276d944
6
+ metadata.gz: 061bf9c1b1ca1b18f16dc5d08753261929365028bca2d84210a4d711f0297e5e659e4396daf352123777a96dcba81c4e15232f0348dc8a14db9b5b5dbd376a9d
7
+ data.tar.gz: c9ec24f01a7055c227afde5bdbf325b6e590f64e1a659363f08781e6ea985fc91f5a54c212f14d7e5668585303be15d8ca4b2612f8706b6175a96145986c0e82
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) [year] [fullname]
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ module Nala
2
+ class EventEmitter
3
+ def initialize
4
+ @events = {}
5
+ end
6
+
7
+ def store_result(event, args)
8
+ events[event] = args
9
+ end
10
+
11
+ def on(event)
12
+ yield(*events[event]) if events.key?(event)
13
+
14
+ self
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :events
20
+ end
21
+ end
@@ -0,0 +1,29 @@
1
+ module Nala
2
+ module Publisher
3
+ def self.included(base)
4
+ base.send(:extend, ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def call(*args)
9
+ EventEmitter.new.tap do |emitter|
10
+ new(*args).listen_with(emitter).call
11
+ end
12
+ end
13
+ end
14
+
15
+ def listen_with(emitter)
16
+ @emitter = emitter
17
+
18
+ self
19
+ end
20
+
21
+ def publish(event, *args)
22
+ emitter.store_result(event, args)
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :emitter
28
+ end
29
+ end
data/lib/nala/rspec.rb ADDED
@@ -0,0 +1,37 @@
1
+ require "rspec/expectations"
2
+
3
+ module Nala
4
+ class BlockSpy
5
+ attr_reader :called, :args
6
+
7
+ def initialize
8
+ @called = false
9
+ @args = []
10
+ end
11
+
12
+ def called!
13
+ @called = true
14
+ end
15
+
16
+ def called_with!(args)
17
+ called!
18
+ @args = *args
19
+ end
20
+
21
+ def called?
22
+ @called
23
+ end
24
+ end
25
+ end
26
+
27
+ RSpec::Matchers.define :be_called_with do |*expected_args|
28
+ match do |block|
29
+ block.called? && block.args == expected_args
30
+ end
31
+
32
+ failure_message do |block|
33
+ return "expected the block to be called but it was not" unless block.called?
34
+
35
+ "expected the block to be called with the arguments #{expected_args.inspect} but was called with #{block.args.inspect}"
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Nala
2
+ VERSION = "0.0.4"
3
+ end
data/readme.md ADDED
@@ -0,0 +1,142 @@
1
+ # Nala
2
+
3
+ Lightweight gem for adding events to use case style classes.
4
+
5
+ ## Install
6
+
7
+ To install via RubyGems run:
8
+
9
+ ```
10
+ gem install nala
11
+ ```
12
+
13
+ To install via bundler, add this line to your Gemfile and run `bundle install`:
14
+
15
+ ```
16
+ gem "nala"
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Nala is intended to be used with use case style classes where the class performs a
22
+ single action. These classes typically have verb names such as `PlaceOrder` etc.
23
+
24
+ To use, add the `Nala::Publisher` module to your class and expose a `call`
25
+ instance method. If your class requires data, it can be passed to it via the constructor.
26
+
27
+ For example:
28
+
29
+ ```ruby
30
+ class PlaceOrder
31
+ include Nala::Publisher
32
+
33
+ def initialize(params)
34
+ @params = params
35
+ end
36
+
37
+ def call
38
+ # ...
39
+
40
+ if order.save
41
+ publish(:ok, order)
42
+ else
43
+ publish(:error)
44
+ end
45
+ end
46
+ end
47
+ ```
48
+
49
+ Usage of these use case style classes gives a home to your business logic, removes
50
+ conditionals from your Rails controllers and helps prevent your models getting too
51
+ big.
52
+
53
+ ```ruby
54
+ class OrderController < ApplicationController
55
+ # ...
56
+
57
+ def create
58
+ PlaceOrder.call(order_params)
59
+ .on(:ok) { |order| redirect_to orders_path, :notice => "..." }
60
+ .on(:error) { render :new }
61
+ end
62
+
63
+ # ...
64
+ end
65
+ ```
66
+
67
+ This comes in handy when there are more than two out comes to a use case:
68
+
69
+ ```ruby
70
+ class RegistrationController < ApplicationController
71
+ # ...
72
+
73
+ def create
74
+ RegisterAccount.call(account_params)
75
+ .on(:already_registered) { redirect_to login_path, :notice => "..." }
76
+ .on(:ok) { |user| redirect_to dashboard_path, :notice => "..." }
77
+ .on(:error) { render :new }
78
+ end
79
+
80
+ # ...
81
+ end
82
+ ```
83
+
84
+ ## Testing with RSpec
85
+
86
+ If you are using RSpec then you can use the supplied matcher by adding the following line to your `spec_helper.rb` file:
87
+
88
+ ```ruby
89
+ require "nala/rspec"
90
+ ```
91
+
92
+ Then within your specs, you can confirm that a handler is called with the following:
93
+
94
+ ```ruby
95
+ let(:block) { Nala::BlockSpy.new }
96
+
97
+ it "invokes a handler for a published event" do
98
+ SuccessClass.call.on(:success) { block.called! }
99
+
100
+ expect(block).to be_called
101
+ end
102
+ ```
103
+
104
+ If you want to check the arguments that are passed to the block you can use the following:
105
+
106
+ ```ruby
107
+ let(:block) { Nala::BlockSpy.new }
108
+
109
+ it "passes multiple arguments to handlers" do
110
+ PublishArgsClass.call
111
+ .on(:multiple) { |*x| block.called_with!(x) }
112
+
113
+ expect(block).to be_called_with(:a, :b, :c)
114
+ end
115
+ ```
116
+
117
+ If you need check the arguments attributes in more detail you can do the following:
118
+
119
+ ```ruby
120
+ let(:block) { Nala::BlockSpy.new }
121
+
122
+ it "passes a user with the correct name" do
123
+ RegisterUser.call
124
+ .on(:success) { |*x| block.called_with!(x) }
125
+
126
+ user = block.args.first
127
+ expect(user.name).to eq("Andy")
128
+ end
129
+ ```
130
+
131
+ ## Building a publishing gem updates
132
+
133
+ Bump the version number in `lib/nala/version.rb` and use the same number in the command below:
134
+
135
+ ```
136
+ gem build nala.gemspec
137
+ gem push nala-0.0.3.gem
138
+ ```
139
+
140
+ ## License
141
+
142
+ See the [LICENSE](LICENSE.txt) file for license rights and limitations (MIT).
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nala
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Pike
@@ -58,7 +58,13 @@ executables: []
58
58
  extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
+ - LICENSE.txt
61
62
  - lib/nala.rb
63
+ - lib/nala/event_emitter.rb
64
+ - lib/nala/publisher.rb
65
+ - lib/nala/rspec.rb
66
+ - lib/nala/version.rb
67
+ - readme.md
62
68
  homepage: https://github.com/loyaltylion/nala
63
69
  licenses:
64
70
  - MIT