Class: Perpetuity::Postgres::Table::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/perpetuity/postgres/table/attribute.rb

Constant Summary collapse

NoDefaultValue =
Module.new
UUID =
Module.new
SQL_TYPE_MAP =
{
  String => 'TEXT',
  Integer => 'BIGINT',
  Fixnum =>  'BIGINT',
  Bignum =>     'NUMERIC',
  BigDecimal => 'NUMERIC',
  Float => 'FLOAT',
  UUID => 'UUID',
  Time => 'TIMESTAMPTZ',
  TrueClass => 'BOOLEAN',
  FalseClass => 'BOOLEAN'
}.tap{|m| m.default = 'JSON' }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options = {}) ⇒ Attribute

Returns a new instance of Attribute.



25
26
27
28
29
30
31
# File 'lib/perpetuity/postgres/table/attribute.rb', line 25

def initialize name, type, options={}
  @name = name
  @type = type
  @max_length = options[:max_length]
  @primary_key = options.fetch(:primary_key) { false }
  @default = options.fetch(:default) { NoDefaultValue }
end

Instance Attribute Details

#max_lengthObject (readonly)

Returns the value of attribute max_length.



7
8
9
# File 'lib/perpetuity/postgres/table/attribute.rb', line 7

def max_length
  @max_length
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/perpetuity/postgres/table/attribute.rb', line 7

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/perpetuity/postgres/table/attribute.rb', line 7

def type
  @type
end

Instance Method Details

#defaultObject



55
56
57
# File 'lib/perpetuity/postgres/table/attribute.rb', line 55

def default
  @default
end

#primary_key?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/perpetuity/postgres/table/attribute.rb', line 51

def primary_key?
  !!@primary_key
end

#sql_declarationObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/perpetuity/postgres/table/attribute.rb', line 37

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_typeObject



33
34
35
# File 'lib/perpetuity/postgres/table/attribute.rb', line 33

def sql_type
  SQL_TYPE_MAP[type]
end