Sha256: b015920ce91b4ff722f48923ca7a4890ad4ecf02b611ac851718553e2820a8eb

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

require 'dev_scripts/script'

class AlreadyInASpecFileError < StandardError
end

ALREADY_IN_SPEC_FILE_MESSAGE = 'Already in Spec File'

DevScripts::Script.define_script :open_spec_file do
  args :file_path 

  def file_exists?
    !found_file_path.nil?
  end

  def already_in_a_spec_file?
    !(file_path =~ /_spec.rb\Z/).nil?
  end

  let(:file_path_without_extension) do
    file_path
      .gsub(/\A\w+\//, '')
      .gsub('.rb', '')
  end

  let(:found_file_path) do
    Dir['./spec/**/*.rb'].find do |path|
      !(path =~ /#{file_path_without_extension}_spec.rb/).nil?
    end
  end

  let(:spec_file_path) do
    if file_exists?
      found_file_path
    else
      "spec/#{file_path_without_extension}_spec.rb"
    end
  end

  let(:constant_name) do
    require 'active_support/inflector'

    spec_file_path
      .gsub(/\Aspec\/|_spec\.rb/, '')
      .split('/')
      .map(&:camelize)
      .join('::')
  end

  execute do
    begin
      raise AlreadyInASpecFileError if already_in_a_spec_file?

    if file_exists?
      print_message 'file already exists, opening file'
    else
      print_message 'file does not exist, writing a new file'

      create_file_in_editor spec_file_path do
        <<-RUBY
RSpec.describe #{constant_name} do
end
        RUBY
      end
    end

    open_file_in_editor spec_file_path

    rescue AlreadyInASpecFileError
      print_message 'Already in Spec File'
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dev_scripts-0.2.1 lib/dev_scripts/scripts/open_spec_file.rb
dev_scripts-0.2.0 lib/dev_scripts/scripts/open_spec_file.rb
dev_scripts-0.1.9 lib/dev_scripts/scripts/open_spec_file.rb