Class: Spinach::Generators::FeatureGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/spinach/generators/feature_generator.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(feature) ⇒ FeatureGenerator

Returns a new instance of FeatureGenerator.

Parameters:



13
14
15
# File 'lib/spinach/generators/feature_generator.rb', line 13

def initialize(feature)
  @feature = feature
end

Instance Attribute Details

#featureObject (readonly)

Returns the value of attribute feature.



9
10
11
# File 'lib/spinach/generators/feature_generator.rb', line 9

def feature
  @feature
end

Instance Method Details

#filenameString

Returns an example filename for this feature steps.

Returns:

  • (String)

    an example filename for this feature steps



51
52
53
54
55
# File 'lib/spinach/generators/feature_generator.rb', line 51

def filename
  Spinach::Support.underscore(
    Spinach::Support.camelize name
  ) + ".rb"
end

#filename_with_pathString

Returns the expanded path where this feature steps may be saved.

Returns:

  • (String)

    the expanded path where this feature steps may be saved



65
66
67
# File 'lib/spinach/generators/feature_generator.rb', line 65

def filename_with_path
  File.expand_path File.join(path, filename)
end

#generateString

Returns an example feature steps definition.

Returns:

  • (String)

    an example feature steps definition



35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

#nameString

Returns this feature’s name.

Returns:

  • (String)

    this feature’s name



29
30
31
# File 'lib/spinach/generators/feature_generator.rb', line 29

def name
  @feature.name
end

#pathString

Returns the path where this feature steps may be saved.

Returns:

  • (String)

    the path where this feature steps may be saved



59
60
61
# File 'lib/spinach/generators/feature_generator.rb', line 59

def path
  Spinach.config[:step_definitions_path]
end

#stepsArray<Hash>

Returns an array of unique steps found in this feature, avoiding name repetition.

Returns:

  • (Array<Hash>)

    an array of unique steps found in this feature, avoiding name repetition



20
21
22
23
24
25
# 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

#storeObject

Stores the example feature steps definition into an expected path



71
72
73
74
75
76
77
78
79
80
81
# 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