Class: RSpec::RfcHelper::Specs

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/rfc_helper/specs.rb

Overview

Spec collection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, specs_url: nil) ⇒ Specs

Instantiate a new list of specs

Parameters:

  • name (String?) (defaults to: nil)

    Specification name

  • specs_url (String?) (defaults to: nil)

    URL to the full specification



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

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/rspec/rfc_helper/specs.rb', line 14

def name
  @name
end

#sectionsObject (readonly)

Returns the value of attribute sections.



14
15
16
# File 'lib/rspec/rfc_helper/specs.rb', line 14

def sections
  @sections
end

#specsObject (readonly)

Returns the value of attribute specs.



14
15
16
# File 'lib/rspec/rfc_helper/specs.rb', line 14

def specs
  @specs
end

#specs_urlObject (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.

Parameters:

  • file (String)

    Path to the specs file

Returns:



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

Parameters:

  • section (String)

    Section number

  • text (String)

    Content

  • id (Symbol?) (defaults to: nil)

    Unique section identifier



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

Parameters:

  • example (RSpec::Core::Example)


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

Parameters:

  • number (String)

    Section number

  • title (String)

    Section title

  • id (Symbol)

    Unique identifier

  • url (String?) (defaults to: nil)

    URL to this 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

#eachObject

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

Parameters:

  • id (Symbol)

    Spec identifier

Returns:



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

#idsObject



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

Parameters:

  • file (String)

    Output file path



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

Parameters:

  • file (String)

    Output file path



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