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.
13 14 15 |
# File 'lib/spinach/generators/feature_generator.rb', line 13 def initialize(feature) @feature = feature end |
Instance Attribute Details
#feature ⇒ Object (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
#filename ⇒ String
Returns 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_path ⇒ String
Returns 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. File.join(path, filename) end |
#generate ⇒ String
Returns 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 |
#name ⇒ String
Returns this feature’s name.
29 30 31 |
# File 'lib/spinach/generators/feature_generator.rb', line 29 def name @feature.name end |
#path ⇒ String
Returns 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 |
#steps ⇒ Array<Hash>
Returns 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 |
#store ⇒ Object
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 |