Class: Spinach::Generators::FeatureGenerator
- Inherits:
-
Object
- Object
- Spinach::Generators::FeatureGenerator
- 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
-
#feature ⇒ Object
readonly
Returns the value of attribute feature.
Instance Method Summary collapse
-
#filename ⇒ String
An example filename for this feature steps.
-
#filename_with_path ⇒ String
The expanded path where this feature steps may be saved.
-
#generate ⇒ String
An example feature steps definition.
-
#initialize(feature) ⇒ FeatureGenerator
constructor
A new instance of FeatureGenerator.
-
#name ⇒ String
This feature’s name.
-
#path ⇒ String
The path where this feature steps may be saved.
-
#steps ⇒ Array<Hash>
An array of unique steps found in this feature, avoiding name repetition.
-
#store ⇒ Object
Stores the example feature steps definition into an expected path.
Constructor Details
#initialize(feature) ⇒ FeatureGenerator
Returns a new instance of FeatureGenerator.
11 12 13 |
# File 'lib/spinach/generators/feature_generator.rb', line 11 def initialize(feature) @feature = feature end |
Instance Attribute Details
#feature ⇒ Object (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
#filename ⇒ String
Returns 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_path ⇒ String
Returns 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. File.join(path, filename) end |
#generate ⇒ String
Returns 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 |
#name ⇒ String
Returns this feature’s name.
27 28 29 |
# File 'lib/spinach/generators/feature_generator.rb', line 27 def name @feature.name end |
#path ⇒ String
Returns 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 |
#steps ⇒ Array<Hash>
Returns 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 |
#store ⇒ Object
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 |