class Perpetuity::Postgres::Table::Attribute

Constants

NoDefaultValue
SQL_TYPE_MAP
UUID

Attributes

max_length[R]
name[R]
type[R]

Public Class Methods

new(name, type, options={}) click to toggle source
# File lib/perpetuity/postgres/table/attribute.rb, line 26
def initialize name, type, options={}
  @name = name
  @type = type
  @max_length = options[:max_length]
  @primary_key = if @name.to_s == 'id'
                   true
                 else
                   options.fetch(:primary_key) { false }
                 end
  @default = options.fetch(:default) { NoDefaultValue }
end

Public Instance Methods

default() click to toggle source
# File lib/perpetuity/postgres/table/attribute.rb, line 60
def default
  @default
end
primary_key?() click to toggle source
# File lib/perpetuity/postgres/table/attribute.rb, line 56
def primary_key?
  !!@primary_key
end
sql_declaration() click to toggle source
# File lib/perpetuity/postgres/table/attribute.rb, line 42
def sql_declaration
  if self.default.is_a? String
    default = "'#{self.default}'"
  else
    default = self.default
  end

  sql = "#{name} #{sql_type}"
  sql << ' PRIMARY KEY' if primary_key?
  sql << " DEFAULT #{default}" unless self.default == NoDefaultValue

  sql
end
sql_type() click to toggle source
# File lib/perpetuity/postgres/table/attribute.rb, line 38
def sql_type
  SQL_TYPE_MAP[type]
end