Class: Ohm::Collection
- Inherits:
-
Object
- Object
- Ohm::Collection
- Includes:
- Enumerable
- Defined in:
- lib/ohm/collection.rb
Instance Attribute Summary (collapse)
-
- (Object) db
Returns the value of attribute db.
-
- (Object) key
Returns the value of attribute key.
Instance Method Summary (collapse)
- - (Object) ==(other)
- - (Object) [](index)
-
- (Object) clear
Clears the values in the collection.
-
- (Object) concat(values)
Appends the given values to the collection.
- - (Object) each(&block)
-
- (true, false) empty?
Returns whether or not the collection is empty.
-
- (Ohm::Model?) first(options = {})
Sort the model instances by id and return the first instance found.
-
- (Collection) initialize(key, db = Ohm.redis)
constructor
A new instance of Collection.
-
- (Object) replace(values)
Replaces the collection with the passed values.
-
- (Object) sort(options = {})
Return the values as model instances, ordered by the options supplied.
- - (Object) to_ary
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.
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.
46 47 48 49 |
# File 'lib/ohm/collection.rb', line 46 def first( = {}) = .merge(:limit => 1) sort().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.
32 33 34 35 36 37 |
# File 'lib/ohm/collection.rb', line 32 def sort( = {}) return [] if empty? [:start] ||= 0 [:limit] = [[:start], [:limit]] if [:limit] db.sort(key, ) end |
- (Object) to_ary
55 56 57 |
# File 'lib/ohm/collection.rb', line 55 def to_ary all end |