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 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(data) ⇒ 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 scenario, avoiding name repetition.
-
#store ⇒ Object
Stores the example feature steps definition into an expected path.
Constructor Details
#initialize(data) ⇒ FeatureGenerator
Returns a new instance of FeatureGenerator.
9 10 11 |
# File 'lib/spinach/generators/feature_generator.rb', line 9 def initialize(data) @data = data end |
Instance Method Details
#filename ⇒ String
Returns an example filename for this feature steps.
61 62 63 64 65 |
# File 'lib/spinach/generators/feature_generator.rb', line 61 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.
75 76 77 |
# File 'lib/spinach/generators/feature_generator.rb', line 75 def filename_with_path File. File.join(path, filename) end |
#generate ⇒ String
Returns an example feature steps definition.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/spinach/generators/feature_generator.rb', line 44 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.
38 39 40 |
# File 'lib/spinach/generators/feature_generator.rb', line 38 def name @data['name'].strip if @data['name'] end |
#path ⇒ String
Returns the path where this feature steps may be saved.
69 70 71 |
# File 'lib/spinach/generators/feature_generator.rb', line 69 def path Spinach.config[:step_definitions_path] end |
#steps ⇒ Array<Hash>
Returns an array of unique steps found in this scenario, avoiding name repetition.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/spinach/generators/feature_generator.rb', line 16 def steps return @steps if @steps @steps = [] if scenarios = @data['elements'] scenarios.each do |scenario| if scenario_steps = scenario['steps'] scenario_steps.each do |step| unless @steps.any?{|s| s['name'] == step['name']} @steps << { 'keyword' => step['keyword'].strip, 'name' => step['name'].strip } end end end end end @steps end |
#store ⇒ Object
Stores the example feature steps definition into an expected path
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/spinach/generators/feature_generator.rb', line 81 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 |