redpear 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -57,8 +57,7 @@ class Redpear::Connection
57
57
  # The (optional) slave connection, defaults to master
58
58
  def initialize(master = Redis.current, slave = nil)
59
59
  @master = _connect(master)
60
- @slave = _connect(slave || master)
61
- @transaction = nil
60
+ @slave = slave ? _connect(slave) : @master
62
61
  end
63
62
 
64
63
  # @param [Symbol] name
@@ -74,20 +73,6 @@ class Redpear::Connection
74
73
  @current = nil
75
74
  end
76
75
 
77
- # Run a transaction, prevents accidental transaction nesting
78
- def transaction(&block)
79
- if @transaction
80
- yield
81
- else
82
- begin
83
- @transaction = true
84
- multi(&block)
85
- ensure
86
- @transaction = nil
87
- end
88
- end
89
- end
90
-
91
76
  MASTER_METHODS.each do |meth|
92
77
  define_method(meth) do |*a, &b|
93
78
  (current || master).send(meth, *a, &b)
data/lib/redpear/model.rb CHANGED
@@ -32,15 +32,15 @@ class Redpear::Model < Hash
32
32
 
33
33
  alias_method :create, :new
34
34
 
35
- # @param [Redpear::Connection] define a custom connection
35
+ # @param [Redis] define a custom connection
36
36
  attr_writer :connection
37
37
 
38
38
  # @param [String] define a custom scope
39
39
  attr_writer :scope
40
40
 
41
- # @return [Redpear::Connection] the connection
41
+ # @return [Redis] the connection
42
42
  def connection
43
- @connection ||= (superclass.respond_to?(:connection) ? superclass.connection : Redpear::Connection.new)
43
+ @connection ||= (superclass.respond_to?(:connection) ? superclass.connection : Redis.current)
44
44
  end
45
45
 
46
46
  # @return [String] the scope of this model. Example:
@@ -69,10 +69,16 @@ class Redpear::Model < Hash
69
69
  @_pk_counter ||= Redpear::Store::Counter.new nested_key(:+), connection
70
70
  end
71
71
 
72
- # Runs a bulk-operation.
72
+ # Runs a transactional bulk-operation.
73
73
  # @yield operations that should be run in the transaction
74
74
  def transaction(&block)
75
- connection.transaction(&block)
75
+ connection.multi(&block)
76
+ end
77
+
78
+ # Runs a bulk-operation.
79
+ # @yield operations that should be run as bulk
80
+ def pipelined(&block)
81
+ connection.pipelined(&block)
76
82
  end
77
83
 
78
84
  # Destroys a record. Example:
@@ -2,12 +2,20 @@ require 'securerandom'
2
2
 
3
3
  class Redpear::Store::Base
4
4
 
5
+ # Transforamtions
6
+ IS_NIL = lambda {|v| v.nil? }.freeze
7
+ IS_ZERO = lambda {|v| v.zero? }.freeze
8
+ IS_TRUE = lambda {|v| !!v }.freeze
9
+ TO_INT = lambda {|v| v.to_i }.freeze
10
+ TO_SET = lambda {|v| v.to_set }.freeze
11
+ PICK_FIRST = lambda {|v| v.first }.freeze
12
+
5
13
  attr_reader :key, :conn
6
14
 
7
15
  # Creates and yields over a temporary key.
8
16
  # Useful in combination with e.g. `interstore`, `unionstore`, etc.
9
17
  #
10
- # @param [Redpear::Connection] conn
18
+ # @param [Redis] conn
11
19
  # The connection
12
20
  # @param [Hash] options
13
21
  # The options hash
@@ -32,7 +40,7 @@ class Redpear::Store::Base
32
40
  # Constructor
33
41
  # @param [String] key
34
42
  # The storage key
35
- # @param [Redpear::Connection] conn
43
+ # @param [Redis] conn
36
44
  # The connection
37
45
  def initialize(key, conn)
38
46
  @key, @conn = key, conn
@@ -126,4 +134,4 @@ class Redpear::Store::Base
126
134
  end
127
135
  end
128
136
 
129
- end
137
+ end
@@ -1,8 +1,13 @@
1
1
  class Redpear::Store::Counter < Redpear::Store::Value
2
-
2
+
3
3
  # @return [Integer] the value
4
4
  def get
5
- super.to_i
5
+ case value = super
6
+ when Redis::Future
7
+ value.instance_eval { @transformation = TO_INT }
8
+ else
9
+ value.to_i
10
+ end
6
11
  end
7
12
 
8
13
  # Sets the value
@@ -41,4 +46,4 @@ class Redpear::Store::Counter < Redpear::Store::Value
41
46
  alias_method :previous, :decrement
42
47
  alias_method :prev, :decrement
43
48
 
44
- end
49
+ end
@@ -5,4 +5,16 @@ class Redpear::Store::Enumerable < Redpear::Store::Base
5
5
  # @see Redpear::Store::Base#value
6
6
  alias_method :value, :to_a
7
7
 
8
- end
8
+ # Alias for #length
9
+ # @return [Integer] the length
10
+ def size
11
+ length
12
+ end
13
+
14
+ # Alias for #length
15
+ # @return [Integer] the length
16
+ def count
17
+ length
18
+ end
19
+
20
+ end
@@ -30,7 +30,12 @@ class Redpear::Store::Hash < Redpear::Store::Enumerable
30
30
 
31
31
  # @return [Boolean] true, if empty
32
32
  def empty?
33
- length.zero?
33
+ case value = length
34
+ when Redis::Future
35
+ value.instance_eval { @transformation = IS_ZERO }
36
+ else
37
+ value.zero?
38
+ end
34
39
  end
35
40
 
36
41
  # @param [String] field
@@ -70,7 +75,6 @@ class Redpear::Store::Hash < Redpear::Store::Enumerable
70
75
  def length
71
76
  conn.hlen key
72
77
  end
73
- alias_method :size, :length
74
78
 
75
79
  # @param [String] field
76
80
  # The field to increment
@@ -97,9 +101,13 @@ class Redpear::Store::Hash < Redpear::Store::Enumerable
97
101
 
98
102
  # @param [Hash] hash
99
103
  # The pairs to update
100
- def update(hash)
101
- merge!(hash)
102
- to_hash
104
+ def update(hash)
105
+ case result = merge!(hash)
106
+ when Redis::Future
107
+ result
108
+ else
109
+ to_hash
110
+ end
103
111
  end
104
112
 
105
113
  # @param [Hash] hash
@@ -117,4 +125,4 @@ class Redpear::Store::Hash < Redpear::Store::Enumerable
117
125
  other.respond_to?(:to_hash) && other.to_hash == to_hash
118
126
  end
119
127
 
120
- end
128
+ end
@@ -45,13 +45,18 @@ class Redpear::Store::List < Redpear::Store::Enumerable
45
45
  # Destructive slice. Returns specified range and removes other items.
46
46
  # @overload slice(start, length)
47
47
  def slice!(start, length = nil)
48
- case start
48
+ result = case start
49
49
  when Range, Array
50
50
  trim *range_pair(start)
51
51
  else
52
52
  trim(start, start + length - 1)
53
53
  end
54
- to_a
54
+ case result
55
+ when Redis::Future
56
+ result
57
+ else
58
+ to_a
59
+ end
55
60
  end
56
61
 
57
62
  # @param [Integer] start
@@ -71,7 +76,6 @@ class Redpear::Store::List < Redpear::Store::Enumerable
71
76
  def length
72
77
  conn.llen key
73
78
  end
74
- alias_method :size, :length
75
79
 
76
80
  # Appends a single item. Chainable example:
77
81
  # list << 'a' << 'b'
@@ -141,4 +145,4 @@ class Redpear::Store::List < Redpear::Store::Enumerable
141
145
  conn.linsert key, :after, pivot, item
142
146
  end
143
147
 
144
- end
148
+ end
@@ -10,14 +10,19 @@ class Redpear::Store::Set < Redpear::Store::Enumerable
10
10
 
11
11
  # @return [Set] all members
12
12
  def all
13
- members.to_set
13
+ case value = members
14
+ when Redis::Future
15
+ value.instance_eval { @transformation = TO_SET }
16
+ else
17
+ value.to_set
18
+ end
14
19
  end
15
20
  alias_method :to_set, :all
16
21
  alias_method :value, :all
17
22
 
18
23
  # @return [Array] the array of members
19
24
  def members
20
- conn.smembers(key) || []
25
+ conn.smembers(key)
21
26
  end
22
27
  alias_method :to_a, :members
23
28
 
@@ -25,7 +30,6 @@ class Redpear::Store::Set < Redpear::Store::Enumerable
25
30
  def length
26
31
  conn.scard key
27
32
  end
28
- alias_method :size, :length
29
33
 
30
34
  # Adds a single value. Chainable example:
31
35
  # set << 'a' << 'b'
@@ -52,7 +56,12 @@ class Redpear::Store::Set < Redpear::Store::Enumerable
52
56
 
53
57
  # @return [Boolean] true, if empty
54
58
  def empty?
55
- length.zero?
59
+ case value = length
60
+ when Redis::Future
61
+ value.instance_eval { @transformation = IS_ZERO }
62
+ else
63
+ value.zero?
64
+ end
56
65
  end
57
66
 
58
67
  # Removes a random value
@@ -144,4 +153,4 @@ class Redpear::Store::Set < Redpear::Store::Enumerable
144
153
  conn.smove key, target.to_s, value
145
154
  end
146
155
 
147
- end
156
+ end
@@ -21,7 +21,6 @@ class Redpear::Store::SortedSet < Redpear::Store::Enumerable
21
21
  def length
22
22
  conn.zcard key
23
23
  end
24
- alias_method :size, :length
25
24
 
26
25
  # Returns items from range
27
26
  # @param [Range] range
@@ -45,8 +44,7 @@ class Redpear::Store::SortedSet < Redpear::Store::Enumerable
45
44
  # @param [String] member
46
45
  # @return [Integer] the score for the given `member`
47
46
  def score(member)
48
- number = conn.zscore(key, member)
49
- number.to_f if number
47
+ conn.zscore(key, member)
50
48
  end
51
49
  alias_method :[], :score
52
50
 
@@ -54,8 +52,7 @@ class Redpear::Store::SortedSet < Redpear::Store::Enumerable
54
52
  # @param [String] member
55
53
  # @return [Integer] the index for the given `member`
56
54
  def index(member)
57
- number = conn.zrank(key, member)
58
- number.to_i if number
55
+ conn.zrank(key, member)
59
56
  end
60
57
  alias_method :rank, :index
61
58
 
@@ -63,8 +60,7 @@ class Redpear::Store::SortedSet < Redpear::Store::Enumerable
63
60
  # @param [String] member
64
61
  # @return [Integer] the index for the given `member`
65
62
  def rindex(member)
66
- number = conn.zrevrank(key, member)
67
- number.to_i if number
63
+ conn.zrevrank(key, member)
68
64
  end
69
65
  alias_method :rrank, :rindex
70
66
 
@@ -76,13 +72,23 @@ class Redpear::Store::SortedSet < Redpear::Store::Enumerable
76
72
 
77
73
  # @return [Boolean] true, if member is included
78
74
  def include?(member)
79
- !!conn.zscore(key, member)
75
+ case value = conn.zscore(key, member)
76
+ when Redis::Future
77
+ value.instance_eval { @transformation = IS_TRUE }
78
+ else
79
+ !!value
80
+ end
80
81
  end
81
82
  alias_method :member?, :include?
82
83
 
83
84
  # @return [Boolean] true, if empty
84
85
  def empty?
85
- length.zero?
86
+ case value = length
87
+ when Redis::Future
88
+ value.instance_eval { @transformation = IS_ZERO }
89
+ else
90
+ value.zero?
91
+ end
86
92
  end
87
93
 
88
94
  # Returns a slice of members between index +range+, with the lower index returned first
@@ -153,7 +159,12 @@ class Redpear::Store::SortedSet < Redpear::Store::Enumerable
153
159
  # Return with scores, defaults to true
154
160
  # @return [Array] member + score for given `index`.
155
161
  def at(index, options = {})
156
- slice(index..index, options).first
162
+ case value = slice(index..index, options)
163
+ when Redis::Future
164
+ value.instance_eval { @transformation = PICK_FIRST }
165
+ else
166
+ value.first
167
+ end
157
168
  end
158
169
 
159
170
  # @return [String] member with the lowest index
@@ -218,14 +229,16 @@ class Redpear::Store::SortedSet < Redpear::Store::Enumerable
218
229
  # The member to increment
219
230
  # @param [Integer] by
220
231
  # The increment, defaults to 1
232
+ # @return [Float] the new value
221
233
  def increment(member, by = 1)
222
- conn.zincrby(key, by, member).to_f
234
+ conn.zincrby(key, by, member)
223
235
  end
224
236
 
225
237
  # @param [String] member
226
238
  # The member to decrement
227
239
  # @param [Integer] by
228
240
  # The decrement, defaults to 1
241
+ # @return [Float] the new value
229
242
  def decrement(member, by = 1)
230
243
  increment member, -by
231
244
  end
@@ -238,4 +251,4 @@ class Redpear::Store::SortedSet < Redpear::Store::Enumerable
238
251
  conn.send method, key, start, finish, options
