Class: Friendly::DocumentTable
- Defined in:
- lib/friendly/document_table.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#translator ⇒ Object
readonly
Returns the value of attribute translator.
Attributes inherited from Table
Instance Method Summary collapse
- #all(query) ⇒ Object
- #create(document) ⇒ Object
- #destroy(document) ⇒ Object
- #first(query) ⇒ Object
-
#initialize(klass, datastore = Friendly.datastore, translator = Translator.new) ⇒ DocumentTable
constructor
A new instance of DocumentTable.
- #satisfies?(query) ⇒ Boolean
- #table_name ⇒ Object
- #update(document) ⇒ Object
Methods inherited from Storage
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
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/friendly/document_table.rb', line 5 def klass @klass end |
#translator ⇒ Object (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
17 18 19 |
# File 'lib/friendly/document_table.rb', line 17 def satisfies?(query) query.conditions.keys == [:id] && !query.order end |
#table_name ⇒ Object
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 |