Class: Ohm::Attributes::Collection
- Inherits:
- Object
- Includes:
- Enumerable
- Defined in:
- lib/ohm.rb
Attribute Summary
- - (Object) db Returns the value of attribute db.
- - (Object) key Returns the value of attribute key.
- - (Object) model Returns the value of attribute model.
Method Summary
- - (Object) ==(other)
- - (Object) add(model)
- - (Object) all Return instances of model for all the ids contained in the collection.
- - (Object) clear Clears the values in the collection.
- - (Object) concat(values) Appends the given values to the collection.
- - (Object) each(&block)
- - (true) 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(db, key, model = nil) 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) sort_by(att, options = {}) Sort the model instances by the given attribute.
- - (Object) to_ary
Constructor Details
- (Collection) initialize(db, key, model = nil)
A new instance of Collection
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)
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.
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.
127 128 129 130 131 132 |
# File 'lib/ohm.rb', line 127 def first( = {}) = .merge(:limit => 1) [:by] ? sort_by(.delete(:by), ).first : sort().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.
97 98 99 100 101 102 103 |
# File 'lib/ohm.rb', line 97 def sort( = {}) return [] if empty? [:start] ||= 0 [:limit] = [[:start], [:limit]] if [:limit] result = db.sort(key, ) [:get] ? result : instantiate(result) end |
- (Object) sort_by(att, options = {})
Sort the model instances by the given attribute.
115 116 117 |
# File 'lib/ohm.rb', line 115 def sort_by(att, = {}) sort(.merge(:by => model.key("*", att))) end |
- (Object) to_ary
134 135 136 |
# File 'lib/ohm.rb', line 134 def to_ary all end |