Class: Moped::Indexes
- Includes:
- Enumerable
- Defined in:
- lib/moped/indexes.rb
Overview
Defines behaviour around indexes.
Instance Attribute Summary collapse
-
#collection_name ⇒ Object
readonly
Returns the value of attribute collection_name.
- #collection_name The collection name.(Thecollectionname.) ⇒ Object readonly
-
#database ⇒ Object
readonly
Returns the value of attribute database.
- #database The database.(Thedatabase.) ⇒ Object readonly
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
- #namespace The index namespace.(Theindexnamespace.) ⇒ Object readonly
Instance Method Summary collapse
-
#[](key) ⇒ Hash?
Retrive an index by its definition.
-
#create(key, options = {}) ⇒ Hash
Create an index unless it already exists.
-
#drop(key = nil) ⇒ Boolean
Drop an index, or all indexes.
-
#each {|Hash| ... } ⇒ Enumerator
Iterate over each of the indexes for the collection.
-
#initialize(database, collection_name) ⇒ Indexes
constructor
Initialize the indexes.
Constructor Details
#initialize(database, collection_name) ⇒ Indexes
Initialize the indexes.
114 115 116 117 |
# File 'lib/moped/indexes.rb', line 114 def initialize(database, collection_name) @database, @collection_name = database, collection_name @namespace = "#{database.name}.#{collection_name}" end |
Instance Attribute Details
#collection_name ⇒ Object (readonly)
Returns the value of attribute collection_name.
10 11 12 |
# File 'lib/moped/indexes.rb', line 10 def collection_name @collection_name end |
#collection_name The collection name.(Thecollectionname.) ⇒ Object (readonly)
10 |
# File 'lib/moped/indexes.rb', line 10 attr_reader :collection_name, :database, :namespace |
#database ⇒ Object (readonly)
Returns the value of attribute database.
10 11 12 |
# File 'lib/moped/indexes.rb', line 10 def database @database end |
#database The database.(Thedatabase.) ⇒ Object (readonly)
10 |
# File 'lib/moped/indexes.rb', line 10 attr_reader :collection_name, :database, :namespace |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
10 11 12 |
# File 'lib/moped/indexes.rb', line 10 def namespace @namespace end |
#namespace The index namespace.(Theindexnamespace.) ⇒ Object (readonly)
10 |
# File 'lib/moped/indexes.rb', line 10 attr_reader :collection_name, :database, :namespace |
Instance Method Details
#[](key) ⇒ Hash?
Retrive an index by its definition.
23 24 25 |
# File 'lib/moped/indexes.rb', line 23 def [](key) database[:"system.indexes"].find(ns: namespace, key: key).one end |
#create(key, options = {}) ⇒ Hash
Create an index unless it already exists.
55 56 57 58 59 |
# File 'lib/moped/indexes.rb', line 55 def create(key, = {}) spec = .merge(ns: namespace, key: key) spec[:name] ||= key.to_a.join("_") database[:"system.indexes"].insert(spec) end |
#drop(key = nil) ⇒ Boolean
Drop an index, or all indexes.
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/moped/indexes.rb', line 78 def drop(key = nil) if key index = self[key] or return false name = index["name"] else name = "*" end result = database.command deleteIndexes: collection_name, index: name result["ok"] == 1 end |
#each {|Hash| ... } ⇒ Enumerator
Iterate over each of the indexes for the collection.
101 102 103 |
# File 'lib/moped/indexes.rb', line 101 def each(&block) database[:"system.indexes"].find(ns: namespace).each(&block) end |