Class: Ohm::Model::Set

Inherits:
Collection show all
Defined in:
lib/ohm.rb

Overview

Provides a Ruby-esque interface to a Redis SET. The SET is assumed to be composed of ids which maps to Collection#model.

Direct Known Subclasses

Index

Instance Attribute Summary

Attributes inherited from Collection

key, model

Instance Method Summary (collapse)

Methods inherited from Collection

#clear, #empty?, #initialize, #replace, #sort, #sort_by, #to_a

Constructor Details

This class inherits a constructor from Ohm::Model::Collection

Instance Method Details

- (Object) <<(model) Also known as: add

Adds a model to this set.

Parameters:

  • model (#id)

    Typically an instance of an Ohm::Model subclass.

See Also:



512
513
514
# File 'lib/ohm.rb', line 512

def <<(model)
  key.sadd(model.id)
end

- (Ohm::Model?) [](id)

Convenient way to scope access to a predefined set, useful for access control.

Examples:

class User < Ohm::Model
  set :photos, Photo
end

class Photo < Ohm::Model
end

@user = User.create
@user.photos.add(Photo.create(:id => "101"))
@user.photos.add(Photo.create(:id => "102"))

Photo.create(:id => "500")

@user.photos[101] == Photo[101]
# => true

@user.photos[500] == nil
# => true

Parameters:

  • id (#to_s)

    Any id existing within this set.

Returns:



502
503
504
# File 'lib/ohm.rb', line 502

def [](id)
  model[id] if key.sismember(id)
end

- (Array<Ohm::Model>) all

Array representation of this set.

Examples:

class Author < Ohm::Model
  set :posts, Post
end

class Post < Ohm::Model
end

author = Author.create
author.posts.add(Author.create(:id => "101"))
author.posts.add(Author.create(:id => "102"))

author.posts.all.is_a?(Array)
# => true

author.posts.all.include?(Author[101])
# => true

author.posts.all.include?(Author[102])
# => true

Returns:

  • (Array<Ohm::Model>)

    All members of this set.



560
561
562
# File 'lib/ohm.rb', line 560

def all
  key.smembers.map(&model)
end

- (Object) delete(member)

Thin Ruby interface wrapper for SREM.

Parameters:

  • member (#id)

    a member of this set.

See Also:



531
532
533
# File 'lib/ohm.rb', line 531

def delete(member)
  key.srem(member.id)
end

- (Object) each(&block)

An implementation which relies on SMEMBERS and yields an instance of Collection#model.

Examples:

class Author < Ohm::Model
  set :poems, Poem
end

class Poem < Ohm::Model
end

neruda = Author.create
neruda.poems.add(Poem.create)

neruda.poems.each do |poem|
  # do something with the poem
end

# if you look at the source, you'll quickly see that this can
# easily be achieved by doing the following:

neruda.poems.key.smembers.each do |id|
  poem = Poem[id]
  # do something with the poem
end

See Also:



472
473
474
# File 'lib/ohm.rb', line 472

def each(&block)
  key.smembers.each { |id| block.call(model.to_proc[id]) }
end

- (Ohm::Model::Set) except(options)

Similar to find except that it negates the criteria.

Examples:

class Post < Ohm::Model
  attribute :title
end

ohm = Post.create(:title => "Ohm")
ruby = Post.create(:title => "Ruby")

Post.except(:title => "Ohm").include?(ruby)
# => true

Post.except(:title => "Ohm").size == 1
# => true

Parameters:

  • options (Hash)

    A hash of key value pairs.

Returns:



625
626
627
628
629
# File 'lib/ohm.rb', line 625

def except(options)
  source = keys(options)
  target = source.inject(key.volatile) { |chain, other| chain - other }
  apply(:sdiffstore, key, source, target)
end

- (Ohm::Model::Set) find(options)

Allows you to find members of this set which fits the given criteria.

Examples:

class Post < Ohm::Model
  attribute :title
  attribute :tags

  index :title
  index :tag

  def tag
    tags.split(/\s+/)
  end
end

post = Post.create(:title => "Ohm", :tags => "ruby ohm redis")
Post.all.is_a?(Ohm::Model::Set)
# => true

Post.all.find(:tag => "ruby").include?(post)
# => true

# Post.find is actually just a wrapper around Post.all.find
Post.find(:tag => "ohm", :title => "Ohm").include?(post)
# => true

Post.find(:tag => ["ruby", "python"]).empty?
# => true

# Alternatively, you may choose to chain them later on.
ruby = Post.find(:tag => "ruby")
ruby.find(:title => "Ohm").include?(post)
# => true

Parameters:

  • options (Hash)

    A hash of key value pairs.

Returns:



601
602
603
604
605
# File 'lib/ohm.rb', line 601

def find(options)
  source = keys(options)
  target = source.inject(key.volatile) { |chain, other| chain + other }
  apply(:sinterstore, key, source, target)
end

- (Ohm::Model?) first(options = {})

Returns by default the lowest id value for this set. You may also pass in options similar to Collection#sort.

Examples:

class Post < Ohm::Model
  attribute :title
end

p1 = Post.create(:id => "101", :title => "Alpha")
p2 = Post.create(:id => "100", :title => "Beta")
p3 = Post.create(:id => "99", :title => "Gamma")

Post.all.is_a?(Ohm::Model::Set)
# => true

p3 == Post.all.first
# => true

p1 == Post.all.first(:order => "DESC")
# => true

p1 == Post.all.first(:by => :title, :order => "ASC ALPHA")
# => true

# just ALPHA also means ASC ALPHA, for brevity.
p1 == Post.all.first(:by => :title, :order => "ALPHA")
# => true

p3 == Post.all.first(:by => :title, :order => "DESC ALPHA")
# => true

Parameters:

  • options (Hash) (defaults to: {})

    Sort options hash.

Returns:

See Also:



668
669
670
671
672
673
674
675
676
677
# File 'lib/ohm.rb', line 668

def first(options = {})
  opts = options.dup
  opts.merge!(:limit => 1)

  if opts[:by]
    sort_by(opts.delete(:by), opts).first
  else
    sort(opts).first
  end
end

- (true, false) include?(model)

Ruby-like interface wrapper around SISMEMBER.

Parameters:

  • model (#id)

    Typically an Ohm::Model instance.

Returns:

  • (true, false)

    Whether or not the model instance is a member of this set.

See Also:



688
689
690
# File 'lib/ohm.rb', line 688

def include?(model)
  key.sismember(model.id)
end

- (Object) inspect



692
693
694
# File 'lib/ohm.rb', line 692

def inspect
  "#<Set (#{model}): #{key.smembers.inspect}>"
end

- (Fixnum) size

Thin Ruby interface wrapper for SCARD.

Returns:

  • (Fixnum)

    The total number of members for this set.

See Also:



522
523
524
# File 'lib/ohm.rb', line 522

def size
  key.scard
end