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

- (FeatureGenerator) initialize(feature)



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

def initialize(feature)
  @feature = feature
end

Instance Attribute Details

- (Object) feature (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

- (String) filename



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

- (String) filename_with_path



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

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

- (String) generate



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

- (String) name



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

def name
  @feature.name
end

- (String) path



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

def path
  Spinach.config[:step_definitions_path]
end

- (Array<Hash>) steps



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

- (Object) store

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