Class: Heresy::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/heresy/schema.rb,
lib/heresy/schema/column.rb,
lib/heresy/schema/fields.rb

Overview

This class is responsible for defining and creating the table used to store model data.

Defined Under Namespace

Classes: Column, Fields

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, name) ⇒ Schema

Returns a new instance of Schema.



7
8
9
10
11
12
13
14
# File 'lib/heresy/schema.rb', line 7

def initialize(db, name)
  @db         = db
  @name       = name
  @id         = Column.new(:id,         :int,       :size => 11, :unsigned => true)
  @uuid       = Column.new(:uuid,       :uuid,      :unique => true)
  @updated_at = Column.new(:updated_at, :timestamp, :default => 'CURRENT_TIMESTAMP'.lit, :index => true)
  @body       = Column.new(:body,       :blob)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/heresy/schema.rb', line 5

def body
  @body
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/heresy/schema.rb', line 5

def id
  @id
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/heresy/schema.rb', line 4

def name
  @name
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



5
6
7
# File 'lib/heresy/schema.rb', line 5

def updated_at
  @updated_at
end

#uuidObject (readonly)

Returns the value of attribute uuid.



5
6
7
# File 'lib/heresy/schema.rb', line 5

def uuid
  @uuid
end

Instance Method Details

#createObject

Creates the model table.



22
23
24
# File 'lib/heresy/schema.rb', line 22

def create
  to_sql.each { |sql| @db.execute_ddl(sql) }
end

#dropObject

Drops the model table.



27
28
29
# File 'lib/heresy/schema.rb', line 27

def drop
  @db.drop_table @name
end

#exists?Boolean

Checks whether the model table exists or not.

Returns:

  • (Boolean)


17
18
19
# File 'lib/heresy/schema.rb', line 17

def exists?
  @db.table_exists?(@name)
end

#inspectObject



36
37
38
# File 'lib/heresy/schema.rb', line 36

def inspect
  "<Heresy::Schema:#{@name}>"
end

#to_sqlObject

Returns an array of the SQL statements used to create the table and any indexes.



32
33
34
# File 'lib/heresy/schema.rb', line 32

def to_sql
  @db.create_table_sql_list @name, *generate.create_info
end