Class: RSpec::RfcHelper::Specs
- Inherits:
-
Object
- Object
- RSpec::RfcHelper::Specs
- Defined in:
- lib/rspec/rfc_helper/specs.rb
Overview
Spec collection
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#sections ⇒ Object
readonly
Returns the value of attribute sections.
-
#specs ⇒ Object
readonly
Returns the value of attribute specs.
-
#specs_url ⇒ Object
readonly
Returns the value of attribute specs_url.
Class Method Summary collapse
-
.new_from_file(file) ⇒ RSpec::RfcHelper::Specs
Instantiates a Spec collection from a YAML file.
Instance Method Summary collapse
-
#add(section:, text:, id: nil) ⇒ Object
Adds a spec to the list.
-
#add_example(example) ⇒ Object
Adds an RSpec example to all the relevant specs.
-
#add_section(number:, title:, id:, url: nil) ⇒ Object
Adds a spec section.
-
#each ⇒ Object
Delegation.
-
#find(id) ⇒ RSpec::RfcHelper::Spec
Finds an example by id.
- #ids ⇒ Object
-
#initialize(name: nil, specs_url: nil) ⇒ Specs
constructor
Instantiate a new list of specs.
-
#save_json_report(file) ⇒ Object
Saves the report in JSON.
-
#save_markdown_report(file) ⇒ Object
Saves the report in Markdown.
Constructor Details
#initialize(name: nil, specs_url: nil) ⇒ Specs
Instantiate a new list of specs
47 48 49 50 51 52 |
# File 'lib/rspec/rfc_helper/specs.rb', line 47 def initialize(name: nil, specs_url: nil) @name = name @specs_url = specs_url @sections = {} @specs = [] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/rspec/rfc_helper/specs.rb', line 14 def name @name end |
#sections ⇒ Object (readonly)
Returns the value of attribute sections.
14 15 16 |
# File 'lib/rspec/rfc_helper/specs.rb', line 14 def sections @sections end |
#specs ⇒ Object (readonly)
Returns the value of attribute specs.
14 15 16 |
# File 'lib/rspec/rfc_helper/specs.rb', line 14 def specs @specs end |
#specs_url ⇒ Object (readonly)
Returns the value of attribute specs_url.
14 15 16 |
# File 'lib/rspec/rfc_helper/specs.rb', line 14 def specs_url @specs_url end |
Class Method Details
.new_from_file(file) ⇒ RSpec::RfcHelper::Specs
Instantiates a Spec collection from a YAML file.
Note: String keys are expected.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rspec/rfc_helper/specs.rb', line 24 def self.new_from_file(file) # rubocop:disable Metrics/AbcSize specs = YAML.load_file(file) instance = new name: specs['name'], specs_url: specs['url'] specs['specs'].each do |section| section_number = section['section'].to_s instance.add_section id: section['id'].to_sym, number: section_number, title: section['title'], url: section['url'] section['specs'].each do |spec| instance.add section: section_number, text: spec['text'], id: spec['id']&.to_sym end end instance end |
Instance Method Details
#add(section:, text:, id: nil) ⇒ Object
Adds a spec to the list
It will prefix the spec ID with relevant section ID
87 88 89 90 91 92 93 94 |
# File 'lib/rspec/rfc_helper/specs.rb', line 87 def add(section:, text:, id: nil) if id.is_a? Symbol id = "#{section_id(section)}__#{id}".to_sym raise "Duplicated id #{id}" if ids.include? id end @specs << Spec.new(section: section, text: text, id: id) end |
#add_example(example) ⇒ Object
Adds an RSpec example to all the relevant specs
100 101 102 103 104 105 106 107 108 |
# File 'lib/rspec/rfc_helper/specs.rb', line 100 def add_example(example) ids = example.[:rfc] return if ids.nil? || ids.empty? ids = [ids] if ids.is_a? Symbol ids.each do |id| find(id).add_example example end end |
#add_section(number:, title:, id:, url: nil) ⇒ Object
Adds a spec section
61 62 63 64 65 66 67 |
# File 'lib/rspec/rfc_helper/specs.rb', line 61 def add_section(number:, title:, id:, url: nil) if @sections.key?(number) || @sections.find { |_number, section| section[:id] == id } raise "Section number #{number} already exists" end @sections[number] = { title: title, id: id, url: url } end |
#each ⇒ Object
Delegation
116 117 118 |
# File 'lib/rspec/rfc_helper/specs.rb', line 116 def each(&) @specs.each(&) end |
#find(id) ⇒ RSpec::RfcHelper::Spec
Finds an example by id
75 76 77 |
# File 'lib/rspec/rfc_helper/specs.rb', line 75 def find(id) @specs.find { |spec| spec.id == id } || raise("Spec not found with id #{id}") end |
#ids ⇒ Object
110 111 112 |
# File 'lib/rspec/rfc_helper/specs.rb', line 110 def ids @specs.map(&:id) end |
#save_json_report(file) ⇒ Object
Saves the report in JSON
132 133 134 |
# File 'lib/rspec/rfc_helper/specs.rb', line 132 def save_json_report(file) File.write file, RSpec::RfcHelper::JsonRenderer.new(self).render end |
#save_markdown_report(file) ⇒ Object
Saves the report in Markdown
124 125 126 |
# File 'lib/rspec/rfc_helper/specs.rb', line 124 def save_markdown_report(file) File.write file, RSpec::RfcHelper::MarkdownRenderer.new(self).render end |