notably 0.0.1 → 0.1.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: bd247eba7aa38faf56a73e9eb6bb8734d22e4358
4
- data.tar.gz: 739b8d7bb0d16402b478e2c79d3c30e1d3822813
3
+ metadata.gz: ad6fa368dc10673bccb89da94dfd1a7f1c09e8ec
4
+ data.tar.gz: 75a53e5bfdb71b388b918f0145bcc9584e83e7aa
5
5
  SHA512:
6
- metadata.gz: cc524ed6ef36a6fe5f56ded1cc91b58011603426af5e9876ec6dc0fd33d35dcea1945d4d86bf6321b3a7cc61a6c8b55c03857823b892913209513b57fd8e8a4e
7
- data.tar.gz: fcb35392ccaf761b3a6a666a1752668e5114621539fc578c7dfe64ea34b40e0ea8a684c8b1248d8c5809d66eae2ce1dd6efa8c8fb418cc280a17f9d8c5c9e8d4
6
+ metadata.gz: 1b73b5376a05129b1672d78d7ade3e2d0e556ab05e608e4ef2108df9e1672c9619e818241219645e3cd532f723f4075f4236732acaeb93d22e3a6d01375e3962
7
+ data.tar.gz: f64cfcb07b6533fc0fab1f4d2028c5ed9ee022e8fb04f5fc786e60ef3970d807914c36ab1b4e2b142e710b85294b20512cf6e2388aa32f127448183435df0b4d
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ .DS_Store
1
2
  *.gem
2
3
  *.rbc
3
4
  .bundle
@@ -0,0 +1,7 @@
1
+ # 0.1.0
2
+
3
+ * Callbacks - `before_notify` and `after_notify`
4
+
5
+ # 0.0.1
6
+
7
+ Initial Release
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Notably
2
2
 
3
- Notably is a redis-backed notification system.
3
+ ![a pretty aggressive slogan](notably-slogan.png)
4
+
5
+ Notably is a redis-backed notification system that _won't_ make you sick and kill you.
4
6
 
5
7
  ## Installation
6
8
 
@@ -241,6 +243,29 @@ Or
241
243
 
242
244
  (Just a warning, even if you're setting it to something that doesn't need the receiver passed in to calculate it, you need to specify it as an argument if you're going to use lambdas. They don't like it when arguments get ignored.)
243
245
 
246
+ ## Callbacks
247
+
248
+ You can use the `before_notify` and `after_notify` class methods to specify callbacks. They work pretty much as expected taking a lambda, Proc, block, or symbol representing a method. All of these need to accept an argument for the receiver:
249
+
250
+ ```ruby
251
+ class CommentNotification
252
+ include Notably::Notification
253
+ required_attributes :comment_id, :commentable_type, :commentable_id, :author_id
254
+ group_by :commentable_type, :commentable_id
255
+ after_notify ->(receiver) { Rails.logger.info "Sent a comment notification to #{receiver.class} #{receiver.id}" }
256
+ after_notify do |receiver|
257
+ UserMailer.delay.new_comment(receiver.id, comment_id)
258
+ end
259
+ after_notify :foo
260
+
261
+ def foo(receiver)
262
+ Rails.logger.info "Foo"
263
+ end
264
+ end
265
+ ```
266
+
267
+ They're useful for sending email notifications, and probably a lot of other things, but email notifications was what we built this for. As a side note, if you are using it to send emails, you'll want to have those emails go through some kind of worker queue so you don't massivly slow down notifications. We recommend [sidekiq](https://github.com/mperham/sidekiq), because it runs on Redis, has a nice dashboard, and has a clean API.
268
+
244
269
 
245
270
  ## Contributing
246
271
 
@@ -1,12 +1,10 @@
1
1
  module Notably
2
2
  class Configuration
3
- attr_writer :redis
3
+ attr_accessor :redis, :touch_receivers
4
4
 
5
5
  def initialize
6
- end
7
-
8
- def redis
9
- @redis ||= Redis.current
6
+ @redis = Redis.current
7
+ @touch_receivers = true
10
8
  end
11
9
  end
12
10
  end
@@ -50,20 +50,26 @@ module Notably
50
50
  def save
51
51
  receivers.each do |receiver|
52
52
  # look for groupable messages within group_within
