Module: RedisAssist::Finders

Included in:
Base
Defined in:
lib/redis_assist/finders.rb

Instance Method Summary collapse

Instance Method Details

#allArray

Find every saved record

Returns:

  • (Array)

    the array of models



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`

Parameters:

  • id (Integer)

    the record id

Returns:

  • (true, false)


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`

Parameters:

  • ids (Integer, Array<Integer>)

    of the record(s) to lookup.

Returns:

  • (Base, Array)

    matching records



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

Parameters:

  • options (Hash) (defaults to: {})

    accepts options ‘:start` to offset from the beginning of index, `:batch_size` the size of the batch, default is 500.

  • &block (Proc)

    passes each batch of articles to the Proc.



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(options={})
  start       = options[:start]      || 0
  marker      = start
  batch_size  = options[: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

Parameters:

  • limit (Integer) (defaults to: 1)

    returns one or many

  • offset (Integer) (defaults to: 0)

    from the beginning of the index, forward.

Returns:



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

Parameters:

  • limit (Integer) (defaults to: 1)

    returns one or many

  • offset (Integer) (defaults to: 0)

    from the end of the index, back

Returns:



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