class Fx::Definition

@api private

Public Class Methods

new(name:, version:, type: "function") click to toggle source
# File lib/fx/definition.rb, line 4
def initialize(name:, version:, type: "function")
  @name = name
  @version = version.to_i
  @type = type
end

Public Instance Methods

full_path() click to toggle source
# File lib/fx/definition.rb, line 18
def full_path
  Rails.root.join(path)
end
path() click to toggle source
# File lib/fx/definition.rb, line 22
def path
  @_path ||= File.join("db", @type.pluralize, filename)
end
to_sql() click to toggle source
# File lib/fx/definition.rb, line 10
def to_sql
  File.read(find_file || full_path).tap do |content|
    if content.empty?
      raise "Define #{@type} in #{path} before migrating."
    end
  end
end
version() click to toggle source
# File lib/fx/definition.rb, line 26
def version
  @version.to_s.rjust(2, "0")
end

Private Instance Methods

filename() click to toggle source
# File lib/fx/definition.rb, line 32
def filename
  @_filename ||= "#{@name}_v#{version}.sql"
end
find_file() click to toggle source
# File lib/fx/definition.rb, line 36
def find_file
  migration_paths.lazy
    .map { |migration_path| File.expand_path(File.join("..", "..", path), migration_path) }
    .find { |definition_path| File.exist?(definition_path) }
end
migration_paths() click to toggle source
# File lib/fx/definition.rb, line 42
def migration_paths
  Rails.application.config.paths["db/migrate"].expanded
end