Sha256: f0188b73add8ab2b41a072c436e684d45fc47d7d018b17e95c909d4156a4114d

Contents?: true

Size: 1.8 KB

Versions: 3

Compression:

Stored size: 1.8 KB

Contents

# Asserts a given file exists. You need to supply an absolute path or a path relative
# to the configured destination:
#
#   generator.should have_generated_file "app/controller/products_controller.rb" 
#            

module RSpec::FileMatchers
  class GenerateDir
    attr_accessor :relative_path

    def initialize(relative_path, type = nil)
      @relative_path = relative_rails_dir(relative_path, type)
    end

    def matches?(generator, &block)              
      file = File.expand_path(relative_path, generator.class.destination_root)
      file_exists = File.exists?(expected)
      if block && file_exists
        read = File.read(file)
        ruby_content = read.extend(RSpec::RubyContent::Helpers)
        yield ruby_content
      else
        file_exists
      end        
    end          
  
    def failure_message
      "Expected file #{relative_path} to have been generated, but it was not"
    end

    def negative_failure_message
      "Did not expected file #{relative_path} to have been generated, but it was"
    end
    
    protected
    
    def relative_rails_dir path, type = nil                             
      path = path.to_s
      return File.join(::Rails.root, base_dir(type), folder(type), "#{path}") if type        
      File.join(::Rails.root, path)
    end

    def folder type
      case type
      when :observer
        'models'
      else
        type.to_s.pluralize
      end
    end      
    
    def base_dir type
      case type
      when :model, :controller, :view, :helper, :observer
        'app'
      when :migration
        'db'
      when :javascript, :stylesheet
        'public'
      when :initializer, :locale
        'config'
      else
        ''          
      end
    end
  end

  def generate_dir(relative, type = nil)
    GenerateDir.new(relative, type)
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
generator-spec-0.4.3 lib/rspec_for_generators/matchers/file/generate_directory.rb
generator-spec-0.4.2 lib/rspec_for_generators/matchers/file/generate_directory.rb
generator-spec-0.4.1 lib/rspec_for_generators/matchers/file/generate_directory.rb