Class: DataDoc::Store

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ Store

Create new.



155
156
157
158
# File 'lib/data_doc/store.rb', line 155

def initialize(doc)
  @doc = doc
  @connection = @doc.connection
end

Instance Attribute Details

#arelObject (readonly)

AREL object encapsulating table.



28
29
30
# File 'lib/data_doc/store.rb', line 28

def arel
  @arel
end

Class Method Details

.store(doc, table_name_or_sym, opts = {}, &blk) ⇒ Object

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
25
# File 'lib/data_doc/store.rb', line 20

def self.store(doc, table_name_or_sym, opts = {}, &blk) # :yields:
  s = Store.new(doc)
  s.create_store(table_name_or_sym, opts)
  s.instance_eval(&blk) unless blk.nil?
  s
end

Instance Method Details

#boolean(name, opts = {}) ⇒ Object

Define a boolean field.

store 'relation' do
  boolean 'field_name'
  boolean :another_field
end


110
111
112
# File 'lib/data_doc/store.rb', line 110

def boolean(name, opts = {})
  @connection.add_column(@arel.name, name, :boolean, opts)
end

#create_store(table_name_or_sym, opts = {}) ⇒ Object

Create and empty the store unless options prevent.



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/data_doc/store.rb', line 140

def create_store(table_name_or_sym, opts = {})
  table_name = table_name_or_sym.to_s 
  @arel = Arel::Table.new(table_name)
  unless @doc.read_only
    if @doc.data_only
      @connection.delete_sql("DELETE from #{table_name}")
    else
      @connection.create_table(table_name, opts.merge(force: true))
    end
  end
end

#date(name, opts = {}) ⇒ Object

Define a date field.

store 'relation' do
  date 'field_name'
  date :another_field
end


98
99
100
# File 'lib/data_doc/store.rb', line 98

def date(name, opts = {})
  @connection.add_column(@arel.name, name, :date, opts)
end

#datetime(name, opts = {}) ⇒ Object

Define a datetime field.

store 'relation' do
  datetime 'field_name'
  datetime :another_field
end


74
75
76
# File 'lib/data_doc/store.rb', line 74

def datetime(name, opts = {})
  @connection.add_column(@arel.name, name, :datetime, opts)
end

#float(name, opts = {}) ⇒ Object

Define a float field.

store 'relation' do
  float 'field_name'
  float :another_field
end


122
123
124
# File 'lib/data_doc/store.rb', line 122

def float(name, opts = {})
  @connection.add_column(@arel.name, name, :float, opts)
end

#insert(record) ⇒ Object

Insert a row from a hash.



129
130
131
132
133
134
135
# File 'lib/data_doc/store.rb', line 129

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.

store 'relation' do
  integer 'field_name', default: 42
  integer :another_field
end


50
51
52
# File 'lib/data_doc/store.rb', line 50

def integer(name, opts = {})
  @connection.add_column(@arel.name, name, :integer, opts)
end

#string(name, opts = {}) ⇒ Object

Define a string field.

store 'relation' do
  string 'field_name', default: 'value'
  string :another_field, null: false
end


38
39
40
# File 'lib/data_doc/store.rb', line 38

def string(name, opts = {})
  @connection.add_column(@arel.name, name, :string, opts)
end

#text(name, opts = {}) ⇒ Object

Define a text field.

store 'relation' do
  text 'field_name'
  text :another_field
end


62
63
64
# File 'lib/data_doc/store.rb', line 62

def text(name, opts = {})
  @connection.add_column(@arel.name, name, :text, opts)
end

#time(name, opts = {}) ⇒ Object

Define a datetime field.

store 'relation' do
  time 'field_name'
  time :another_field
end


86
87
88
# File 'lib/data_doc/store.rb', line 86

def time(name, opts = {})
  @connection.add_column(@arel.name, name, :time, opts)
end