pusherable 1.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pusherable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tony Coconate
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Pusherable
2
+
3
+ Adds callback hooks for your ActiveRecord models for sending messages to a Pusher channel.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pusherable'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pusherable
18
+
19
+ ## Usage
20
+
21
+ Add in the following lines to any ActiveRecord model class:
22
+
23
+ pusherable(YOUR_CHANNEL)
24
+
25
+ On your subscribed client(s), events will be triggered by Pusher reflecting your ActiveRecord create/update/destroy actions.
26
+
27
+ ### Example
28
+
29
+ If you have a model called, __Story__, and you create a new record, Pusher will receive an event called, "story.create".
30
+ It will also carry a payload of data containing the __model_id__
31
+
32
+ Here is a list of the ActiveRecord callbacks that trigger Pusher events...
33
+
34
+ ```
35
+ "model.create" => after_create
36
+ "model.update" => after_update
37
+ "model.destroy" => before_destroy
38
+ ```
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ desc 'Default: run specs'
4
+ task :default => :spec
5
+
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new do |t|
8
+ t.pattern = 'spec/**/*_spec.rb'
9
+ end
@@ -0,0 +1,3 @@
1
+ module Pusherable
2
+ VERSION = '1.0.1'
3
+ end
data/lib/pusherable.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'pusherable/version'
2
+
3
+ module Pusherable
4
+ def pusherable?
5
+ false
6
+ end
7
+
8
+ def pusherable(pusherable_channel='test_channel')
9
+ class_attribute :pusherable_channel
10
+ self.pusherable_channel = pusherable_channel
11
+
12
+ class_eval do
13
+ after_create :pusherable_trigger_create
14
+ after_update :pusherable_trigger_update
15
+ before_destroy :pusherable_trigger_destroy
16
+
17
+ def self.pusherable?
18
+ true
19
+ end
20
+
21
+ private
22
+
23
+ def pusherable_class_name
24
+ self.class.name.underscore
25
+ end
26
+
27
+ def pusherable_trigger_create
28
+ Pusher.trigger(pusherable_channel, "#{pusherable_class_name}.create", { model_id: self.id })
29
+ end
30
+
31
+ def pusherable_trigger_update
32
+ Pusher.trigger(pusherable_channel, "#{pusherable_class_name}.update", { model_id: self.id })
33
+ end
34
+
35
+ def pusherable_trigger_destroy
36
+ Pusher.trigger(pusherable_channel, "#{pusherable_class_name}.destroy", { model_id: self.id })
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ if defined?(ActiveRecord::Base)
43
+ ActiveRecord::Base.extend Pusherable
44
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pusherable/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'pusherable'
8
+ gem.version = Pusherable::VERSION
9
+ gem.authors = ['Tony Coconate']
10
+ gem.email = ['me@tonycoconate.com']
11
+ gem.description = %q{Adds callback hooks for your ActiveRecord models for sending messages to a Pusher channel.}
12
+ gem.summary = %q{Adds callback hooks for your ActiveRecord models for sending messages to a Pusher channel.}
13
+ gem.homepage = 'https://github.com/tonycoco/pusherable'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+
20
+ gem.add_development_dependency 'activerecord', '~> 3.2.0'
21
+ gem.add_development_dependency 'pusher', '~> 0.11.0'
22
+ gem.add_development_dependency 'sqlite3'
23
+ gem.add_development_dependency 'rspec', '~> 2.12.0'
24
+ gem.add_development_dependency 'acts_as_fu'
25
+ gem.add_development_dependency 'database_cleaner'
26
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pusherable do
4
+ describe 'setup' do
5
+ it 'should make ActiveRecord models pusherable' do
6
+ NonPusherableModel.pusherable?.should == false
7
+ PusherableModel.pusherable?.should == true
8
+ end
9
+
10
+ it 'should set the channel to push to' do
11
+ DefaultedPusherableModel.pusherable_channel.should == 'test_channel'
12
+ PusherableModel.pusherable_channel.should == 'our_channel'
13
+
14
+ default_model = DefaultedPusherableModel.new
15
+ setup_model = PusherableModel.new
16
+
17
+ default_model.pusherable_channel.should == 'test_channel'
18
+ setup_model.pusherable_channel.should == 'our_channel'
19
+ end
20
+ end
21
+
22
+ describe 'callbacks' do
23
+ before(:each) do
24
+ @pusherable_model = PusherableModel.new
25
+ @non_pusherable_model = NonPusherableModel.new
26
+ Pusher.stub(:trigger).and_return true
27
+ end
28
+
29
+ it 'should trigger after create' do
30
+ @pusherable_model.should_receive(:pusherable_trigger_create).once
31
+ @pusherable_model.save
32
+ end
33
+
34
+ it 'should trigger after update' do
35
+ @pusherable_model.should_receive(:pusherable_trigger_create).once
36
+ @pusherable_model.save
37
+ @pusherable_model.should_receive(:pusherable_trigger_update).once
38
+ @pusherable_model.save!
39
+ end
40
+
41
+ it 'should trigger after update' do
42
+ @pusherable_model.should_receive(:pusherable_trigger_create).once
43
+ @pusherable_model.save
44
+ @pusherable_model.should_receive(:pusherable_trigger_destroy).once
45
+ @pusherable_model.destroy
46
+ end
47
+
48
+ it 'should not trigger events on a regular ActiveRecord model' do
49
+ @non_pusherable_model.should_not_receive(:pusherable_trigger_create)
50
+ @non_pusherable_model.save
51
+ @non_pusherable_model.should_not_receive(:pusherable_trigger_update)
52
+ @non_pusherable_model.save
53
+ @non_pusherable_model.should_not_receive(:pusherable_trigger_destroy)
54
+ @non_pusherable_model.destroy
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,24 @@
1
+ require 'database_cleaner'
2
+ require 'active_record'
3
+ require 'pusher'
4
+ require 'pusherable'
5
+
6
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
7
+
8
+ load File.dirname(__FILE__) + '/support/schema.rb'
9
+ load File.dirname(__FILE__) + '/support/models.rb'
10
+
11
+ RSpec.configure do |config|
12
+ config.before(:suite) do
13
+ DatabaseCleaner.strategy = :transaction
14
+ DatabaseCleaner.clean_with(:truncation)
15
+ end
16
+
17
+ config.before(:each) do
18
+ DatabaseCleaner.start
19
+ end
20
+
21
+ config.after(:each) do
22
+ DatabaseCleaner.clean
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ class NonPusherableModel < ActiveRecord::Base
2
+ end
3
+
4
+ class DefaultedPusherableModel < ActiveRecord::Base
5
+ pusherable
6
+ end
7
+
8
+ class PusherableModel < ActiveRecord::Base
9
+ pusherable('our_channel')
10
+ end
@@ -0,0 +1,15 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :non_pusherable_models, :force => true do |t|
5
+ t.timestamps
6
+ end
7
+
8
+ create_table :defaulted_pusherable_models, :force => true do |t|
9
+ t.timestamps
10
+ end
11
+
12
+ create_table :pusherable_models, :force => true do |t|
13
+ t.timestamps
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pusherable
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Tony Coconate
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ type: :development
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 3.2.0
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: pusher
32
+ type: :development
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: 0.11.0
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.11.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ type: :development
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ type: :development
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ version: 2.12.0
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 2.12.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: acts_as_fu
80
+ type: :development
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: database_cleaner
96
+ type: :development
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Adds callback hooks for your ActiveRecord models for sending messages
111
+ to a Pusher channel.
112
+ email:
113
+ - me@tonycoconate.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - lib/pusherable.rb
124
+ - lib/pusherable/version.rb
125
+ - pusherable.gemspec
126
+ - spec/pusherable_spec.rb
127
+ - spec/spec_helper.rb
128
+ - spec/support/models.rb
129
+ - spec/support/schema.rb
130
+ homepage: https://github.com/tonycoco/pusherable
131
+ licenses: []
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 1.8.23
151
+ signing_key:
152
+ specification_version: 3
153
+ summary: Adds callback hooks for your ActiveRecord models for sending messages to
154
+ a Pusher channel.
155
+ test_files:
156
+ - spec/pusherable_spec.rb
157
+ - spec/spec_helper.rb
158
+ - spec/support/models.rb
159
+ - spec/support/schema.rb