class Spinach::Generators::FeatureGenerator

A feature generator generates and/or writes an example feature steps class given the parsed feture data

Attributes

feature[R]

Public Class Methods

new(feature) click to toggle source

@param [Feature] feature

The feature returned from the {Parser}
# File lib/spinach/generators/feature_generator.rb, line 13
def initialize(feature)
  @feature = feature
end

Public Instance Methods

filename() click to toggle source

@return [String]

an example filename for this feature steps
# File lib/spinach/generators/feature_generator.rb, line 51
def filename
  Spinach::Support.underscore(
    Spinach::Support.camelize name
  ) + ".rb"
end
filename_with_path() click to toggle source

@return [String]

the expanded path where this feature steps may be saved
# File lib/spinach/generators/feature_generator.rb, line 65
def filename_with_path
  File.expand_path File.join(path, filename)
end
generate() click to toggle source

@return [String]

an example feature steps definition
# File lib/spinach/generators/feature_generator.rb, line 35
def generate
  result = StringIO.new
  result.puts "class #{Spinach::Support.scoped_camelize name} < Spinach::FeatureSteps"
  generated_steps = steps.map do |step|
    step_generator = Generators::StepGenerator.new(step)
    step_generator.generate.split("\n").map do |line|
      "  #{line}"
    end.join("\n")
  end
  result.puts generated_steps.join("\n\n")
  result.puts "end"
  result.string
end
name() click to toggle source

@return [String]

this feature's name
# File lib/spinach/generators/feature_generator.rb, line 29
def name
  @feature.name
end
path() click to toggle source

@return [String]

the path where this feature steps may be saved
# File lib/spinach/generators/feature_generator.rb, line 59
def path
  Spinach.config[:step_definitions_path]
end
steps() click to toggle source

@return [Array<Hash>]

an array of unique steps found in this feature, avoiding name
repetition
# File lib/spinach/generators/feature_generator.rb, line 20
def steps
  scenario_steps   = @feature.scenarios.map(&:steps).flatten
  background_steps = @feature.background_steps

  (scenario_steps + background_steps).uniq(&:name)
end
store() click to toggle source

Stores the example feature steps definition into an expected path

# File lib/spinach/generators/feature_generator.rb, line 71
def store
  if file_exists?(filename)
    raise FeatureGeneratorException.new("File #{filename} already exists at #{file_path(filename)}.")
  else
    FileUtils.mkdir_p path
    File.open(filename_with_path, 'w') do |file|
      file.write(generate)
      puts "Generating #{File.basename(filename_with_path)}"
    end
  end
end

Private Instance Methods

file_exists?(filename) click to toggle source
# File lib/spinach/generators/feature_generator.rb, line 85
def file_exists?(filename)
  !!file_path(filename)
end
file_path(filename) click to toggle source
# File lib/spinach/generators/feature_generator.rb, line 89
def file_path(filename)
  Dir.glob("#{path}/**/#{filename}").first
end