Class: Ohm::Collection

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/ohm/collection.rb

Direct Known Subclasses

List, Set

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Collection) initialize(key, db = Ohm.redis)

A new instance of Collection



7
8
9
10
# File 'lib/ohm/collection.rb', line 7

def initialize(key, db = Ohm.redis)
  self.key = key
  self.db = db
end

Instance Attribute Details

- (Object) db

Returns the value of attribute db



5
6
7
# File 'lib/ohm/collection.rb', line 5

def db
  @db
end

- (Object) key

Returns the value of attribute key



5
6
7
# File 'lib/ohm/collection.rb', line 5

def key
  @key
end

Instance Method Details

- (Object) ==(other)



59
60
61
# File 'lib/ohm/collection.rb', line 59

def ==(other)
  to_ary == other
end

- (Object) [](index)



51
52
53
# File 'lib/ohm/collection.rb', line 51

def [](index)
  first(:start => index)
end

- (Object) clear

Clears the values in the collection.



69
70
71
72
# File 'lib/ohm/collection.rb', line 69

def clear
  db.del(key)
  self
end

- (Object) concat(values)

Appends the given values to the collection.



75
76
77
78
# File 'lib/ohm/collection.rb', line 75

def concat(values)
  values.each { |value| self << value }
  self
end

- (Object) each(&block)



12
13
14
# File 'lib/ohm/collection.rb', line 12

def each(&block)
  all.each(&block)
end

- (true, false) empty?

Returns whether or not the collection is empty.

Returns:

  • (true, false)

    Returns whether or not the collection is empty.



64
65
66
# File 'lib/ohm/collection.rb', line 64

def empty?
  size.zero?
end

- (Ohm::Model?) first(options = {})

Sort the model instances by id and return the first instance found. If a :by option is provided with a valid attribute name, the method sort_by is used instead and the option provided is passed as the first parameter.

Returns:

  • (Ohm::Model, nil)

    Returns the first instance found or nil.

See Also:



46
47
48
49
# File 'lib/ohm/collection.rb', line 46

def first(options = {})
  options = options.merge(:limit => 1)
  sort(options).first
end

- (Object) replace(values)

Replaces the collection with the passed values.



81
82
83
84
# File 'lib/ohm/collection.rb', line 81

def replace(values)
  clear
  concat(values)
end

- (Object) sort(options = {})

Return the values as model instances, ordered by the options supplied. Check redis documentation to see what values you can provide to each option.

Examples:

Get the first ten users sorted alphabetically by name:

@event.attendees.sort(:by => :name, :order => "ALPHA", :limit => 10)

Get five posts sorted by number of votes and starting from the number 5 (zero based):

@blog.posts.sort(:by => :votes, :start => 5, :limit => 10")

Parameters:

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

    options to sort the collection.

Options Hash (options):

  • (#to_s) :by N/A

    Model attribute to sort the instances by.

  • (#to_s) :order — default: ASC

    Sorting order, which can be ASC or DESC.

  • (Integer) :limit — default: all

    Number of items to return.

  • (Integer) :start — default: 0

    An offset from where the limit will be applied.



32
33
34
35
36
37
# File 'lib/ohm/collection.rb', line 32

def sort(options = {})
  return [] if empty?
  options[:start] ||= 0
  options[:limit] = [options[:start], options[:limit]] if options[:limit]
  db.sort(key, options)
end

- (Object) to_ary



55
56
57
# File 'lib/ohm/collection.rb', line 55

def to_ary
  all
end