signal 0.2.2 → 0.3.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/README.md +43 -1
 - data/lib/signal.rb +1 -0
 - data/lib/signal/call.rb +21 -0
 - data/lib/signal/version.rb +1 -1
 - data/spec/signal/call_spec.rb +18 -0
 - data/spec/spec_helper.rb +1 -0
 - data/spec/support/observable_with_call.rb +13 -0
 - metadata +7 -3
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: f93acc02fac0dc15d821540be7b8429a1c4ee767
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: ccbf61c85f7448b07f32e5a708905862b63a66f3
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 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,  
     | 
| 
      
 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 
     | 
    
         | 
    
        data/lib/signal.rb
    CHANGED
    
    
    
        data/lib/signal/call.rb
    ADDED
    
    | 
         @@ -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
         
     | 
    
        data/lib/signal/version.rb
    CHANGED
    
    
| 
         @@ -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
         
     | 
    
        data/spec/spec_helper.rb
    CHANGED
    
    
    
        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. 
     | 
| 
      
 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- 
     | 
| 
      
 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: 
         
     |