53
- # group_within = self.class.group_within.arity == 1 ? self.class.group_within.call(user) : self.class.group_within.call
54
- group_within = self.class.group_within.call(receiver)
55
- groupable_notifications = receiver.notifications_since(group_within)
56
- groupable_notifications.select! { |notification| notification[:data].slice(*self.class.group_by) == data.slice(*self.class.group_by) }
57
- groupable_notifications.each do |notification|
58
- @groups += notification[:groups]
53
+ if self.class.group?
54
+ group_within = self.class.group_within.call(receiver)
55
+ groupable_notifications = receiver.notifications_since(group_within)
56
+ groupable_notifications.select! { |notification| notification[:data].slice(*self.class.group_by) == data.slice(*self.class.group_by) }
57
+ groupable_notifications.each do |notification|
58
+ @groups += notification[:groups]
59
+ end
59
60
  end
61
+ run_callbacks(:before_notify, receiver)
60
62
  Notably.config.redis.pipelined do
61
63
  Notably.config.redis.zadd(receiver.send(:notification_key), created_at.to_i, marshal)
62
- groupable_notifications.each do |notification|
63
- Notably.config.redis.zrem(receiver.send(:notification_key), Marshal.dump(notification))
64
- @groups -= notification[:groups]
64
+ receiver.touch if Notably.config.touch_receivers
65
+ if self.class.group?
66
+ groupable_notifications.each do |notification|
67
+ Notably.config.redis.zrem(receiver.send(:notification_key), Marshal.dump(notification))
68
+ @groups -= notification[:groups]
69
+ end
65
70
  end
66
71
  end
72
+ run_callbacks(:after_notify, receiver)
67
73
  end
68
74
  end
69
75
 
@@ -100,36 +106,71 @@ module Notably
100
106
  end
101
107
  end
102
108
 
109
+ private
110
+
111
+ def run_callbacks(type, *args)
112
+ self.class.callbacks[type].each do |callback|
113
+ if callback.is_a? Symbol
114
+ # callback.to_proc.call(self, *args)
115
+ self.send(callback, *args)
116
+ else
117
+ self.instance_exec(*args, &callback)
118
+ end
119
+ end
120
+ end
121
+
103
122
  module ClassMethods
123
+ attr_reader :callbacks
124
+
125
+ def self.extended(base)
126
+ base.class_eval do
127
+ @callbacks = {after_notify: [], before_notify: []}
128
+ @group_by = []
129
+ @group_within = ->(receiver) { receiver.last_notification_read_at }
130
+ @required_attributes = []
131
+ end
132
+ end
133
+
104
134
  def create(attributes={})
105
135
  new(attributes).save
106
136
  end
107
137
 
108
138
  def required_attributes(*args)
109
139
  if args.any?
110
- @required_attributes ||= []
111
140
  @required_attributes += args
112
141
  else
113
- @required_attributes ||= []
142
+ @required_attributes
114
143
  end
115
144
  end
116
145
 
117
146
  def group_by(*args)
118
147
  if args.any?
119
- @group_by ||= []
120
148
  @group_by += args
121
149
  else
122
- @group_by ||= []
150
+ @group_by
123
151
  end
124
152
  end
125
153
 
154
+ def group?
155
+ @group_by.any?
156
+ end
157
+
126
158
  def group_within(block=nil)
127
159
  if block
128
160
  @group_within = block
129
161
  else
130
- @group_within ||= ->(receiver) { receiver.last_notification_read_at }
162
+ @group_within
131
163
  end
132
164
  end
165
+
166
+ def before_notify(method=nil, &block)
167
+ @callbacks[:before_notify] << (block || method)
168
+ end
169
+
170
+ def after_notify(method=nil, &block)
171
+ @callbacks[:after_notify] << (block || method)
172
+ end
173
+
133
174
  end
134
175
 
135
176
  end
@@ -1,3 +1,3 @@
1
1
  module Notably
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notably
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Will Cosgrove
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-16 00:00:00.000000000 Z
11
+ date: 2013-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -46,6 +46,7 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - .gitignore
49
+ - CHANGELOG.md
49
50
  - Gemfile
50
51
  - LICENSE.txt
51
52
  - README.md
@@ -55,6 +56,7 @@ files:
55
56
  - lib/notably/notifiable.rb
56
57
  - lib/notably/notification.rb
57
58
  - lib/notably/version.rb
59
+ - notably-slogan.png
58
60
  - notably.gemspec
59
61
  homepage: ''
60
62
  licenses: