Class: Ohm::Model::Index

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

Instance Attribute Summary

Attributes inherited from Collection

key, model

Instance Method Summary (collapse)

Methods inherited from Set

#<<, #[], #all, #delete, #each, #except, #first, #include?, #inspect, #size

Methods inherited from Collection

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

Constructor Details

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

Instance Method Details

- (Object) find(options)

This method is here primarily as an optimization. Let’s say you have the following model:

  class Post < Ohm::Model
    attribute :title
    index :title
  end

  ruby  = Post.create(:title => "ruby")
  redis = Post.create(:title => "redis")

  Post.key[:all].smembers == [ruby.id, redis.id]
  # => true

  Post.index_key_for(:title, "ruby").smembers == [ruby.id]
  # => true

  Post.index_key_for(:title, "redis").smembers == [redis.id]
  # => true

If we want to search for example all `Posts` entitled “ruby” or “redis”, then it doesn’t make sense to do an INTERSECTION with `Post.key[:all]` since it would be redundant.

The implementation of #find avoids this redundancy.



747
748
749
750
751
752
# File 'lib/ohm.rb', line 747

def find(options)
  keys = keys(options)
  return super(options) if keys.size > 1

  Set.new(keys.first, Wrapper.wrap(model))
end