Class: Ohm::Model::Collection
Overview
Instance Attribute Summary (collapse)
-
- (Object) key
readonly
An instance of Key.
-
- (Object) model
readonly
A subclass of Ohm::Model.
Instance Method Summary (collapse)
-
- (Object) add(model)
Adds an instance of Ohm::Model to this collection.
-
- (Object) clear
Delete this collection.
-
- (true, false) empty?
Whether or not this collection is empty.
-
- (Collection) initialize(key, model)
constructor
A new instance of Collection.
-
- (Object) replace(models)
Simultaneously clear and add all models.
-
- (Object) sort(options = {})
Sort this collection using the ID by default, or an attribute defined in the elements of this collection.
-
- (Object) sort_by(att, options = {})
Sort the model instances by the given attribute.
-
- (Array) to_a
Array representation of this collection.
Constructor Details
- (Collection) initialize(key, model)
A new instance of Collection
290 291 292 293 |
# File 'lib/ohm.rb', line 290 def initialize(key, model) @key = key @model = model.unwrap end |
Instance Attribute Details
- (Object) key (readonly)
An instance of Key.
283 284 285 |
# File 'lib/ohm.rb', line 283 def key @key end |
- (Object) model (readonly)
A subclass of Ohm::Model.
286 287 288 |
# File 'lib/ohm.rb', line 286 def model @model end |
Instance Method Details
- (Object) add(model)
Adds an instance of Ohm::Model to this collection.
298 299 300 |
# File 'lib/ohm.rb', line 298 def add(model) self << model end |
- (Object) clear
Delete this collection.
392 393 394 |
# File 'lib/ohm.rb', line 392 def clear key.del end |
- (true, false) empty?
Whether or not this collection is empty.
430 431 432 |
# File 'lib/ohm.rb', line 430 def empty? !key.exists end |
- (Object) replace(models)
Simultaneously clear and add all models. This wraps all operations in a MULTI EXEC block to make the whole operation atomic.
422 423 424 425 426 427 |
# File 'lib/ohm.rb', line 422 def replace(models) model.db.multi do clear models.each { |model| add(model) } end end |
- (Object) sort(options = {})
Sort this collection using the ID by default, or an attribute defined in the elements of this collection.
NOTE: It is worth mentioning that if you want to sort by a specific attribute instead of an ID, you would probably want to use sort_by instead.
335 336 337 338 339 340 341 342 343 |
# File 'lib/ohm.rb', line 335 def sort( = {}) return [] unless key.exists opts = .dup opts[:start] ||= 0 opts[:limit] = [opts[:start], opts[:limit]] if opts[:limit] key.sort(opts).map(&model) end |
- (Object) sort_by(att, options = {})
Sort the model instances by the given attribute.
357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/ohm.rb', line 357 def sort_by(att, = {}) return [] unless key.exists opts = .dup opts.merge!(:by => model.key["*->#{att}"]) if opts[:get] key.sort(opts.merge(:get => model.key["*->#{opts[:get]}"])) else sort(opts) end end |
- (Array) to_a
Array representation of this collection.
435 436 437 |
# File 'lib/ohm.rb', line 435 def to_a all end |