Sha256: c2ef3f10afd2e9cf15eb59b4d87874aabb8c966db0090d3e8dfac869de8d5f71

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 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('.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(/spec\/|_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.1.7 lib/dev_scripts/scripts/open_spec_file.rb
dev_scripts-0.1.6 lib/dev_scripts/scripts/open_spec_file.rb
dev_scripts-0.1.5 lib/dev_scripts/scripts/open_spec_file.rb