Class: DataDoc::Store
- Inherits:
-
Object
- Object
- DataDoc::Store
- Defined in:
- lib/data_doc/store.rb
Overview
Defines schema for structured content and accepts rows.
Sets up an ActiveRecord store, defines its tables and fields, and adds row content.
Instance Attribute Summary collapse
-
#arel ⇒ Object
readonly
AREL object encapsulating table.
Instance Method Summary collapse
-
#datetime(name, opts = {}) ⇒ Object
Define a datetime field.
-
#initialize(doc, table_name_or_sym, opts = {}, &blk) ⇒ Store
constructor
Define a store.
-
#insert(record) ⇒ Object
Insert a row from a hash.
-
#integer(name, opts = {}) ⇒ Object
Define an integer field.
-
#string(name, opts = {}) ⇒ Object
Define a string field.
-
#text(name, opts = {}) ⇒ Object
Define a text field.
Constructor Details
#initialize(doc, table_name_or_sym, opts = {}, &blk) ⇒ Store
Define a store.
Yields to a block calling the store for fields and other schema definition. Table is re-created and emptied unless read_only, and just emptied if data_only.
20 21 22 23 24 |
# File 'lib/data_doc/store.rb', line 20 def initialize(doc, table_name_or_sym, opts = {}, &blk) @doc = doc @connection = @doc.connection create_store(table_name_or_sym, opts, &blk) end |
Instance Attribute Details
#arel ⇒ Object (readonly)
AREL object encapsulating table.
27 28 29 |
# File 'lib/data_doc/store.rb', line 27 def arel @arel end |
Instance Method Details
#datetime(name, opts = {}) ⇒ Object
Define a datetime field.
53 54 55 |
# File 'lib/data_doc/store.rb', line 53 def datetime(name, opts = {}) @connection.add_column(@arel.name, name, :datetime, opts) end |
#insert(record) ⇒ Object
Insert a row from a hash.
60 61 62 63 64 65 66 |
# File 'lib/data_doc/store.rb', line 60 def insert(record) return if @doc.read_only manager = @arel.insert_manager columns = record.keys.map { |k| @arel[k] } manager.insert(columns.zip(record.values)) @connection.insert(manager) end |
#integer(name, opts = {}) ⇒ Object
Define an integer field.
39 40 41 |
# File 'lib/data_doc/store.rb', line 39 def integer(name, opts = {}) @connection.add_column(@arel.name, name, :integer, opts) end |
#string(name, opts = {}) ⇒ Object
Define a string field.
32 33 34 |
# File 'lib/data_doc/store.rb', line 32 def string(name, opts = {}) @connection.add_column(@arel.name, name, :string, opts) end |
#text(name, opts = {}) ⇒ Object
Define a text field.
46 47 48 |
# File 'lib/data_doc/store.rb', line 46 def text(name, opts = {}) @connection.add_column(@arel.name, name, :text, opts) end |