Class: Ohm::Attributes::Collection

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

Attribute Summary

Method Summary

Constructor Details

- (Collection) initialize(db, key, model = nil)

A new instance of Collection

Returns:



66
67
68
69
70
# File 'lib/ohm.rb', line 66

def initialize(db, key, model = nil)
  self.db = db
  self.key = key
  self.model = model
end

Attribute Details

- (Object) db

Returns the value of attribute db



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

def db
  @db
end

- (Object) key

Returns the value of attribute key



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

def key
  @key
end

- (Object) model

Returns the value of attribute model



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

def model
  @model
end

Method Details

- (Object) ==(other)



138
139
140
# File 'lib/ohm.rb', line 138

def ==(other)
  to_ary == other
end

- (Object) add(model)

Parameters:

  • (Ohm::Model#id) value — Adds the id of the object if it’s an Ohm::Model.


166
167
168
# File 'lib/ohm.rb', line 166

def add(model)
  self << model.id
end

- (Object) all

Return instances of model for all the ids contained in the collection.



77
78
79
# File 'lib/ohm.rb', line 77

def all
  instantiate(raw)
end

- (Object) clear

Clears the values in the collection.



148
149
150
151
# File 'lib/ohm.rb', line 148

def clear
  db.del(key)
  self
end

- (Object) concat(values)

Appends the given values to the collection.



154
155
156
157
# File 'lib/ohm.rb', line 154

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

- (Object) each(&block)



72
73
74
# File 'lib/ohm.rb', line 72

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

- (true) empty?

Returns whether or not the collection is empty.

Returns:

  • (true, false) — Returns whether or not the collection is empty.


143
144
145
# File 'lib/ohm.rb', line 143

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:



127
128
129
130
131
132
# File 'lib/ohm.rb', line 127

def first(options = {})
  options = options.merge(:limit => 1)
  options[:by] ?
    sort_by(options.delete(:by), options).first :
    sort(options).first
end

- (Object) replace(values)

Replaces the collection with the passed values.



160
161
162
163
# File 'lib/ohm.rb', line 160

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.


97
98
99
100
101
102
103
# File 'lib/ohm.rb', line 97

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

- (Object) sort_by(att, options = {})

Sort the model instances by the given attribute.

Examples:

Sorting elements by name:

  User.create :name => "B"
  User.create :name => "A"

  user = User.all.sort_by(:name, :order => "ALPHA").first
  user.name == "A"
  # => true


115
116
117
# File 'lib/ohm.rb', line 115

def sort_by(att, options = {})
  sort(options.merge(:by => model.key("*", att)))
end

- (Object) to_ary



134
135
136
# File 'lib/ohm.rb', line 134

def to_ary
  all
end