Class: Dumbo::EnumType

Inherits:
Type show all
Defined in:
lib/dumbo/enum_type.rb

Instance Attribute Summary collapse

Attributes inherited from Type

#name, #type, #typrelid

Attributes inherited from PgObject

#oid

Instance Method Summary collapse

Methods inherited from Type

#drop, #get

Methods inherited from PgObject

#downgrade, #execute, #get, identfied_by, #identify, #initialize, #upgrade

Constructor Details

This class inherits a constructor from Dumbo::PgObject

Instance Attribute Details

#labelsObject

Returns the value of attribute labels.



3
4
5
# File 'lib/dumbo/enum_type.rb', line 3

def labels
  @labels
end

Instance Method Details

#load_attributesObject



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/dumbo/enum_type.rb', line 5

def load_attributes
  super

  res = execute <<-SQL
      SELECT enumlabel
      FROM pg_enum
      WHERE enumtypid = #{oid}
      ORDER by enumsortorder
    SQL
  @labels = res.to_a.map { |r| r['enumlabel'] }
end

#to_sqlObject



17
18
19
20
21
22
23
24
25
# File 'lib/dumbo/enum_type.rb', line 17

def to_sql
    lbl_str = labels.map { |l| "'" + l + "'" }.join(",\n  ")

    <<-SQL.gsub(/^ {6}/, '')
    CREATE TYPE #{name} AS ENUM (
      #{lbl_str}
    );
    SQL
end