Sha256: 85919f554d4de8b6c0cfd7249c26522f9db0c833ddd9df02fee42f103f32bdd2

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

require 'hanami/commands/generate/abstract'
require 'hanami/utils/file_list'

module Hanami
  module Commands
    class Generate
      class Migration < Abstract

        attr_reader :name, :underscored_name

        # @since 0.6.0
        # @api private
        #
        # @example
        #   20150612160502
        TIMESTAMP_FORMAT = '%Y%m%d%H%M%S'.freeze

        # @since 0.6.0
        # @api private
        #
        # @example
        #   20150612160502_create_books.rb
        FILENAME_PATTERN = '%{timestamp}_%{name}.rb'.freeze

        def initialize(options, name)
          super(options)

          @name = name
          @underscored_name = Utils::String.new(@name).underscore

          Components.resolve('model.configuration')
          assert_migration_name!
        end

        def map_templates
          add_mapping('migration.rb.tt', destination_path)
        end

        private

        def destination_path
          existing_migration_path || new_migration_path
        end

        def existing_migration_path
          Utils::FileList["#{Hanami::Model.configuration.migrations}/[0-9]*_#{underscored_name}.rb"].first
        end

        def new_migration_path
          timestamp = Time.now.utc.strftime(TIMESTAMP_FORMAT)
          filename = FILENAME_PATTERN % { timestamp: timestamp, name: underscored_name}

          Hanami::Model.configuration.migrations.join(filename)
        end

        def assert_migration_name!
          if argument_blank?(name)
            raise ArgumentError.new('Migration name is missing')
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hanami-1.0.0.beta2 lib/hanami/commands/generate/migration.rb
hanami-1.0.0.beta1 lib/hanami/commands/generate/migration.rb
hanami-0.9.2 lib/hanami/commands/generate/migration.rb
hanami-0.9.1 lib/hanami/commands/generate/migration.rb
hanami-0.9.0 lib/hanami/commands/generate/migration.rb