better_call_redis 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3f2d5235623cd3e5b4a9aea2ea826051a872d7c6
4
+ data.tar.gz: 672e0a5ec38c20baaf3da23d6607fc6ebf3bee04
5
+ SHA512:
6
+ metadata.gz: 331068f68c982d6da558c51f59763e497373217f12bdd9d9f3df05d1a4a0dd694b0f61157cb24b072d4a57f4ff98e6fe67853f70976a7ac54d27989bacc1e9a0
7
+ data.tar.gz: dde38aea1166dea7b975e59f5ce394c752189eb43f65d9e5c851438c07261666118ddf2a93de28f1ef0ca8f7781638b640b467ca5dd919dd13b52a2c25002c94
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,42 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.0
7
+ - 2.1.1
8
+ - jruby-19mode # JRuby in 1.9 mode
9
+ - rbx-2.2.1
10
+
11
+ gemfile:
12
+ - gemfiles/rails_3.0.gemfile
13
+ - gemfiles/rails_3.1.gemfile
14
+ - gemfiles/rails_3.2.gemfile
15
+ - gemfiles/rails_4.0.gemfile
16
+ - gemfiles/rails_4.1.gemfile
17
+ - gemfiles/rails_4.2.gemfile
18
+
19
+ matrix:
20
+ allow_failures:
21
+ - rvm: rbx-2.2.1
22
+ - rvm: jruby-19mode
23
+ exclude:
24
+ - rvm: 1.9.3
25
+ gemfile: gemfiles/rails_4.0.gemfile
26
+ - rvm: 1.9.3
27
+ gemfile: gemfiles/rails_4.1.gemfile
28
+ - rvm: jruby-19mode
29
+ gemfile: gemfiles/rails_3.2.gemfile
30
+ - rvm: jruby-19mode
31
+ gemfile: gemfiles/rails_4.0.gemfile
32
+ - rvm: jruby-19mode
33
+ gemfile: gemfiles/rails_4.1.gemfile
34
+ - rvm: rbx-2.2.1
35
+ gemfile: gemfiles/rails_3.2.gemfile
36
+ - rvm: rbx-2.2.1
37
+ gemfile: gemfiles/rails_4.0.gemfile
38
+ - rvm: rbx-2.2.1
39
+ gemfile: gemfiles/rails_4.1.gemfile
40
+
41
+ before_script:
42
+ - bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in better_call_redis.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 TODO: Write your name
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,55 @@
1
+ # BetterCallRedis
2
+
3
+ [![Build Status](https://api.travis-ci.org/clod81/better_call_redis.svg)](http://travis-ci.org/clod81/better_call_redis)
4
+
5
+ Active Record Callback that pushes notification of creation or deletion to a Redis channel.
6
+ Redis pushes a message (notification) to a channel with a configurable namespace when an active record entry is created or deleted.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'better_call_redis'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install better_call_redis
23
+
24
+ ## Usage
25
+
26
+ ### Initializer
27
+
28
+ All those initialize options are optional.
29
+ "Redis.new" will be used with default connection options if not specified.
30
+ "bettercallredis:notification" is the default namespace.
31
+ "deleted_attributes" for logically deleted active record models (ex: table column deleted or cancelled). Default is an empty array.
32
+
33
+ ```ruby
34
+ BetterCallRedis::configure do |bcr|
35
+ bcr.redis = Redis.new
36
+ bcr.namespace = "bettercallredis:notification"
37
+ bcr.deleted_attributes = %w(deleted cancelled)
38
+ end
39
+ ```
40
+
41
+ Include the following in your active record model class to enable redis channel notification messages.
42
+
43
+ ```ruby
44
+ include BetterCallRedis::ActiveRecord
45
+ ```
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it ( https://github.com/[my-github-username]/better_call_redis/fork )
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create a new Pull Request
54
+
55
+ Thanks
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'better_call_redis/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "better_call_redis"
8
+ spec.version = BetterCallRedis::VERSION
9
+ spec.authors = ["Claudio Contin"]
10
+ spec.email = ["contin@gmail.com"]
11
+ spec.summary = %q{Active Record Callback that pushes notification of creation or deletion to a Redis channel}
12
+ spec.description = %q{Redis pushes a message (notification) to a channel when an active record entry is created or deleted}
13
+ spec.homepage = "https://github.com/clod81/better_call_redis"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activerecord", ">= 3.0"
22
+ spec.add_dependency "activesupport", ">= 3.0"
23
+ spec.add_dependency "redis", ">= 3.0.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.2.0"
28
+ spec.add_development_dependency "fakeredis", "~> 0.5.0"
29
+ end
@@ -0,0 +1,17 @@
1
+ require "better_call_redis/version"
2
+ require "better_call_redis/configuration"
3
+ require "better_call_redis/active_record"
4
+
5
+ module BetterCallRedis
6
+
7
+ class << self
8
+ def configure
9
+ yield(configuration) if block_given?
10
+ end
11
+
12
+ def configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,57 @@
1
+ module BetterCallRedis
2
+ module ActiveRecord
3
+
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ private
9
+
10
+ def better_call_redis_notify_redis_created
11
+ better_call_redis_notify(1)
12
+ end
13
+
14
+ def better_call_redis_notify_redis_removed
15
+ better_call_redis_notify(-1)
16
+ end
17
+
18
+ def better_call_redis_logically_delete
19
+ return if self.new_record?
20
+ return unless common_deleted_attribute = self.class.better_call_redis_common_deleted_attribute
21
+ return unless self.send("#{common_deleted_attribute}_changed?")
22
+ self.send("#{common_deleted_attribute}") ? better_call_redis_notify_redis_removed : better_call_redis_notify_redis_created
23
+ end
24
+
25
+ def better_call_redis_notify(action)
26
+ self.class.better_call_redis_configuration.redis.publish(self.class.better_call_redis_configuration.namespace, {class_name: self.class.name, message: action}.to_json)
27
+ end
28
+
29
+ module ClassMethods
30
+
31
+ def self.extended(base)
32
+ base.class_eval do
33
+ before_save :better_call_redis_logically_delete
34
+ after_create :better_call_redis_notify_redis_created
35
+ after_destroy :better_call_redis_notify_redis_removed
36
+ end
37
+
38
+ def better_call_redis_configuration
39
+ BetterCallRedis.configuration
40
+ end
41
+
42
+ def better_call_redis_common_deleted_attribute
43
+ (new.attributes.keys & better_call_redis_configuration.deleted_attributes).first
44
+ end
45
+
46
+ def better_call_redis_count
47
+ common_deleted_attribute = better_call_redis_common_deleted_attribute
48
+ return where("#{common_deleted_attribute} != true").count if common_deleted_attribute
49
+ count
50
+ end
51
+
52
+ end # end extended
53
+
54
+ end # end ClassMethods
55
+
56
+ end
57
+ end
@@ -0,0 +1,21 @@
1
+ module BetterCallRedis
2
+ class Configuration
3
+
4
+ attr_writer :redis
5
+ attr_writer :namespace
6
+ attr_writer :deleted_attributes
7
+
8
+ def redis
9
+ @redis ||= Redis.new
10
+ end
11
+
12
+ def namespace
13
+ @namespace ||= "bettercallredis:notification"
14
+ end
15
+
16
+ def deleted_attributes
17
+ @deleted_attributes ||= []
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module BetterCallRedis
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe "BetterCallRedis::ActiveRecord" do
4
+
5
+ before(:each) do
6
+ BetterCallRedis.configure do |bcr|
7
+ bcr.namespace = "test"
8
+ bcr.deleted_attributes = %w(deleted)
9
+ end
10
+ allow(BetterCallRedis.configuration.redis).to receive(:to_str) { "" }
11
+ end
12
+
13
+ describe "for a new record" do
14
+ before(:each) do
15
+ @model = Person.new
16
+ allow(@model).to receive(:new_record?) { true }
17
+ allow(@model).to receive(:save) { @model.run_callbacks(:create) }
18
+ end
19
+ it "should call redis with correct params" do
20
+ expect(BetterCallRedis.configuration.redis).to receive(:publish).with("test", "{\"class_name\":\"Person\",\"message\":1}") { true }
21
+ @model.save
22
+ end
23
+ end
24
+
25
+ describe "for an existing record" do
26
+ before(:each) do
27
+ @model = Person.new
28
+ allow(@model).to receive(:new_record?) { false }
29
+ allow(@model).to receive(:save) { @model.run_callbacks(:save) }
30
+ end
31
+ it "should call redis with correct params" do
32
+ expect(BetterCallRedis.configuration.redis).not_to receive(:publish)
33
+ @model.save
34
+ end
35
+ end
36
+
37
+ describe "deleting an existing record" do
38
+ before(:each) do
39
+ @model = Person.new
40
+ allow(@model).to receive(:new_record?) { false }
41
+ allow(@model).to receive(:destroy) { @model.run_callbacks(:destroy) }
42
+ end
43
+ it "should call redis with correct params" do
44
+ expect(BetterCallRedis.configuration.redis).to receive(:publish).with("test", "{\"class_name\":\"Person\",\"message\":-1}") { true }
45
+ @model.destroy
46
+ end
47
+ end
48
+
49
+ describe "logically delete an existing record" do
50
+ before(:each) do
51
+ @model = PersonLogicallyDeletable.new
52
+ allow(@model).to receive(:new_record?) { false }
53
+ allow(@model).to receive(:save) { @model.run_callbacks(:save) }
54
+ end
55
+ it "should call redis with correct params" do
56
+ expect(BetterCallRedis.configuration.redis).to receive(:publish).with("test", "{\"class_name\":\"PersonLogicallyDeletable\",\"message\":-1}") { true }
57
+ @model.deleted = true
58
+ @model.save
59
+ end
60
+ end
61
+
62
+ describe "logically undelete an existing record" do
63
+ before(:each) do
64
+ @model = PersonLogicallyDelated.new
65
+ allow(@model).to receive(:new_record?) { false }
66
+ allow(@model).to receive(:save) { @model.run_callbacks(:save) }
67
+ end
68
+ it "should call redis with correct params" do
69
+ expect(BetterCallRedis.configuration.redis).to receive(:publish).with("test", "{\"class_name\":\"PersonLogicallyDelated\",\"message\":1}") { true }
70
+ @model.deleted = false
71
+ @model.save
72
+ end
73
+ end
74
+
75
+ describe "changind a delete attribute not specified in the configuration deleted_attributes" do
76
+ before(:each) do
77
+ @model = PersonLogicallyDelatedNotChanged.new
78
+ allow(@model).to receive(:new_record?) { false }
79
+ allow(@model).to receive(:save) { @model.run_callbacks(:save) }
80
+ end
81
+ it "should call redis with correct params" do
82
+ expect(BetterCallRedis.configuration.redis).not_to receive(:publish)
83
+ @model.cancelled = false
84
+ @model.save
85
+ end
86
+ end
87
+
88
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe "BetterCallRedis Initializer" do
4
+
5
+ describe "with default options" do
6
+ let(:conf) { BetterCallRedis.configuration }
7
+ it "should have all default options" do
8
+ expect(conf.redis).to be_kind_of Redis
9
+ expect(conf.namespace). equal? "bettercallredis:notification"
10
+ expect(conf.deleted_attributes).equal? []
11
+ end
12
+ end
13
+
14
+ describe "with non default options" do
15
+ let(:conf) do
16
+ BetterCallRedis.configuration do |bcr|
17
+ bcr.redis = "Invalid Redis"
18
+ bcr.namespace = "different"
19
+ bcr.deleted_attributes = %w(deleted cancelled)
20
+ end
21
+ end
22
+ it "should have all default options" do
23
+ expect(conf.redis).equal? "Invalid Redis"
24
+ expect(conf.namespace). equal? "different"
25
+ expect(conf.deleted_attributes).equal? %w(deleted cancelled)
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,47 @@
1
+ require 'better_call_redis'
2
+ require 'active_record'
3
+ require 'active_record/serialization'
4
+ require 'fakeredis'
5
+
6
+ class Person
7
+ include ActiveRecord::Callbacks
8
+ include BetterCallRedis::ActiveRecord
9
+
10
+ def attributes
11
+ {"id" => 1, "name" => "name_person"}
12
+ end
13
+ end
14
+
15
+
16
+ class PersonLogicallyDeletable < Person
17
+ attr_accessor :deleted
18
+ attr_accessor :cancelled
19
+
20
+ def attributes
21
+ {"id" => 1, "name" => "name_person", "deleted" => false, "cancelled" => false}
22
+ end
23
+
24
+ def deleted_changed?
25
+ true
26
+ end
27
+ end
28
+
29
+ class PersonLogicallyDelated < PersonLogicallyDeletable
30
+ def attributes
31
+ {"id" => 1, "name" => "name_person", "deleted" => true, "cancelled" => true}
32
+ end
33
+
34
+ def deleted_changed?
35
+ true
36
+ end
37
+ end
38
+
39
+ class PersonLogicallyDelatedNotChanged < PersonLogicallyDeletable
40
+ def attributes
41
+ {"id" => 1, "name" => "name_person", "deleted" => true, "cancelled" => true}
42
+ end
43
+
44
+ def deleted_changed?
45
+ false
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: better_call_redis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Claudio Contin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: redis
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 3.2.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 3.2.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: fakeredis
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.5.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.5.0
111
+ description: Redis pushes a message (notification) to a channel when an active record
112
+ entry is created or deleted
113
+ email:
114
+ - contin@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - better_call_redis.gemspec
126
+ - lib/better_call_redis.rb
127
+ - lib/better_call_redis/active_record.rb
128
+ - lib/better_call_redis/configuration.rb
129
+ - lib/better_call_redis/version.rb
130
+ - spec/lib/better_call_redis/active_record_spec.rb
131
+ - spec/lib/better_call_redis_spec.rb
132
+ - spec/spec_helper.rb
133
+ homepage: https://github.com/clod81/better_call_redis
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.4.5
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Active Record Callback that pushes notification of creation or deletion to
157
+ a Redis channel
158
+ test_files:
159
+ - spec/lib/better_call_redis/active_record_spec.rb
160
+ - spec/lib/better_call_redis_spec.rb
161
+ - spec/spec_helper.rb