redistry 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,11 +1,67 @@
1
- Redistry
2
- ========
1
+ Getting Started
2
+ ---------------
3
3
 
4
- Some useful Redis patterns for Ruby (and Rails)
4
+ ### Rails 3
5
5
 
6
+ Add `redistry` to your `Gemfile`
6
7
 
7
- TODO
8
- ----
8
+ gem "redistry"
9
+
10
+
11
+ #### Using has_list
12
+
13
+ Associate a Redis list with each ActiveRecord item:
14
+
15
+ class Notification < ActiveRecord::Base
16
+
17
+ end
18
+
19
+ ...
20
+
21
+ class User < ActiveRecord::Base
22
+ has_list :recent_notifications, :class => Notification, :size => 10
23
+
24
+ ...
25
+ end
26
+
27
+ You can add items to the list with `add`:
28
+
29
+ user.recent_notifications.add(notification)
30
+
31
+ Since you specified `:size`, Redistry will treat the list as a queue (first in, last out) with a
32
+ maximum of `size` elements. If you don't specify `:size`, the list will not limit the size at all.
33
+
34
+ You can fetch the items with `all`:
35
+
36
+ @notifications = user.recent_notifications.all
37
+
38
+ And you can clear all the notifications with `clear`:
39
+
40
+ user.recent_notifications.clear
41
+
42
+
43
+ #### Using list
44
+
45
+ Sometimes, you want to associate a list generically with an entire class (or a module)
46
+
47
+ class User
48
+ list :popular, :size => 10
49
+ ...
50
+ end
51
+
52
+ Note that since we didn't specify `:class`, Redistry will assume the objects are of the same type as the outer class (in this case, `User`).
53
+
54
+ Use the same interface as `has_list` to access the items:
55
+
56
+ User.popular.add(@user)
57
+ @popular_users = User.popular.all
58
+ User.popular.clear
59
+
60
+
61
+ Known Issues / TODO
62
+ -------------------
9
63
 
10
64
  * JSON serializer does not work with strings... not sure how to deal since this technically is not valid json
11
- * Allow :class as option on has_list rather than always autodetecting
65
+
66
+
67
+ Copyright (c) 2011 Brian Smith (bsmith@swig505.com)
@@ -1,5 +1,5 @@
1
1
  module Redistry
2
- module HasList
2
+ module List
3
3
  class CollectionProxy
4
4
  attr_accessor :klass, :collection_name, :options
5
5
 
@@ -0,0 +1,35 @@
1
+ require 'redistry/list/collection_proxy'
2
+
3
+ module Redistry
4
+ module List
5
+
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ def has_list(name, options = {})
13
+ klass = options.delete(:class) || self
14
+
15
+ raise("The class `#{klass.name}` must have an unique `id` to use `has_list`") unless klass.instance_methods.include?(:id)
16
+
17
+ define_method name do
18
+ return instance_variable_get("@_#{name}_has_list") if instance_variable_get("@_#{name}_has_list")
19
+ instance_variable_set("@_#{name}_has_list", CollectionProxy.new(klass, "#{name}-#{id}", options))
20
+ end
21
+ end
22
+
23
+ def list(name, options = {})
24
+ klass = options.delete(:class) || self
25
+
26
+ instance_variable_set("@_#{name}_list".to_sym, CollectionProxy.new(klass, name, options))
27
+ class_eval <<-EOF
28
+ def self.#{name}
29
+ @_#{name}_list
30
+ end
31
+ EOF
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,5 +1,5 @@
1
1
  module Redistry
2
2
 
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
 
5
5
  end
data/lib/redistry.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'redis'
2
2
  require 'redistry/version'
3
- require 'redistry/has_list'
3
+ require 'redistry/list'
4
4
  require 'redistry/serializers/json'
5
5
 
6
6
  module Redistry
@@ -20,7 +20,7 @@ module Redistry
20
20
 
21
21
  def setup_active_record!
22
22
  require 'redistry/serializers/activerecord'
23
- ActiveRecord::Base.send(:include, Redistry::HasList)
23
+ ActiveRecord::Base.send(:include, Redistry::List)
24
24
  @loaded_frameworks << :activerecord
25
25
  end
26
26
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brian Smith
@@ -81,8 +81,8 @@ extensions: []
81
81
  extra_rdoc_files: []
82
82
 
83
83
  files:
84
- - lib/redistry/has_list/collection_proxy.rb
85
- - lib/redistry/has_list.rb
84
+ - lib/redistry/list/collection_proxy.rb
85
+ - lib/redistry/list.rb
86
86
  - lib/redistry/serializers/activerecord.rb
87
87
  - lib/redistry/serializers/json.rb
88
88
  - lib/redistry/version.rb
@@ -1,24 +0,0 @@
1
- require 'redistry/has_list/collection_proxy'
2
-
3
- module Redistry
4
- module HasList
5
-
6
- def self.included(base)
7
- base.extend(ClassMethods)
8
- end
9
-
10
- module ClassMethods
11
-
12
- def has_list(name, options = {})
13
- klass = options.delete(:class) || self
14
- instance_variable_set("@_#{name}_has_list".to_sym, CollectionProxy.new(klass, name, options))
15
-
16
- class_eval <<-EOF
17
- def self.#{name}
18
- @_#{name}_has_list
19
- end
20
- EOF
21
- end
22
- end
23
- end
24
- end