redpear 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/redpear/model.rb CHANGED
@@ -58,16 +58,23 @@ class Redpear::Model < Hash
58
58
  self["id"] = value.to_s
59
59
  end
60
60
 
61
- # Custom comparator
62
- # @return [Boolean]
61
+ # Custom comparator, inspired by ActiveRecord::Base#==
62
+ # @param [Object] other the comparison object
63
+ # @return [Boolean] true, if +other+ is persisted and ID
63
64
  def ==(other)
64
65
  case other
65
66
  when Redpear::Model
66
- other.instance_of?(self.class) && to_hash(true) == other.to_hash(true)
67
+ other.instance_of?(self.class) && persisted? && other.id == id
67
68
  else
68
69
  super
69
70
  end
70
71
  end
72
+ alias :eql? :==
73
+
74
+ # Use ID as base for hash
75
+ def hash
76
+ id.hash
77
+ end
71
78
 
72
79
  # Attribute reader with type-casting
73
80
  # @return [Object]
@@ -9,8 +9,8 @@
9
9
  # # => ['comments:1', 'comments:+', 'comments:*', 'comments:post_id:2']
10
10
  #
11
11
  # # Instance nesting
12
- # instance.nest # => 'comments:1'
13
- # instance.nest.mapped_hmget_all # => { "post_id" => "2" }
12
+ # instance.nest # => 'comments:1'
13
+ # instance.nest.hgetall # => { "post_id" => "2" }
14
14
  #
15
15
  # # Member nesting
16
16
  # Comment.mb_nest # "comments:[~]"
data/lib/redpear/nest.rb CHANGED
@@ -1,13 +1,90 @@
1
- class Redpear::Nest < ::Nest
1
+ # Nested redis key-value store, with master slave support
2
+ # Heavily "inspired" by the nest library
3
+ # Original copyright: Michel Martens & Damian Janowski
4
+ class Redpear::Nest < ::String
2
5
 
3
- [:mapped_hmget, :mapped_hmset].each do |meth|
6
+ MASTER_METHODS = %w|
7
+ append auth
8
+ bgrewriteaof bgsave blpop brpop brpoplpush
9
+ config
10
+ decr decrby del discard
11
+ exec expire expireat
12
+ flushall flushdb getset
13
+ hset hsetnx hincrby hmset hdel
14
+ incr incrby
15
+ linsert lpop lpush lpushx lrem lset ltrim
16
+ mapped_hmset mapped_mset mapped_msetnx
17
+ move mset msetnx multi
18
+ persist pipelined psubscribe punsubscribe quit
19
+ rename renamenx rpop rpoplpush rpush rpushx
20
+ sadd save sdiffstore set setbit
21
+ setex setnx setrange sinterstore
22
+ shutdown smove spop srem subscribe
23
+ sunionstore sync synchronize
24
+ unsubscribe unwatch watch
25
+ zadd zincrby zinterstore zrem
26
+ zremrangebyrank zremrangebyscore zunionstore
27
+ |.freeze
28
+
29
+ SLAVE_METHODS = %w|
30
+ dbsize debug get getbit getrange
31
+ echo exists
32
+ hget hmget hexists hlen hkeys hvals hgetall
33
+ info keys lastsave lindex llen lrange
34
+ mapped_hmget mapped_mget mget monitor
35
+ object ping publish randomkey
36
+ scard sdiff select sinter sismember slaveof
37
+ smembers sort srandmember strlen substr sunion
38
+ ttl type
39
+ zcard zcount zrange zrangebyscore zrank
40
+ zrevrange zrevrangebyscore zrevrank zscore
41
+ |.freeze
42
+
43
+ attr_reader :master, :slave, :current
44
+
45
+ # Constructor
46
+ # @param [String] key
47
+ # The redis key
48
+ # @param [Redis::Client|Redis::Namespace|ConnectionPool] master
49
+ # The master connection, optional, defaults to the current connection
50
+ # @param [Redis::Client|Redis::Namespace|ConnectionPool] slave
51
+ # The slave connection, optional, defaults to master
52
+ def initialize(key, master = Redis.current, slave = nil)
53
+ super(key)
54
+ @master = master
55
+ @slave = slave || master
56
+ end
57
+
58
+ # @param [multiple] keys
59
+ # Nest within these keys
60
+ # @return [Redpear::Nest]
61
+ # The nested key
62
+ def [](*keys)
63
+ self.class.new [self, *keys].join(':'), master, slave
64
+ end
65
+
66
+ # @param [Symbol] name
67
+ # Either :master or :slave
68
+ # @yield
69
+ # Perform a block with the given connection
70
+ def with_connection(name)
71
+ @current = send(name)
72
+ yield
73
+ ensure
74
+ @current = nil
75
+ end
76
+ alias_method :with, :with_connection
77
+
78
+ MASTER_METHODS.each do |meth|
4
79
  define_method(meth) do |*args, &block|
