Class: Sohm::MultiSet

Inherits:
BasicSet show all
Defined in:
lib/sohm.rb

Overview

Anytime you filter a set with more than one requirement, you internally use a MultiSet. MultiSet is a bit slower than just a Set because it has to SINTERSTORE all the keys prior to retrieving the members, size, etc.

Example:

User.all.kind_of?(Sohm::Set)
# => true

User.find(:name => "John").kind_of?(Sohm::Set)
# => true

User.find(:name => "John", :age => 30).kind_of?(Sohm::MultiSet)
# => true

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BasicSet

#[], #exists?, #ids, #include?, #sample, #size

Methods included from Collection

#each, #empty?, #fetch, #to_a, #to_json

Constructor Details

#initialize(namespace, model, command) ⇒ MultiSet

Returns a new instance of MultiSet.



535
536
537
538
539
# File 'lib/sohm.rb', line 535

def initialize(namespace, model, command)
  @namespace = namespace
  @model = model
  @command = command
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



533
534
535
# File 'lib/sohm.rb', line 533

def command
  @command
end

#modelObject (readonly)

Returns the value of attribute model.



532
533
534
# File 'lib/sohm.rb', line 532

def model
  @model
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

Instance Method Details

#combine(dict) ⇒ Object

Perform an intersection between the existent set and the new set created by the union of the passed filters.

Example:

set = User.find(:status => "active")
set.combine(:name => ["John", "Jane"])

# The result will include all users with active status
# and with names "John" or "Jane".


580
581
582
583
584
# File 'lib/sohm.rb', line 580

def combine(dict)
  MultiSet.new(
    namespace, model, Command[:sinterstore, command, unioned(dict)]
  )
end

#except(dict) ⇒ Object

Reduce the set using any number of filters.

Example:

set = User.find(:name => "John")
set.except(:country => "US")

# You can also do it in one line.
User.find(:name => "John").except(:country => "US")


564
565
566
567
568
# File 'lib/sohm.rb', line 564

def except(dict)
  MultiSet.new(
    namespace, model, Command[:sdiffstore, command, unioned(dict)]
  )
end

#find(dict) ⇒ Object

Chain new fiters on an existing set.

Example:

set = User.find(:name => "John", :age => 30)
set.find(:status => 'pending')


548
549
550
551
552
# File 'lib/sohm.rb', line 548

def find(dict)
  MultiSet.new(
    namespace, model, Command[:sinterstore, command, intersected(dict)]
  )
end

#union(dict) ⇒ Object

Do a union to the existing set using any number of filters.

Example:

set = User.find(:name => "John")
set.union(:name => "Jane")

# You can also do it in one line.
User.find(:name => "John").union(:name => "Jane")


596
597
598
599
600
# File 'lib/sohm.rb', line 596

def union(dict)
  MultiSet.new(
    namespace, model, Command[:sunionstore, command, intersected(dict)]
  )
end