class Specific::Command::Feature

Attributes

file_name[R]
group_name[R]
spec[R]
title[R]

Public Class Methods

new(spec, group_name, file_name, title) click to toggle source
# File lib/specific/command/feature.rb, line 6
def initialize(spec, group_name, file_name, title)
  @spec = spec
  @group_name = group_name
  @file_name = file_name
  @title = title
end

Public Instance Methods

run() click to toggle source
# File lib/specific/command/feature.rb, line 13
def run
  mk_group_dir
  write_feature_template
end

Private Instance Methods

feature_file_path() click to toggle source
# File lib/specific/command/feature.rb, line 33
def feature_file_path
  File.join(group_dir_path, file_name + ".feature")
end
group_dir_path() click to toggle source
# File lib/specific/command/feature.rb, line 28
def group_dir_path
  dir_name = sanitized_group_name
  File.join spec.base_path, dir_name
end
mk_group_dir() click to toggle source
# File lib/specific/command/feature.rb, line 20
def mk_group_dir
  FileUtils.mkdir_p group_dir_path
end
next_id() click to toggle source
# File lib/specific/command/feature.rb, line 73
def next_id
  spec.next_id
end
printable_feature_file_path() click to toggle source
# File lib/specific/command/feature.rb, line 77
def printable_feature_file_path
  feature_file_path.gsub(/^#{Dir.pwd}\//, '')
end
sanitized_group_name() click to toggle source
# File lib/specific/command/feature.rb, line 24
def sanitized_group_name
  group_name.gsub(' ', '-').downcase
end
write_feature_template() click to toggle source
# File lib/specific/command/feature.rb, line 37
      def write_feature_template
        template = <<-EOT
          @id_[ID] @group_[GROUP]
          Feature: [NAME]
            In Order
            As
            I

            Scenario:
              Given
              When
              Then
        EOT
        template_intendation = template.match(/^(\s*)/)[1]
        template.gsub!(/^#{template_intendation}/, '')

        subs = {
          'ID' => next_id,
          'GROUP' => sanitized_group_name,
          'NAME' => title,
        }

        subs.each do |key, value|
          template.gsub!(/\[#{key}\]/, value.to_s)
        end

        if File.exist?(feature_file_path)
          raise "File #{printable_feature_file_path} already exists"
        end

        File.open(feature_file_path, 'w')do |f|
          f << template
        end
        puts "File #{printable_feature_file_path} created"
      end