5
- redis.send(meth, self, *args, &block)
80
+ (current || master).send(meth, self, *args, &block)
6
81
  end
7
82
  end
8
83
 
9
- def mapped_hmget_all
10
- mapped_hmget *hkeys
84
+ SLAVE_METHODS.each do |meth|
85
+ define_method(meth) do |*args, &block|
86
+ (current || slave).send(meth, self, *args, &block)
87
+ end
11
88
  end
12
89
 
13
90
  end
@@ -52,7 +52,7 @@ module Redpear::Persistence
52
52
 
53
53
  # Load attributes from DB (destructive)
54
54
  def refresh_attributes
55
- update nest.mapped_hmget(*__loadable_attributes__) if persisted?
55
+ update nest.hgetall if persisted?
56
56
  @__loaded__ = true
57
57
  self
58
58
  end
@@ -130,11 +130,6 @@ module Redpear::Persistence
130
130
  result
131
131
  end
132
132
 
133
- # Attributes that can be loaded
134
- def __loadable_attributes__
135
- self.class.columns.names
136
- end
137
-
138
133
  def __persistable_value__(value)
139
134
  case value
140
135
  when Time
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redpear
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-01 00:00:00.000000000Z
12
+ date: 2011-12-24 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
16
- requirement: &19999180 !ruby/object:Gem::Requirement
16
+ requirement: &19913160 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *19999180
24
+ version_requirements: *19913160
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: nest
27
- requirement: &19998660 !ruby/object:Gem::Requirement
27
+ requirement: &19933600 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.1.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *19998660
35
+ version_requirements: *19933600
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &19998180 !ruby/object:Gem::Requirement
38
+ requirement: &19933220 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *19998180
46
+ version_requirements: *19933220
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &19997620 !ruby/object:Gem::Requirement
49
+ requirement: &19932760 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *19997620
57
+ version_requirements: *19932760
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rspec
60
- requirement: &19997060 !ruby/object:Gem::Requirement
60
+ requirement: &19932340 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *19997060
68
+ version_requirements: *19932340
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: fakeredis
71
- requirement: &19996200 !ruby/object:Gem::Requirement
71
+ requirement: &19931920 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *19996200
79
+ version_requirements: *19931920
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: shoulda-matchers
82
- requirement: &19995740 !ruby/object:Gem::Requirement
82
+ requirement: &19931500 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,32 +87,32 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *19995740
90
+ version_requirements: *19931500
91
91
  description: Simple, elegant & efficient ORM for Redis
92
92
  email: dimitrij@blacksquaremedia.com
93
93
  executables: []
94
94
  extensions: []
95
95
  extra_rdoc_files: []
96
96
  files:
97
- - lib/redpear.rb
98
- - lib/redpear/connection.rb
99
- - lib/redpear/schema/collection.rb
100
- - lib/redpear/core_ext/stringify_keys.rb
101
- - lib/redpear/concern.rb
102
97
  - lib/redpear/zmembers.rb
98
+ - lib/redpear/column.rb
103
99
  - lib/redpear/namespace.rb
100
+ - lib/redpear/connection.rb
101
+ - lib/redpear/persistence.rb
102
+ - lib/redpear/schema.rb
103
+ - lib/redpear/index.rb
104
+ - lib/redpear/concern.rb
104
105
  - lib/redpear/nest.rb
105
106
  - lib/redpear/finders.rb
107
+ - lib/redpear/model.rb
106
108
  - lib/redpear/zindex.rb
107
- - lib/redpear/persistence.rb
109
+ - lib/redpear/schema/collection.rb
110
+ - lib/redpear/core_ext/stringify_keys.rb
111
+ - lib/redpear/machinist.rb
112
+ - lib/redpear/counters.rb
108
113
  - lib/redpear/expiration.rb
109
- - lib/redpear/column.rb
110
- - lib/redpear/model.rb
111
- - lib/redpear/index.rb
112
114
  - lib/redpear/members.rb
113
- - lib/redpear/schema.rb
114
- - lib/redpear/counters.rb
115
- - lib/redpear/machinist.rb
115
+ - lib/redpear.rb
116
116
  homepage: https://github.com/bsm/redpear
117
117
  licenses: []
118
118
  post_install_message: