redis_assist 0.9.0 → 0.10.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: e106eceead9af03beab72f68a1b50447fe5b7ef8
4
- data.tar.gz: 1074d7edf5d0264c57b1d2b356066983f5226996
3
+ metadata.gz: 9d4ea0740c8e9da01b4fc9cd16be301974435766
4
+ data.tar.gz: 4b3a675d57e7a2ee4c7fafb88e731a0a5a3d6fde
5
5
  SHA512:
6
- metadata.gz: ab3732eb75c8040010dbd40e2b318abea7faa835192a6fc474ae3c0c09f8c4299db014d002e6cc98742f13cc5e01fec0f1362449a935e93a19a1a17f6317308d
7
- data.tar.gz: 0f31ad582470bf564cab920d61a9a01d2b00114197e276c87fe1eca2542000907e2b10d22d8bc91991e3c1d4a413cb25d9458db9575349949f63d7473e00f9d2
6
+ metadata.gz: 869875ea789eabf01018c43aebe0777f4cc4bc712d047728add8f317d73e72ffe40cd992a0df0a49209e705fd93a036d1b9ba0c2e31ccbae2d9c1fbf4f37009a
7
+ data.tar.gz: f6eaee0706cd91d18240d458d7466d2c5ec8903a46f8cd66429da9a76df10993ca69d950f441ab78b7155ef2f221414936dd75c55c52aab3480e4370843326c5
data/lib/redis_assist.rb CHANGED
@@ -8,6 +8,7 @@ require 'redis_assist/transform'
8
8
  require 'redis_assist/callbacks'
9
9
  require 'redis_assist/validations'
10
10
  require 'redis_assist/associations'
11
+ require 'redis_assist/finders'
11
12
  require 'redis_assist/base'
12
13
  require 'redis_assist/utils'
13
14
 
@@ -4,6 +4,10 @@ module RedisAssist
4
4
  include Callbacks
5
5
  include Validations
6
6
  include Associations
7
+
8
+
9
+ extend RedisAssist::Finders
10
+
7
11
 
8
12
  def self.inherited(base)
9
13
  base.before_create {|record| record.send(:created_at=, Time.now.to_f) if record.respond_to?(:created_at) }
@@ -32,77 +36,6 @@ module RedisAssist
32
36
  def count
33
37
  redis.zcard(index_key_for(:id))
34
38
  end
35
-
36
-
37
- def all
38
- ids = redis.zrange(index_key_for(:id), 0, -1)
39
- find(ids)
40
- end
41
-
42
-
43
- def first(limit=1, offset=0)
44
- from = offset
45
- to = from + limit - 1
46
- members = redis.zrange(index_key_for(:id), from, to)
47
-
48
- find(limit > 1 ? members : members.first)
49
- end
50
-
51
-
52
- def last(limit=1, offset=0)
53
- from = offset
54
- to = from + limit - 1
55
- members = redis.zrange(index_key_for(:id), (to * -1) + -1, (from * -1) + -1).reverse
56
-
57
- find(limit > 1 ? members : members.first)
58
- end
59
-
60
-
61
- def find(ids, opts={})
62
- ids.is_a?(Array) ? find_by_ids(ids, opts) : find_by_id(ids, opts)
63
- end
64
-
65
-
66
- # find articles in batches
67
- def find_in_batches(params={})
68
- start = params[:start] || 0
69
- marker = start
70
- batch_size = params[:batch_size] || 500
71
- record_ids = redis.zrange(index_key_for(:id), marker, marker + batch_size - 1)
72
-
73
- while record_ids.length > 0
74
- records_count = record_ids.length
75
- marker += records_count
76
- records = find(record_ids)
77
-
78
- yield records
79
-
80
- break if records_count < batch_size
81
-
82
- record_ids = redis.zrange(index_key_for(:id), marker, marker + batch_size - 1)
83
- end
84
- end
85
-
86
-
87
- # Deprecated finds
88
- def find_by_id(id, opts={})
89
- raw_attributes = load_attributes(id)
90
- return nil unless raw_attributes[id][:exists].value
91
- obj = new(id: id, raw_attributes: raw_attributes[id])
92
- (obj.deleted? && !opts[:deleted].eql?(true)) ? nil : obj
93
- end
94
-
95
-
96
- def find_by_ids(ids, opts={})
97
- attrs = load_attributes(*ids)
98
- raw_attributes = attrs
99
- ids.each_with_object([]) do |id, instances|
100
- if raw_attributes[id][:exists].value
101
- instance = new(id: id, raw_attributes: raw_attributes[id])
102
- instances << instance if instance && (!instance.deleted? || opts[:deleted].eql?(true))
103
- end
104
- end
105
- end
106
39
 
107
40
 
108
41
  def create(attrs={})
@@ -111,11 +44,6 @@ module RedisAssist
111
44
  end
112
45
 
113
46
 
114
- def exists?(id)
115
- redis.exists(key_for(id, :attributes))
116
- end
117
-
118
-
119
47
  # TODO: needs a refactor. Should this be an interface for skipping validations?
120
48
  # Should we optimize and skip the find? Support an array of ids?
121
49
  def update(id, params={}, opts={})
