Class: DynamicMigrations::Postgres::Server::Database::Schema::Enum

Inherits:
DynamicMigrations::Postgres::Server::Database::Source show all
Defined in:
lib/dynamic_migrations/postgres/server/database/schema/enum.rb

Overview

This class represents a postgres enum.

Defined Under Namespace

Classes: ExpectedSchemaError, ExpectedValuesError, ValueAlreadyExistsError, ValueMustBeStringError

Instance Attribute Summary collapse

Attributes inherited from DynamicMigrations::Postgres::Server::Database::Source

#source

Instance Method Summary collapse

Methods inherited from DynamicMigrations::Postgres::Server::Database::Source

#assert_is_a_symbol!, #from_configuration?, #from_database?

Constructor Details

#initialize(source, schema, name, values, description: nil) ⇒ Enum

initialize a new object to represent a postgres enum



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 29

def initialize source, schema, name, values, description: nil
  super source

  @columns = []

  raise ExpectedSchemaError, schema unless schema.is_a? Schema
  @schema = schema

  raise ExpectedSymbolError, name unless name.is_a? Symbol
  @name = name

  unless values.is_a?(Array) && values.count > 0
    raise ExpectedValuesError, "Values are required for enums"
  end
  @values = []
  values.each do |value|
    add_value value
  end

  unless description.nil?
    raise ExpectedStringError, description unless description.is_a? String
    @description = description.strip
    @description = nil if description == ""
  end
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



26
27
28
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 26

def columns
  @columns
end

#descriptionObject (readonly)

Returns the value of attribute description.



25
26
27
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 25

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



23
24
25
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 23

def name
  @name
end

#schemaObject (readonly)

Returns the value of attribute schema.



22
23
24
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 22

def schema
  @schema
end

#valuesObject (readonly)

Returns the value of attribute values.



24
25
26
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 24

def values
  @values
end

Instance Method Details

#add_column(column) ⇒ Object

for tracking all the columns which are associated with this enum



73
74
75
76
77
78
79
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 73

def add_column column
  # this should never happen, but adding it just in case
  unless column.source == source
    raise "Internal error - column source `#{column.source}` does not match enum source `#{source}`"
  end
  @columns << column
end

#add_value(value) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 55

def add_value value
  unless value.is_a? String
    raise ValueMustBeStringError, "Value `#{value}` must be a string"
  end

  if @values.include? value
    raise ValueAlreadyExistsError, "Value `#{value}` already exists in enum `#{name}`"
  end

  @values << value
end

#differences_descriptions(other_enum) ⇒ Object



81
82
83
84
85
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 81

def differences_descriptions other_enum
  method_differences_descriptions other_enum, [
    :values
  ]
end

#full_nameObject



87
88
89
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 87

def full_name
  :"#{schema.name}.#{name}"
end

#has_description?Boolean

returns true if this enum has a description, otehrwise false

Returns:

  • (Boolean)


68
69
70
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 68

def has_description?
  !@description.nil?
end