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

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

Defined Under Namespace

Classes: 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.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 17

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
  @content = content
end

Instance Attribute Details

#dependency_enum_nameObject (readonly)

Returns the value of attribute dependency_enum_name.



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

def dependency_enum_name
  @dependency_enum_name
end

#dependency_function_nameObject (readonly)

Returns the value of attribute dependency_function_name.



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

def dependency_function_name
  @dependency_function_name
end

#dependency_schema_nameObject (readonly)

Returns the value of attribute dependency_schema_name.



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

def dependency_schema_name
  @dependency_schema_name
end

#dependency_table_nameObject (readonly)

Returns the value of attribute dependency_table_name.



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

def dependency_table_name
  @dependency_table_name
end

#migration_methodObject (readonly)

Returns the value of attribute migration_method.



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

def migration_method
  @migration_method
end

#object_nameObject (readonly)

Returns the value of attribute object_name.



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

def object_name
  @object_name
end

#schema_nameObject (readonly)

Returns the value of attribute schema_name.



8
9
10
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 8

def schema_name
  @schema_name
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



9
10
11
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 9

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.



82
83
84
85
86
87
88
89
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 82

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.



71
72
73
74
75
76
77
78
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 71

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)


54
55
56
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 54

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)


93
94
95
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 93

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



119
120
121
122
123
124
125
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 119

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



109
110
111
112
113
114
115
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 109

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



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

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.



60
61
62
63
64
65
66
67
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 60

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.



43
44
45
46
47
48
49
50
51
# File 'lib/dynamic_migrations/postgres/generator/fragment.rb', line 43

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