Class: RedisScope

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_record/redis_scope.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  offset:   0,
  filters:  [],
  sort:     :id,
  min:      '-inf',
  max:      '+inf'
}

Instance Method Summary collapse

Constructor Details

#initialize(model, opts = {}) ⇒ RedisScope

Returns a new instance of RedisScope.



10
11
12
13
# File 'lib/redis_record/redis_scope.rb', line 10

def initialize(model, opts = {})
  @model = model
  @options = DEFAULT_OPTIONS.merge opts
end

Instance Method Details

#allObject



59
60
61
# File 'lib/redis_record/redis_scope.rb', line 59

def all
  ids.map {|id| @model.find id}
end

#countObject

Executors



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/redis_record/redis_scope.rb', line 46

def count
  apply_filters
  total_records = RedisRecord.REDIS.zcard temp_key

  remaining_records = total_records - @options[:offset]

  if @options[:limit] and @options[:limit] < remaining_records
    @options[:limit]
  else
    remaining_records
  end
end

#filter(name, value = true) ⇒ Object

Modifiers always returns self

Raises:

  • (NameError)


18
19
20
21
22
23
# File 'lib/redis_record/redis_scope.rb', line 18

def filter(name, value=true)
  raise NameError, ":#{name} isn't in the defined filters" unless @model.defined_filters.include? name
  new_filters = @options[:filters] + [[name, value]]

  chain_scope filters: new_filters
end

#firstObject



65
66
67
# File 'lib/redis_record/redis_scope.rb', line 65

def first
  limit(1).all[0]
end

#lastObject



69
70
71
# File 'lib/redis_record/redis_scope.rb', line 69

def last
  offset(@options[:offset] + count - 1).limit(1).all[0]
end

#limit(n) ⇒ Object



37
38
39
# File 'lib/redis_record/redis_scope.rb', line 37

def limit(n)
  chain_scope limit: n
end

#max(value) ⇒ Object



33
34
35
# File 'lib/redis_record/redis_scope.rb', line 33

def max(value)
  chain_scope max: value
end

#min(value) ⇒ Object



29
30
31
# File 'lib/redis_record/redis_scope.rb', line 29

def min(value)
  chain_scope min: value
end

#offset(n) ⇒ Object



41
42
43
# File 'lib/redis_record/redis_scope.rb', line 41

def offset(n)
  chain_scope offset: n
end

#sort(attr) ⇒ Object



25
26
27
# File 'lib/redis_record/redis_scope.rb', line 25

def sort(attr)
  chain_scope sort: attr
end