Sha256: 035ac460fefeb8e49b6f521e58a5863ecf52d9921d0e1a5532f3a532cf9002c5

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module MinceMigrator
  module Migrations
    class Loader
      def initialize(options)
        @klass_name = options[:klass_name]
        @full_path = options[:full_path]
      end

      def klass
        eval "::MinceMigrator::Migrations::#{@klass_name}"
      end

      def call
        if valid?
          require @full_path
          perform_post_load_validations
        end
      end

      def valid?
        file_exists?
      end

      private

      def perform_post_load_validations
        mince_migration? && has_valid_interface?
      end

      def mince_migration?
        klass # Check that constant is valid and loaded
      rescue NameError
        raise 'invalid migration'
      end

      def has_valid_interface?
        if !has_all_interfaces?
          raise "migration does not have all required methods (:run, :revert, and :time_created)"
        end
      end

      def has_all_interfaces?
        %w(run revert time_created).all?{|a| klass.respond_to?(a) }
      end

      def file_exists?
        raise 'migration does not exist' unless ::File.exists?(@full_path)
        true
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mince_migrator-1.0.2 lib/mince_migrator/migrations/loader.rb
mince_migrator-1.0.1 lib/mince_migrator/migrations/loader.rb
mince_migrator-1.0.0 lib/mince_migrator/migrations/loader.rb