Class: PgKingdom::Table
- Inherits:
-
Object
- Object
- PgKingdom::Table
- Defined in:
- lib/pgkingdom/table.rb
Overview
Instance Attribute Summary collapse
-
#name ⇒ String|Symbol
readonly
Returns table name.
-
#schema ⇒ String|Symbol
readonly
Returns schema name.
Instance Method Summary collapse
-
#column_defined?(name) ⇒ Boolean
Helper, checks if column was defined in Table.
-
#column_exists?(name, type = nil) ⇒ String
Generates SQL query to check if column exists in table.
-
#create(safe = true) ⇒ String
Generates SQL to create table in the database.
-
#create! ⇒ String
Helper, which calls create with safe=false.
-
#drop(safe = true) ⇒ String
Generates SQL to drop table in the database.
-
#drop! ⇒ String
Helper, which calls drop with safe=false.
-
#initialize(name, &attr_block) ⇒ Postruby::Table
constructor
Defines new Table with a given block.
-
#method_missing(type, *opts) ⇒ Object
Acts as DSL to define table columns.
-
#table_exists? ⇒ String
Generates SQL to check if table exists in the database.
-
#table_path ⇒ String
Helper, which returns table path
. .
Constructor Details
#initialize(name, &attr_block) ⇒ Postruby::Table
Defines new Table with a given block.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pgkingdom/table.rb', line 20 def initialize(name, &attr_block) if attr_block.nil? raise SyntaxError.new "Table columns should be given within a block." end if name.is_a? Array @schema = name.first @name = name.last else @schema = :public @name = name end @columns = [] @columns.extend ColumnsHelper instance_eval &attr_block unless @columns.any? raise SyntaxError.new "Given block should not be empty." end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(type, *opts) ⇒ Object
Acts as DSL to define table columns.
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/pgkingdom/table.rb', line 45 def method_missing(type, *opts) unless @columns.available? type raise "Unsupported column datatype #{type} for table #{@name}." end col_name = opts.shift if @columns.defined? col_name raise "Column #{col_name} with type #{type} has been redeclared for table #{@name}." end @columns.add col_name, type, opts end |
Instance Attribute Details
#name ⇒ String|Symbol (readonly)
Returns table name.
15 16 17 |
# File 'lib/pgkingdom/table.rb', line 15 def name @name end |
#schema ⇒ String|Symbol (readonly)
Returns schema name.
11 12 13 |
# File 'lib/pgkingdom/table.rb', line 11 def schema @schema end |
Instance Method Details
#column_defined?(name) ⇒ Boolean
Helper, checks if column was defined in Table.
95 |
# File 'lib/pgkingdom/table.rb', line 95 def column_defined?(name) @columns.defined? name end |
#column_exists?(name, type = nil) ⇒ String
Generates SQL query to check if column exists in table.
99 100 101 |
# File 'lib/pgkingdom/table.rb', line 99 def column_exists?(name, type=nil) # TODO: write pgSQL query end |
#create(safe = true) ⇒ String
Generates SQL to create table in the database. When safe is true and table already exists it allows to skip this action.
66 67 68 69 70 71 72 73 |
# File 'lib/pgkingdom/table.rb', line 66 def create(safe=true) allow_skip = safe ? " IF NOT EXISTS" : "" <<~SQL CREATE TABLE#{allow_skip} #{table_path} ( #{@columns.join} ); SQL end |
#create! ⇒ String
Helper, which calls create with safe=false.
87 |
# File 'lib/pgkingdom/table.rb', line 87 def create!; create(false) end |
#drop(safe = true) ⇒ String
Generates SQL to drop table in the database. When safe is true and table doesn't exist it allows to skip this action.
79 80 81 82 83 |
# File 'lib/pgkingdom/table.rb', line 79 def drop(safe=true) <<~SQL DROP TABLE#{safe ? " IF EXISTS" : ""} #{table_path}; SQL end |
#drop! ⇒ String
Helper, which calls drop with safe=false.
91 |
# File 'lib/pgkingdom/table.rb', line 91 def drop!; drop(false) end |
#table_exists? ⇒ String
Generates SQL to check if table exists in the database.
105 106 107 |
# File 'lib/pgkingdom/table.rb', line 105 def table_exists? "SELECT to_regclass('#{table_path}') IS NOT NULL AS table_exists;" end |
#table_path ⇒ String
Helper, which returns table path
60 |
# File 'lib/pgkingdom/table.rb', line 60 def table_path; "#{@schema}.#{@name}" end |