Sha256: e457c1ec9511dc3cd88a707e0250bda7d841b3e8bb688b541f6c15ee4a03d905

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

# This method manipulates the given path and tries to find any migration which
# matches the migration name. For example, the call above is converted to:
#
#   generator.should generate_migration "db/migrate/003_create_products.rb"
#
# Consequently it accepts the same arguments as the matcher have_file.

module RSpec
  module FileMatchers
    class GenerateMigration
      include RSpec::Rails::Migration

      attr_reader :name

      def initialize(name)
        @name = name      
      end

      def matches?(generator)
        expected = migration_file_name(name, generator)
        if block_given? && expected
          read = File.read(expected)
          ruby_content = read.extend(RSpec::RubyContent::Helpers)
          yield ruby_content
        else
          expected
        end
      end          
    
      def failure_message
        "Expected migration #{name} to have been generated, but it was not"
      end

      def negative_failure_message
        "Did not expect migration #{name} to have been generated, but it was"
      end
      
      protected
      
      def migration_file_name(relative, generator) #:nodoc:
        dirname = migration_dir
        file_name = relative.sub(/\.rb$/, '')
        migrations = Dir.glob("#{dirname}/[0-9]*_*.rb")
        if !migrations.empty? 
          migrations.grep(/\d+_#{file_name}\.rb$/).first 
        else 
          false
        end
      end
      
    end
  
    def generate_migration(name)
      GenerateMigration.new(name)
    end

  end   
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
generator-spec-0.4.0 lib/rspec_for_generators/matchers/file/generate_migration.rb
generator-spec-0.3.5 lib/rspec_for_generators/matchers/file/generate_migration.rb
generator-spec-0.3.4 lib/rspec_for_generators/matchers/file/generate_migration.rb