Class: Friendly::DocumentTable

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

Instance Attribute Summary collapse

Attributes inherited from Table

#datastore

Instance Method Summary collapse

Methods inherited from Storage

#count

Constructor Details

#initialize(klass, datastore = Friendly.datastore, translator = Translator.new) ⇒ DocumentTable

Returns a new instance of DocumentTable.



7
8
9
10
11
# File 'lib/friendly/document_table.rb', line 7

def initialize(klass, datastore=Friendly.datastore, translator=Translator.new)
  super(datastore)
  @klass      = klass
  @translator = translator
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
end

#translatorObject (readonly)

Returns the value of attribute translator.



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

def translator
  @translator
end

Instance Method Details

#all(query) ⇒ Object



42
43
44
# File 'lib/friendly/document_table.rb', line 42

def all(query)
  datastore.all(klass, query).map { |r| to_object(r) }
end

#create(document) ⇒ Object



21
22
23
24
25
# File 'lib/friendly/document_table.rb', line 21

def create(document)
  record = translator.to_record(document)
  datastore.insert(document, record)
  update_document(document, record)
end

#destroy(document) ⇒ Object



33
34
35
# File 'lib/friendly/document_table.rb', line 33

def destroy(document)
  datastore.delete(document, document.id)
end

#first(query) ⇒ Object



37
38
39
40
# File 'lib/friendly/document_table.rb', line 37

def first(query)
  record = datastore.first(klass, query)
  record && to_object(record)
end

#satisfies?(query) ⇒ Boolean

Returns:



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

def satisfies?(query)
  query.conditions.keys == [:id] && !query.order
end

#table_nameObject



13
14
15
# File 'lib/friendly/document_table.rb', line 13

def table_name
  klass.table_name
end

#update(document) ⇒ Object



27
28
29
30
31
# File 'lib/friendly/document_table.rb', line 27

def update(document)
  record = translator.to_record(document)
  datastore.update(document, document.id, record)
  update_document(document, record)
end