Class: DynamicMigrations::Postgres::Server::Database::Schema::Enum
- Inherits:
-
DynamicMigrations::Postgres::Server::Database::Source
- Object
- DynamicMigrations::Postgres::Server::Database::Source
- DynamicMigrations::Postgres::Server::Database::Schema::Enum
- Defined in:
- lib/dynamic_migrations/postgres/server/database/schema/enum.rb
Overview
This class represents a postgres enum.
Defined Under Namespace
Classes: EnumValueTooLongError, ExpectedSchemaError, ExpectedValuesError, ValueAlreadyExistsError, ValueMustBeStringError
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Attributes inherited from DynamicMigrations::Postgres::Server::Database::Source
Instance Method Summary collapse
-
#add_column(column) ⇒ Object
for tracking all the columns which are associated with this enum.
- #add_value(value) ⇒ Object
- #differences_descriptions(other_enum) ⇒ Object
- #full_name ⇒ Object
-
#has_description? ⇒ Boolean
returns true if this enum has a description, otehrwise false.
-
#initialize(source, schema, name, values, description: nil) ⇒ Enum
constructor
initialize a new object to represent a postgres enum.
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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 32 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
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
29 30 31 |
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 29 def columns @columns end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
28 29 30 |
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 28 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
26 27 28 |
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 26 def name @name end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
25 26 27 |
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 25 def schema @schema end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
27 28 29 |
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 27 def values @values end |
Instance Method Details
#add_column(column) ⇒ Object
for tracking all the columns which are associated with this enum
80 81 82 83 84 85 86 |
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 80 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 58 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 if value.length > 63 raise EnumValueTooLongError, "Value `#{value}` must be less than 64 characters" end @values << value end |
#differences_descriptions(other_enum) ⇒ Object
88 89 90 91 92 |
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 88 def differences_descriptions other_enum method_differences_descriptions other_enum, [ :values ] end |
#full_name ⇒ Object
94 95 96 |
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 94 def full_name :"#{schema.name}.#{name}" end |
#has_description? ⇒ Boolean
returns true if this enum has a description, otehrwise false
75 76 77 |
# File 'lib/dynamic_migrations/postgres/server/database/schema/enum.rb', line 75 def has_description? !@description.nil? end |