239
252
  end
240
253
 
241
- end
254
+ end
@@ -43,12 +43,22 @@ class Redpear::Store::Value < Redpear::Store::Base
43
43
  # @param [String] other
44
44
  # @return [Boolean] true, if equals `other`
45
45
  def ==(other)
46
- value == other
46
+ case value = self.value
47
+ when Redis::Future
48
+ value.instance_eval { @transformation = Kernel.lambda {|v| v == other } }
49
+ else
50
+ value == other
51
+ end
47
52
  end
48
53
 
49
54
  # @return [Boolean] true, if value is nil
50
55
  def nil?
51
- value.nil?
56
+ case value = self.value
57
+ when Redis::Future
58
+ value.instance_eval { @transformation = IS_NIL }
59
+ else
60
+ value.nil?
61
+ end
52
62
  end
53
63
 
54
64
  # @return [Boolean] true, if responds to `method`
@@ -63,4 +73,4 @@ class Redpear::Store::Value < Redpear::Store::Base
63
73
  base.respond_to?(method) ? base.send(method, *a, &b) : super
64
74
  end
65
75
 
66
- end
76
+ end
@@ -1,7 +1,7 @@
1
1
  module Redpear
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 8
4
+ MINOR = 9
5
5
  TINY = 0
6
6
  PRE = nil
7
7
 
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.8.0
4
+ version: 0.9.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-29 00:00:00.000000000 Z
12
+ date: 2012-07-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
@@ -123,52 +123,36 @@ dependencies:
123
123
  - - ! '>='
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0'
126
- - !ruby/object:Gem::Dependency
127
- name: hiredis
128
- requirement: !ruby/object:Gem::Requirement
129
- none: false
130
- requirements:
131
- - - ! '>='
132
- - !ruby/object:Gem::Version
133
- version: '0'
134
- type: :development
135
- prerelease: false
136
- version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
- requirements:
139
- - - ! '>='
140
- - !ruby/object:Gem::Version
141
- version: '0'
142
126
  description: Simple, elegant & efficient ORM for Redis
143
127
  email: dimitrij@blacksquaremedia.com
144
128
  executables: []
145
129
  extensions: []
146
130
  extra_rdoc_files: []
147
131
  files:
148
- - lib/redpear.rb
149
132
  - lib/redpear/connection.rb
133
+ - lib/redpear/schema.rb
134
+ - lib/redpear/store/lock.rb
135
+ - lib/redpear/store/set.rb
136
+ - lib/redpear/store/base.rb
137
+ - lib/redpear/store/value.rb
138
+ - lib/redpear/store/sorted_set.rb
139
+ - lib/redpear/store/enumerable.rb
140
+ - lib/redpear/store/hash.rb
141
+ - lib/redpear/store/counter.rb
142
+ - lib/redpear/store/list.rb
143
+ - lib/redpear/concern.rb
144
+ - lib/redpear/store.rb
145
+ - lib/redpear/model.rb
150
146
  - lib/redpear/schema/column.rb
147
+ - lib/redpear/schema/score.rb
151
148
  - lib/redpear/schema/index.rb
152
149
  - lib/redpear/schema/collection.rb
153
- - lib/redpear/schema/score.rb
154
- - lib/redpear/concern.rb
155
- - lib/redpear/store.rb
156
150
  - lib/redpear/model/factory_girl.rb
157
151
  - lib/redpear/model/finders.rb
158
- - lib/redpear/model/expiration.rb
159
152
  - lib/redpear/model/machinist.rb
153
+ - lib/redpear/model/expiration.rb
160
154
  - lib/redpear/version.rb
161
- - lib/redpear/store/lock.rb
162
- - lib/redpear/store/enumerable.rb
163
- - lib/redpear/store/counter.rb
164
- - lib/redpear/store/hash.rb
165
- - lib/redpear/store/base.rb
166
- - lib/redpear/store/value.rb
167
- - lib/redpear/store/list.rb
168
- - lib/redpear/store/set.rb
169
- - lib/redpear/store/sorted_set.rb
170
- - lib/redpear/model.rb
171
- - lib/redpear/schema.rb
155
+ - lib/redpear.rb
172
156
  homepage: https://github.com/bsm/redpear
173
157
  licenses: []
174
158
  post_install_message:
@@ -194,3 +178,4 @@ signing_key:
194
178
  specification_version: 3
195
179
  summary: Redpear, a Redis ORM
196
180
  test_files: []
181
+ has_rdoc: