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:



11
12
13
# File 'lib/spinach/generators/feature_generator.rb', line 11

def initialize(feature)
  @feature = feature
end

Instance Attribute Details

#featureObject (readonly)

Returns the value of attribute feature.



7
8
9
# File 'lib/spinach/generators/feature_generator.rb', line 7

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



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

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



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

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/spinach/generators/feature_generator.rb', line 33

def generate
  result = StringIO.new
  result.puts "class #{Spinach::Support.camelize name} < Spinach::FeatureSteps"
  result.puts "  feature \'#{Spinach::Support.escape_single_commas name}\'\n"
  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



27
28
29
# File 'lib/spinach/generators/feature_generator.rb', line 27

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



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

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



18
19
20
21
22
23
# File 'lib/spinach/generators/feature_generator.rb', line 18

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



70
71
72
73
74
75
76
77
78
79
# File 'lib/spinach/generators/feature_generator.rb', line 70

def store
  if File.exists?(filename_with_path)
    raise FeatureGeneratorException.new("File already exists")
  else
    FileUtils.mkdir_p path
    File.open(filename_with_path, 'w') do |file|
      file.write(generate)
    end
  end
end