Sha256: c2bcd41faab1222ee86010dbb82870210088a1ead95ed409cf39b42c3351dd20

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 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.to_s
      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
    alias_method :have_migration, :generate_migration  
  end   
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
generator-spec-0.4.5 lib/generator_spec/matchers/file/generate_migration.rb
generator-spec-0.4.4 lib/generator_spec/matchers/file/generate_migration.rb