Class: DynamicMigrations::Postgres::Generator::Fragment

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_migrations/postgres/generator/fragment.rb

Defined Under Namespace

Classes: ContentRequiredError, InvalidNameError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_name, table_name, migration_method, object_name, code_comment, content) ⇒ Fragment

Returns a new instance of Fragment.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 20

def initialize schema_name, table_name, migration_method, object_name, code_comment, content
  valid_name_regex = /\A[a-z][a-z0-9]*(_[a-z0-9]+)*\z/

  unless schema_name.nil? || (schema_name.to_s.match valid_name_regex)
    raise InvalidNameError, "Invalid schema name `#{schema_name}`, must only be lowercase letters, numbers and underscores"
  end
  @schema_name = schema_name

  unless table_name.nil? || (table_name.to_s.match valid_name_regex)
    raise InvalidNameError, "Invalid table name `#{table_name}`, must only be lowercase letters, numbers and underscores"
  end
  @table_name = table_name

  unless object_name.to_s.match valid_name_regex
    raise InvalidNameError, "Invalid object name `#{object_name}`, must only be lowercase letters, numbers and underscores"
  end
  @object_name = object_name

  @migration_method = migration_method
  @code_comment = code_comment&.freeze

  if content.nil?
    raise ContentRequiredError, "Content is required for a fragment"
  end
  @content = content.freeze
end

Instance Attribute Details

#dependency_enum_nameObject (readonly)

Returns the value of attribute dependency_enum_name.



18
19
20
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 18

def dependency_enum_name
  @dependency_enum_name
end

#dependency_function_nameObject (readonly)

Returns the value of attribute dependency_function_name.



17
18
19
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 17

def dependency_function_name
  @dependency_function_name
end

#dependency_schema_nameObject (readonly)

Returns the value of attribute dependency_schema_name.



15
16
17
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 15

def dependency_schema_name
  @dependency_schema_name
end

#dependency_table_nameObject (readonly)

Returns the value of attribute dependency_table_name.



16
17
18
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 16

def dependency_table_name
  @dependency_table_name
end

#migration_methodObject (readonly)

Returns the value of attribute migration_method.



13
14
15
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 13

def migration_method
  @migration_method
end

#object_nameObject (readonly)

Returns the value of attribute object_name.



14
15
16
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 14

def object_name
  @object_name
end

#schema_nameObject (readonly)

Returns the value of attribute schema_name.



11
12
13
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 11

def schema_name
  @schema_name
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



12
13
14
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 12

def table_name
  @table_name
end

Instance Method Details

#enum_dependencyObject

If an enum dependency has been set, then returns a hash with the schema_name and enum_name, otherwise returns nil.



89
90
91
92
93
94
95
96
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 89

def enum_dependency
  if dependency_schema_name && dependency_enum_name
    {
      schema_name: dependency_schema_name,
      enum_name: dependency_enum_name
    }
  end
end

#function_dependencyObject

If a function dependency has been set, then returns a hash with the schema_name and function_name, otherwise returns nil.



78
79
80
81
82
83
84
85
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 78

def function_dependency
  if dependency_schema_name && dependency_function_name
    {
      schema_name: dependency_schema_name,
      function_name: dependency_function_name
    }
  end
end

#has_code_comment?Boolean

Returns true if the fragment has a code comment, otherwise false.

Returns:

  • (Boolean)


61
62
63
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 61

def has_code_comment?
  !@code_comment.nil?
end

#is_dependent_on_table?(schema_name, table_name) ⇒ Boolean

returns true if the fragment has a table dependency, and the dependency matches the provided schema_name and table_name, otherwise returns false.

Returns:

  • (Boolean)


100
101
102
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 100

def is_dependent_on_table? schema_name, table_name
  dependency_schema_name && schema_name == dependency_schema_name && table_name == dependency_table_name || false
end

#set_dependent_enum(schema_name, enum_name) ⇒ Object

Set an enum dependency for this fragment. Takes a schema name and enum name



126
127
128
129
130
131
132
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 126

def set_dependent_enum schema_name, enum_name
  if @dependency_schema_name
    raise "Cannot set a table dependency for a fragment that already has a #{dependency_type} dependency"
  end
  @dependency_schema_name = schema_name
  @dependency_enum_name = enum_name
end

#set_dependent_function(schema_name, function_name) ⇒ Object

Set a function dependency for this fragment. Takes a schema name and function name



116
117
118
119
120
121
122
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 116

def set_dependent_function schema_name, function_name
  if @dependency_schema_name
    raise "Cannot set a table dependency for a fragment that already has a #{dependency_type} dependency"
  end
  @dependency_schema_name = schema_name
  @dependency_function_name = function_name
end

#set_dependent_table(schema_name, table_name) ⇒ Object

Set a table dependency for this fragment. Takes a schema name and table name



106
107
108
109
110
111
112
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 106

def set_dependent_table schema_name, table_name
  if @dependency_schema_name
    raise "Cannot set a table dependency for a fragment that already has a #{dependency_type} dependency"
  end
  @dependency_schema_name = schema_name
  @dependency_table_name = table_name
end

#table_dependencyObject

If a table dependency has been set, then returns a hash with the schema_name and table_name, otherwise returns nil.



67
68
69
70
71
72
73
74
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 67

def table_dependency
  if dependency_schema_name && dependency_table_name
    {
      schema_name: dependency_schema_name,
      table_name: dependency_table_name
    }
  end
end

#to_sObject

Returns a string representation of the fragment for use in the final migration. This final string is a combination of the code_comment (if present) and the content of the fragment.



50
51
52
53
54
55
56
57
58
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 50

def to_s
  strings = []
  comment = @code_comment
  unless comment.nil?
    strings << "# " + comment.split("\n").join("\n# ")
  end
  strings << @content
  strings.join("\n").strip
end