Sha256: b061b1f6ed38b80ccca7e822655da8c4f12534885d4f5e3a0f08d71c1fa0c505

Contents?: true

Size: 969 Bytes

Versions: 7

Compression:

Stored size: 969 Bytes

Contents

module Fx
  # @api private
  class Definition
    def initialize(name:, version:, type: "function")
      @name = name
      @version = version.to_i
      @type = type
    end

    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

    def full_path
      Rails.root.join(path)
    end

    def path
      @_path ||= File.join("db", @type.pluralize, filename)
    end

    def version
      @version.to_s.rjust(2, "0")
    end

    private

    def filename
      @_filename ||= "#{@name}_v#{version}.sql"
    end

    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

    def migration_paths
      Rails.application.config.paths["db/migrate"].expanded
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
fx-0.8.0 lib/fx/definition.rb
fx-0.7.0 lib/fx/definition.rb
fx-0.6.2 lib/fx/definition.rb
fx-0.6.1 lib/fx/definition.rb
fx-0.6.0 lib/fx/definition.rb
fx-0.5.0 lib/fx/definition.rb
fx-0.4.0 lib/fx/definition.rb