Sha256: 63462740cc996c44312ce77e2fea62ed94a39b3f459c33a5ef8f7693adc0290f

Contents?: true

Size: 1.99 KB

Versions: 7

Compression:

Stored size: 1.99 KB

Contents

require "screw_server/base"
require "screw_server/fixture_file"

module ScrewServer
  class SpecFile
    def self.all
      Dir.glob(File.join(Base.spec_base_dir, "*_spec.js")).sort.map do |file|
        SpecFile.new(file.gsub("#{Base.spec_base_dir}/", "").gsub("_spec.js", ""))
      end
    end

    def self.latest
      SpecFile.all.sort{ |a, b| a.last_changed <=> b.last_changed }.last
    end

    attr_reader :name

    def initialize(name)
      @name = name
    end

    def filename
      File.join(Base.spec_base_dir, full_name)
    end

    def full_name
      name + "_spec.js"
    end

    def used_fixtures
      scan_for_statement("use_fixture", filename).map {|name| FixtureFile.new(name) }
    end

    def fixture_hash
      used_fixtures.inject({}) do |result, fixture|
        fixture_content = File.read(fixture.filename) rescue ""
        result.merge(fixture.name => fixture_content)
      end
    end

    def required_scripts
      SpecFile.spec_helper_files.map { |file| required_files_in(file) }.flatten +
        required_files_in(filename)
    end

    def last_dependency_change
      used_files.map do |file|
        File.mtime(file).to_i rescue 0
      end.max
    end

    def last_changed
      @last_changed ||= File.mtime(filename).to_i
    end

    def used_files
      [filename] +
        SpecFile.spec_helper_files +
        used_fixtures.map(&:filename) +
        required_scripts.map {|script| File.join(Base.code_base_dir, script)}
    end

    def self.spec_helper_files
      Dir.glob(Base.spec_base_dir + "spec_helper.js") +
        Dir.glob(Base.spec_base_dir + "support/**/*.js")
    end

    protected

    def scan_for_statement(statement, filename)
      File.read(filename).scan(/#{statement}\(["']\/?(.+)['"]\)/).map { |groups| groups[0] }
    end

    def required_files_in(filename)
      files = scan_for_statement("require", filename)
      files = files.reject { |file| file == "spec_helper.js" }
      files = files.map { |file| file.gsub("../../public", "") }
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
screw_server-0.1.14 lib/screw_server/spec_file.rb
screw_server-0.1.13 lib/screw_server/spec_file.rb
screw_server-0.1.12 lib/screw_server/spec_file.rb
screw_server-0.1.11 lib/screw_server/spec_file.rb
screw_server-0.1.10 lib/screw_server/spec_file.rb
screw_server-0.1.9 lib/screw_server/spec_file.rb
screw_server-0.1.8 lib/screw_server/spec_file.rb