Class: Friendly::Index
Instance Attribute Summary collapse
-
#datastore ⇒ Object
readonly
Returns the value of attribute datastore.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
- #all(query) ⇒ Object
- #count(query) ⇒ Object
- #create(document) ⇒ Object
- #destroy(document) ⇒ Object
- #first(query) ⇒ Object
-
#initialize(klass, fields, datastore = Friendly.datastore) ⇒ Index
constructor
A new instance of Index.
- #satisfies?(query) ⇒ Boolean
- #table_name ⇒ Object
- #update(document) ⇒ Object
Constructor Details
Instance Attribute Details
#datastore ⇒ Object (readonly)
Returns the value of attribute datastore.
5 6 7 |
# File 'lib/friendly/index.rb', line 5 def datastore @datastore end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
5 6 7 |
# File 'lib/friendly/index.rb', line 5 def fields @fields end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/friendly/index.rb', line 5 def klass @klass end |
Instance Method Details
#all(query) ⇒ Object
26 27 28 29 |
# File 'lib/friendly/index.rb', line 26 def all(query) ids = datastore.all(self, query).map { |row| row[:id] } klass.all(:id => ids, :preserve_order! => !query.order.nil?) end |
#count(query) ⇒ Object
31 32 33 |
# File 'lib/friendly/index.rb', line 31 def count(query) datastore.count(self, query) end |
#create(document) ⇒ Object
35 36 37 |
# File 'lib/friendly/index.rb', line 35 def create(document) datastore.insert(self, record(document)) end |
#destroy(document) ⇒ Object
43 44 45 |
# File 'lib/friendly/index.rb', line 43 def destroy(document) datastore.delete(self, document.id) end |
#first(query) ⇒ Object
21 22 23 24 |
# File 'lib/friendly/index.rb', line 21 def first(query) row = datastore.first(self, query) row && klass.first(:id => row[:id]) end |
#satisfies?(query) ⇒ Boolean
17 18 19 |
# File 'lib/friendly/index.rb', line 17 def satisfies?(query) exact_match?(query) || valid_partial_match?(query) end |
#table_name ⇒ Object
13 14 15 |
# File 'lib/friendly/index.rb', line 13 def table_name ["index", klass.table_name, "on", fields.join("_and_")].join("_") end |
#update(document) ⇒ Object
39 40 41 |
# File 'lib/friendly/index.rb', line 39 def update(document) datastore.update(self, document.id, record(document)) end |