@@ -509,6 +437,12 @@ module RedisAssist
509
437
  end
510
438
 
511
439
 
440
+ def inspect
441
+ attr_list = self.class.persisted_attrs.map{|key,val| "#{key}: #{send(key).to_s[0, 200]}" } * ", "
442
+ "#<#{self.class.name} id: #{id}, #{attr_list}>"
443
+ end
444
+
445
+
512
446
  protected
513
447
 
514
448
 
@@ -0,0 +1,102 @@
1
+ module RedisAssist
2
+ module Finders
3
+
4
+ # Checks to see if a record exists for the given `id`
5
+ # @param id [Integer] the record id
6
+ # @return [true, false]
7
+ def exists?(id)
8
+ redis.exists(key_for(id, :attributes))
9
+ end
10
+
11
+
12
+ # Find every saved record
13
+ # @return [Array] the array of models
14
+ def all
15
+ ids = redis.zrange(index_key_for(:id), 0, -1)
16
+ find(ids)
17
+ end
18
+
19
+
20
+ # Find the first saved record
21
+ # @note `first` uses a sorted set as an index of `ids` and finds the lowest id.
22
+ # @param limit [Integer] returns one or many
23
+ # @param offset [Integer] from the beginning of the index, forward.
24
+ # @return [Base, Array]
25
+ def first(limit=1, offset=0)
26
+ from = offset
27
+ to = from + limit - 1
28
+ members = redis.zrange(index_key_for(:id), from, to)
29
+
30
+ find(limit > 1 ? members : members.first)
31
+ end
32
+
33
+
34
+ # Find the first saved record
35
+ # @note `last` uses a sorted set as an index of `ids` and finds the highest id.
36
+ # @param limit [Integer] returns one or many
37
+ # @param offset [Integer] from the end of the index, back
38
+ # @return [Base, Array]
39
+ def last(limit=1, offset=0)
40
+ from = offset
41
+ to = from + limit - 1
42
+ members = redis.zrange(index_key_for(:id), (to * -1) + -1, (from * -1) + -1).reverse
43
+
44
+ find(limit > 1 ? members : members.first)
45
+ end
46
+
47
+ # Find a record by `id`
48
+ # @param ids [Integer, Array<Integer>] of the record(s) to lookup.
49
+ # @return [Base, Array] matching records
50
+ def find(ids, opts={})
51
+ ids.is_a?(Array) ? find_by_ids(ids, opts) : find_by_id(ids, opts)
52
+ end
53
+
54
+
55
+ # @deprecated Use {#find} instead
56
+ def find_by_id(id, opts={})
57
+ raw_attributes = load_attributes(id)
58
+ return nil unless raw_attributes[id][:exists].value
59
+ obj = new(id: id, raw_attributes: raw_attributes[id])
60
+ (obj.deleted? && !opts[:deleted].eql?(true)) ? nil : obj
61
+ end
62
+
63
+
64
+ # @deprecated Use {#find} instead
65
+ def find_by_ids(ids, opts={})
66
+ attrs = load_attributes(*ids)
67
+ raw_attributes = attrs
68
+ ids.each_with_object([]) do |id, instances|
69
+ if raw_attributes[id][:exists].value
70
+ instance = new(id: id, raw_attributes: raw_attributes[id])
71
+ instances << instance if instance && (!instance.deleted? || opts[:deleted].eql?(true))
72
+ end
73
+ end
74
+ end
75
+
76
+
77
+ # Iterate over all records in batches
78
+ # @param options [Hash] accepts options
79
+ # `:start` to offset from the beginning of index,
80
+ # `:batch_size` the size of the batch, default is 500.
81
+ # @param &block [Proc] passes each batch of articles to the Proc.
82
+ def find_in_batches(options={})
83
+ start = options[:start] || 0
84
+ marker = start
85
+ batch_size = options[:batch_size] || 500
86
+ record_ids = redis.zrange(index_key_for(:id), marker, marker + batch_size - 1)
87
+
88
+ while record_ids.length > 0
89
+ records_count = record_ids.length
90
+ marker += records_count
91
+ records = find(record_ids)
92
+
93
+ yield records
94
+
95
+ break if records_count < batch_size
96
+
97
+ record_ids = redis.zrange(index_key_for(:id), marker, marker + batch_size - 1)
98
+ end
99
+ end
100
+
101
+ end
102
+ end
@@ -1,3 +1,3 @@
1
1
  module RedisAssist
2
- VERSION = '0.9.0' unless defined?(::RedisAssist::VERSION)
2
+ VERSION = '0.10.0' unless defined?(::RedisAssist::VERSION)
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_assist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Love
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-27 00:00:00.000000000 Z
11
+ date: 2014-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -105,6 +105,7 @@ files:
105
105
  - lib/redis_assist/base.rb
106
106
  - lib/redis_assist/callbacks.rb
107
107
  - lib/redis_assist/config.rb
108
+ - lib/redis_assist/finders.rb
108
109
  - lib/redis_assist/transform.rb
109
110
  - lib/redis_assist/transforms/boolean_transform.rb
110
111
  - lib/redis_assist/transforms/float_transform.rb