signal 0.2.2 → 0.3.0

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: 4bab949237a86fb74f7d3d89ce215a614f87f725
4
- data.tar.gz: cc343238a6eacaa010c910ebff962729baa62f30
3
+ metadata.gz: f93acc02fac0dc15d821540be7b8429a1c4ee767
4
+ data.tar.gz: ccbf61c85f7448b07f32e5a708905862b63a66f3
5
5
  SHA512:
6
- metadata.gz: 1358d2e51f6f5fb757fdad35e74325adc15be310d6a1b56ea7dd432b7861afee4ffd2b32fbe41d473bd8335eb39b11d7957ce29fb587cdfdbb7012e191c34034
7
- data.tar.gz: 55b1bd38fbb3b9ac9eb10c29feb6f3cfb5bf4116e30400e91da211571661080132ba3edd5bbd780139747c0271fcc9497e5bd1c15e717d810f2b66743210cf10
6
+ metadata.gz: edebdf9690ad027d27f25a1e80d94272750b1a62434df184d6894c7a253b0f050fe06dda337601179886d22b02d4bffeb5155e695a8d7d09b27f88aef4f3d1f6
7
+ data.tar.gz: 055b3b383611a0de961f0f66d4cfd7b0b40fcfc36dd252e46d2cdade60dd34b9f73ce50bd07a4e677e596063c3154dbb96687ac788236d4cad9e06a6a15526ac
data/README.md CHANGED
@@ -172,13 +172,55 @@ Although there's no special code for Rails, here's just an example of how you ca
172
172
  class UsersController < ApplicationController
173
173
  def create
174
174
  @user = User.new(user_params)
175
+
175
176
  Signup.new(@user)
176
177
  .on(:success) { redirect_to login_path, notice: 'Welcome to MyApp!' }
177
178
  .on(:failure) { render :new }
179
+ .call
178
180
  end
179
181
  end
180
182
  ```
181
183
 
184
+ If you're using plain ActiveRecord, just do something like the following:
185
+
186
+ ```ruby
187
+ class UsersController < ApplicationController
188
+ def create
189
+ @user = User.new(user_params)
190
+ @user
191
+ .on(:create) { redirect_to login_path, notice: 'Welcome to MyApp!' }
192
+ .on(:validation) { render :new }
193
+ .save
194
+ end
195
+ end
196
+ ```
197
+
198
+ ### Signal::Call
199
+
200
+ You can include `Signal::Call` instead, so you can have a common interface for your observable object. This will add the `.call()` method to the target class, which will delegate attributes to the observable's `initialize` method and call its `call` method.
201
+
202
+ ```ruby
203
+ class Contact
204
+ include Signal.call
205
+
206
+ attr_reader :name, :email
207
+
208
+ def initialize(name, email)
209
+ @name, @email = name, email
210
+ end
211
+
212
+ def call
213
+ emit(:output, self)
214
+ end
215
+ end
216
+
217
+ Contact.call('John', 'john@example.com') do |o|
218
+ o.on(:output) {|contact| puts contact }
219
+ end
220
+ ```
221
+
222
+ Notice that you don't have to explicit call the instance's `call` method; `Contact.call` will initialize the object with all the provided parameters and call `Contact#call` after the block has been executed.
223
+
182
224
  ## Contributing
183
225
 
184
226
  1. Fork it
@@ -197,7 +239,7 @@ Permission is hereby granted, free of charge, to any person obtaining
197
239
  a copy of this software and associated documentation files (the
198
240
  "Software"), to deal in the Software without restriction, including
199
241
  without limitation the rights to use, copy, modify, merge, publish,
200
- distribute, sublicense, and/or sell copies of the Software, and to
242
+ distribute, sub-license, and/or sell copies of the Software, and to
201
243
  permit persons to whom the Software is furnished to do so, subject to
202
244
  the following conditions:
203
245
 
@@ -1,5 +1,6 @@
1
1
  require 'signal/active_record'
2
2
  require 'signal/listener'
3
+ require 'signal/call'
3
4
  require 'signal/version'
4
5
 
5
6
  module Signal
@@ -0,0 +1,21 @@
1
+ module Signal
2
+ def self.call
3
+ Call
4
+ end
5
+
6
+ module Call
7
+ def self.included(target)
8
+ target.include(Signal)
9
+ target.extend(ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+ def call(*args, &block)
14
+ new(*args).tap do |instance|
15
+ yield(instance)
16
+ instance.call
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module Signal
2
- VERSION = '0.2.2'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Signal::Call do
4
+ let(:callable) { Callable.new }
5
+
6
+ it 'initializes observable with arguments' do
7
+ observable = ObservableWithCall.call(1, 2, 3) {}
8
+ expect(observable.args).to eq([1, 2, 3])
9
+ end
10
+
11
+ it 'triggers event' do
12
+ expect(callable).to receive(:called).with([1, 2, 3])
13
+
14
+ ObservableWithCall.call(1, 2, 3) do |o|
15
+ o.on(:args, &callable)
16
+ end
17
+ end
18
+ end
@@ -8,6 +8,7 @@ I18n.enforce_available_locales = false
8
8
 
9
9
  require 'signal'
10
10
  require 'support/observable'
11
+ require 'support/observable_with_call'
11
12
  require 'support/callable'
12
13
  require 'support/user'
13
14
 
@@ -0,0 +1,13 @@
1
+ class ObservableWithCall
2
+ include Signal.call
3
+
4
+ attr_reader :args
5
+
6
+ def initialize(*args)
7
+ @args = args
8
+ end
9
+
10
+ def call
11
+ emit(:args, @args)
12
+ end
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: signal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-10 00:00:00.000000000 Z
11
+ date: 2015-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -89,15 +89,18 @@ files:
89
89
  - examples/listener.rb
90
90
  - lib/signal.rb
91
91
  - lib/signal/active_record.rb
92
+ - lib/signal/call.rb
92
93
  - lib/signal/listener.rb
93
94
  - lib/signal/version.rb
94
95
  - signal.gemspec
95
96
  - spec/signal/activerecord_spec.rb
97
+ - spec/signal/call_spec.rb
96
98
  - spec/signal/listener_spec.rb
97
99
  - spec/signal/signal_spec.rb
98
100
  - spec/spec_helper.rb
99
101
  - spec/support/callable.rb
100
102
  - spec/support/observable.rb
103
+ - spec/support/observable_with_call.rb
101
104
  - spec/support/user.rb
102
105
  homepage: http://github.com/fnando/signal
103
106
  licenses: []
@@ -125,10 +128,11 @@ summary: A simple observer implementation for POROs (Plain Old Ruby Object) and
125
128
  objects.
126
129
  test_files:
127
130
  - spec/signal/activerecord_spec.rb
131
+ - spec/signal/call_spec.rb
128
132
  - spec/signal/listener_spec.rb
129
133
  - spec/signal/signal_spec.rb
130
134
  - spec/spec_helper.rb
131
135
  - spec/support/callable.rb
132
136
  - spec/support/observable.rb
137
+ - spec/support/observable_with_call.rb
133
138
  - spec/support/user.rb
134
- has_rdoc: