Class: Friendly::Index

Inherits:
Table show all
Defined in:
lib/friendly/index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, fields, datastore = Friendly.datastore) ⇒ Index

Returns 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

#datastoreObject (readonly)

Returns the value of attribute datastore.



5
6
7
# File 'lib/friendly/index.rb', line 5

def datastore
  @datastore
end

#fieldsObject (readonly)

Returns the value of attribute fields.



5
6
7
# File 'lib/friendly/index.rb', line 5

def fields
  @fields
end

#klassObject (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

Returns:



17
18
19
# File 'lib/friendly/index.rb', line 17

def satisfies?(query)
  exact_match?(query) || valid_partial_match?(query)
end

#table_nameObject



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