Class: Friendly::Index
Instance Attribute Summary (collapse)
-
- (Object) datastore
readonly
Returns the value of attribute datastore.
-
- (Object) fields
readonly
Returns the value of attribute fields.
-
- (Object) klass
readonly
Returns the value of attribute klass.
Instance Method Summary (collapse)
- - (Object) all(query)
- - (Object) count(query)
- - (Object) create(document)
- - (Object) destroy(document)
- - (Object) first(query)
-
- (Index) initialize(klass, fields, datastore = Friendly.datastore)
constructor
A new instance of Index.
- - (Boolean) satisfies?(query)
- - (Object) table_name
- - (Object) update(document)
Constructor Details
- (Index) initialize(klass, fields, datastore = Friendly.datastore)
A new instance of Index
7 8 9 10 11 |
# File 'lib/friendly/index.rb', line 7 def initialize(klass, fields, datastore = Friendly.datastore) @klass = klass @fields = fields @datastore = datastore end |
Instance Attribute Details
- (Object) datastore (readonly)
Returns the value of attribute datastore
5 6 7 |
# File 'lib/friendly/index.rb', line 5 def datastore @datastore end |
- (Object) fields (readonly)
Returns the value of attribute fields
5 6 7 |
# File 'lib/friendly/index.rb', line 5 def fields @fields end |
- (Object) klass (readonly)
Returns the value of attribute klass
5 6 7 |
# File 'lib/friendly/index.rb', line 5 def klass @klass end |
Instance Method Details
- (Object) all(query)
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 |
- (Object) count(query)
31 32 33 |
# File 'lib/friendly/index.rb', line 31 def count(query) datastore.count(self, query) end |
- (Object) create(document)
35 36 37 |
# File 'lib/friendly/index.rb', line 35 def create(document) datastore.insert(self, record(document)) end |
- (Object) destroy(document)
43 44 45 |
# File 'lib/friendly/index.rb', line 43 def destroy(document) datastore.delete(self, document.id) end |
- (Object) first(query)
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 |
- (Boolean) satisfies?(query)
17 18 19 |
# File 'lib/friendly/index.rb', line 17 def satisfies?(query) exact_match?(query) || valid_partial_match?(query) end |
- (Object) table_name
13 14 15 |
# File 'lib/friendly/index.rb', line 13 def table_name ["index", klass.table_name, "on", fields.join("_and_")].join("_") end |
- (Object) update(document)
39 40 41 |
# File 'lib/friendly/index.rb', line 39 def update(document) datastore.update(self, document.id, record(document)) end |