Module: RedisAssist::Finders
- Included in:
- Base
- Defined in:
- lib/redis_assist/finders.rb
Instance Method Summary collapse
-
#all ⇒ Array
Find every saved record.
-
#exists?(id) ⇒ true, false
Checks to see if a record exists for the given ‘id`.
-
#find(ids, opts = {}) ⇒ Base, Array
Find a record by ‘id`.
-
#find_by_id(id, opts = {}) ⇒ Object
deprecated
Deprecated.
Use #find instead
-
#find_by_ids(ids, opts = {}) ⇒ Object
deprecated
Deprecated.
Use #find instead
-
#find_in_batches(options = {}) ⇒ Object
Iterate over all records in batches.
-
#first(limit = 1, offset = 0) ⇒ Base, Array
Find the first saved record.
-
#last(limit = 1, offset = 0) ⇒ Base, Array
Find the first saved record.
Instance Method Details
#all ⇒ Array
Find every saved record
14 15 16 17 |
# File 'lib/redis_assist/finders.rb', line 14 def all ids = redis.zrange(index_key_for(:id), 0, -1) find(ids) end |
#exists?(id) ⇒ true, false
Checks to see if a record exists for the given ‘id`
7 8 9 |
# File 'lib/redis_assist/finders.rb', line 7 def exists?(id) redis.exists(key_for(id, :attributes)) end |
#find(ids, opts = {}) ⇒ Base, Array
Find a record by ‘id`
50 51 52 |
# File 'lib/redis_assist/finders.rb', line 50 def find(ids, opts={}) ids.is_a?(Array) ? find_by_ids(ids, opts) : find_by_id(ids, opts) end |
#find_by_id(id, opts = {}) ⇒ Object
Deprecated.
Use #find instead
56 57 58 59 60 61 |
# File 'lib/redis_assist/finders.rb', line 56 def find_by_id(id, opts={}) raw_attributes = load_attributes(id) return nil unless raw_attributes[id][:exists].value obj = new(id: id, raw_attributes: raw_attributes[id]) (obj.deleted? && !opts[:deleted].eql?(true)) ? nil : obj end |
#find_by_ids(ids, opts = {}) ⇒ Object
Deprecated.
Use #find instead
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/redis_assist/finders.rb', line 65 def find_by_ids(ids, opts={}) attrs = load_attributes(*ids) raw_attributes = attrs ids.each_with_object([]) do |id, instances| if raw_attributes[id][:exists].value instance = new(id: id, raw_attributes: raw_attributes[id]) instances << instance if instance && (!instance.deleted? || opts[:deleted].eql?(true)) end end end |
#find_in_batches(options = {}) ⇒ Object
Iterate over all records in batches
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/redis_assist/finders.rb', line 82 def find_in_batches(={}) start = [:start] || 0 marker = start batch_size = [:batch_size] || 500 record_ids = redis.zrange(index_key_for(:id), marker, marker + batch_size - 1) while record_ids.length > 0 records_count = record_ids.length marker += records_count records = find(record_ids) yield records break if records_count < batch_size record_ids = redis.zrange(index_key_for(:id), marker, marker + batch_size - 1) end end |
#first(limit = 1, offset = 0) ⇒ Base, Array
Note:
‘first` uses a sorted set as an index of `ids` and finds the lowest id.
Find the first saved record
25 26 27 28 29 30 31 |
# File 'lib/redis_assist/finders.rb', line 25 def first(limit=1, offset=0) from = offset to = from + limit - 1 members = redis.zrange(index_key_for(:id), from, to) find(limit > 1 ? members : members.first) end |
#last(limit = 1, offset = 0) ⇒ Base, Array
Note:
‘last` uses a sorted set as an index of `ids` and finds the highest id.
Find the first saved record
39 40 41 42 43 44 45 |
# File 'lib/redis_assist/finders.rb', line 39 def last(limit=1, offset=0) from = offset to = from + limit - 1 members = redis.zrange(index_key_for(:id), (to * -1) + -1, (from * -1) + -1).reverse find(limit > 1 ? members : members.first) end |