redistry 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2011 Brian Smith
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
+
data/README.md ADDED
@@ -0,0 +1,6 @@
1
+ Redistry
2
+ ========
3
+
4
+ Some useful Redis patterns for Ruby (and Rails)
5
+
6
+
data/lib/redistry.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'redis'
2
+ require 'redistry/version'
3
+ require 'redistry/serializers/activerecord'
4
+ require 'redistry/has_list'
5
+
6
+ module Redistry
7
+ extend self
8
+
9
+ attr_accessor :client, :serializer
10
+
11
+ def client
12
+ @client ||= Redis.new
13
+ end
14
+
15
+ def serializer
16
+ @serializer ||= Redistry::Serializers::ActiveRecord.new
17
+ end
18
+ end
19
+
20
+ ActiveRecord::Base.send(:include, Redistry::HasList) if defined?(ActiveRecord)
@@ -0,0 +1,23 @@
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
+ instance_variable_set("@_#{name}_has_list".to_sym, CollectionProxy.new(self, name, options))
14
+
15
+ class_eval <<-EOF
16
+ def self.#{name}
17
+ @_#{name}_has_list
18
+ end
19
+ EOF
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,67 @@
1
+ module Redistry
2
+ module HasList
3
+ class CollectionProxy
4
+ include Enumerable
5
+
6
+ attr_accessor :klass, :collection_name, :options
7
+
8
+ def initialize(klass, collection_name, options = {})
9
+ self.klass = klass
10
+ self.collection_name = collection_name
11
+ self.options = default_options.merge(options)
12
+ end
13
+
14
+ def add(*objs)
15
+ serialized_objs = Redistry.serializer.serialize(klass, objs.flatten)
16
+ serialized_objs.each do |val|
17
+ Redistry.client.lpush(key, val)
18
+ end
19
+
20
+ if options[:size]
21
+ Redistry.client.ltrim(key,0,options[:size]-1)
22
+ end
23
+ self
24
+ end
25
+
26
+ def clear
27
+ Redistry.client.del(key)
28
+ self
29
+ end
30
+
31
+ def reload
32
+ load_collection
33
+ self
34
+ end
35
+
36
+ def each
37
+ to_a.each do |v|
38
+ yield v
39
+ end
40
+ end
41
+
42
+ def key
43
+ @key ||= "#{klass.name}-#{collection_name.to_s}"
44
+ end
45
+
46
+ def to_ary
47
+ @collection ||= load_collection
48
+ end
49
+ alias_method :to_a, :to_ary
50
+
51
+ private
52
+
53
+ def default_options
54
+ {
55
+ :size => nil
56
+ }
57
+ end
58
+
59
+ def load_collection
60
+ @collection = begin
61
+ serialized_objs = Redistry.client.lrange(key,0,-1)
62
+ Redistry.serializer.deserialize(klass, serialized_objs)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,13 @@
1
+ module Redistry
2
+ module Serializers
3
+ class ActiveRecord
4
+ def serialize(klass, *objs)
5
+ objs.flatten.map(&:id)
6
+ end
7
+
8
+ def deserialize(klass, *objs)
9
+ klass.find(objs.flatten)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Redistry
2
+
3
+ VERSION = "0.1.0"
4
+
5
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redistry
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Brian Smith
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-07-26 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: redis
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 2
31
+ - 1
32
+ version: 2.2.1
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ description: A set of useful Redis patterns/abstractions for Ruby (and Rails)
49
+ email:
50
+ - bsmith@swig505.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - lib/redistry/has_list/collection_proxy.rb
59
+ - lib/redistry/has_list.rb
60
+ - lib/redistry/serializers/activerecord.rb
61
+ - lib/redistry/version.rb
62
+ - lib/redistry.rb
63
+ - LICENSE
64
+ - README.md
65
+ has_rdoc: true
66
+ homepage: http://github.com/Lytol/redistry
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options: []
71
+
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 1
89
+ - 3
90
+ - 6
91
+ version: 1.3.6
92
+ requirements: []
93
+
94
+ rubyforge_project: redistry
95
+ rubygems_version: 1.3.7
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: A set of useful Redis patterns/abstractions for Ruby (and Rails)
99
+ test_files: []
100
+