Class: Ohm::Model::Collection

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

Overview

Defines the base implementation for all enumerable types in Ohm, which includes Sets, Lists and Indices.

Direct Known Subclasses

List, Set

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Collection) initialize(key, model)

A new instance of Collection

Parameters:



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.

Parameters:

  • model (#id)

    A model with an ID.



298
299
300
# File 'lib/ohm.rb', line 298

def add(model)
  self << model
end

- (Object) clear

Delete this collection.

Examples:

class Post < Ohm::Model
  list :comments, Comment
end

class Comment < Ohm::Model
end

post = Post.create
post.comments << Comment.create

post.comments.size == 1
# => true

post.comments.clear
post.comments.size == 0
# => true

See Also:



392
393
394
# File 'lib/ohm.rb', line 392

def clear
  key.del
end

- (true, false) empty?

Whether or not this collection is empty.

Returns:

  • (true, false)

    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.

Examples:

class Post < Ohm::Model
  list :comments, Comment
end

class Comment < Ohm::Model
end

post = Post.create
post.comments << Comment.create(:id => 100)

post.comments.map(&:id) == ["100"]
# => true

comments = (101..103).to_a.map { |i| Comment.create(:id => i) }

post.comments.replace(comments)
post.comments.map(&:id) == ["101", "102", "103"]
# => true

See Also:



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.

Examples:

class Post < Ohm::Model
  attribute :title
end

p1 = Post.create(:title => "Alpha")
p2 = Post.create(:title => "Beta")
p3 = Post.create(:title => "Gamma")

[p1, p2, p3] == Post.all.sort.to_a
# => true

[p3, p2, p1] == Post.all.sort(:order => "DESC").to_a
# => true

[p1, p2, p3] == Post.all.sort(:by => "Post:*->title",
                              :order => "ASC ALPHA").to_a
# => true

[p3, p2, p1] == Post.all.sort(:by => "Post:*->title",
                              :order => "DESC ALPHA").to_a
# => true

See Also:



335
336
337
338
339
340
341
342
343
# File 'lib/ohm.rb', line 335

def sort(options = {})
  return [] unless key.exists

  opts = options.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.

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

See Also:



357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/ohm.rb', line 357

def sort_by(att, options = {})
  return [] unless key.exists

  opts = options.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.

Returns:

  • (Array)

    Array representation of this collection.



435
436
437
# File 'lib/ohm.rb', line 435

def to_a
  all
end