Class: Asciidoctor::DocTest::IO::Base Abstract
- Inherits:
-
Object
- Object
- Asciidoctor::DocTest::IO::Base
- Defined in:
- lib/asciidoctor/doctest/io/base.rb
Overview
This is a base class that should be extended for specific example formats.
Instance Attribute Summary collapse
-
#file_ext ⇒ Object
readonly
Returns the value of attribute file_ext.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #group_names ⇒ Array<String>
-
#initialize(path: DocTest.examples_path, file_ext: nil) ⇒ Base
constructor
A new instance of Base.
-
#pair_with(other_suite) ⇒ Enumerator
Returns enumerator that yields pairs of the examples from this suite and the
other_suite
(examples with the same name) in order of this suite. -
#parse(input, group_name) ⇒ Array<Example>
abstract
Parses group of examples from the
input
and returns array of the parsed examples. -
#read_examples(group_name) ⇒ Array<Example>
Reads the named examples group from file(s).
-
#serialize(examples) ⇒ String
abstract
Serializes the given examples into string.
-
#update_examples(examples) ⇒ Object
Replaces existing examples with the given ones.
-
#write_examples(examples) ⇒ Object
Writes the given examples into file(s) {path.first}/{group_name}{file_ext}.
Constructor Details
#initialize(path: DocTest.examples_path, file_ext: nil) ⇒ Base
Returns a new instance of Base.
25 26 27 28 29 30 31 |
# File 'lib/asciidoctor/doctest/io/base.rb', line 25 def initialize(path: DocTest.examples_path, file_ext: nil) fail ArgumentError, 'file_ext must not be blank or nil' if file_ext.blank? @path = Array(path).freeze @file_ext = file_ext.strip.freeze @examples_cache = {} end |
Instance Attribute Details
#file_ext ⇒ Object (readonly)
Returns the value of attribute file_ext.
16 17 18 |
# File 'lib/asciidoctor/doctest/io/base.rb', line 16 def file_ext @file_ext end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
16 17 18 |
# File 'lib/asciidoctor/doctest/io/base.rb', line 16 def path @path end |
Instance Method Details
#group_names ⇒ Array<String>
146 147 148 149 150 151 152 |
# File 'lib/asciidoctor/doctest/io/base.rb', line 146 def group_names @path.reduce(Set.new) { |acc, path| acc | Pathname.new(path).each_child .select { |p| p.file? && p.extname == @file_ext } .map { |p| p.sub_ext('').basename.to_s } }.sort end |
#pair_with(other_suite) ⇒ Enumerator
Returns enumerator that yields pairs of the examples from this suite and the other_suite
(examples with the same name) in order of this suite.
When some example is missing in this or the other_suite
, it’s substituted with an empty example of the corresponding type and name. In the case of missing example from this suite, the pair is placed at the end of the examples group.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/asciidoctor/doctest/io/base.rb', line 72 def pair_with(other_suite) Enumerator.new do |y| group_names.each do |group_name| theirs_by_name = other_suite.read_examples(group_name).index_by(&:name) read_examples(group_name).each do |ours| theirs = theirs_by_name.delete(ours.name) theirs ||= other_suite.create_example(ours.name) y.yield ours, theirs end theirs_by_name.each_value do |theirs| y.yield create_example(theirs.name), theirs end end end end |
#parse(input, group_name) ⇒ Array<Example>
Parses group of examples from the input
and returns array of the parsed examples.
:nocov:
42 43 44 |
# File 'lib/asciidoctor/doctest/io/base.rb', line 42 def parse(input, group_name) fail NotImplementedError end |
#read_examples(group_name) ⇒ Array<Example>
100 101 102 103 104 105 |
# File 'lib/asciidoctor/doctest/io/base.rb', line 100 def read_examples(group_name) @examples_cache[group_name] ||= read_files(group_name) .map { |data| parse(data, group_name) } .flatten .uniq(&:name) end |
#serialize(examples) ⇒ String
Serializes the given examples into string.
:nocov:
54 55 56 |
# File 'lib/asciidoctor/doctest/io/base.rb', line 54 def serialize(examples) fail NotImplementedError end |
#update_examples(examples) ⇒ Object
Replaces existing examples with the given ones.
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/asciidoctor/doctest/io/base.rb', line 127 def update_examples(examples) examples.group_by(&:group_name).each do |group, exmpls| # replace cached examples with the given ones and preserve original order updated_group = [ read_examples(group), exmpls ] .map { |e| e.index_by(&:local_name) } .reduce(:merge) .values write_examples updated_group @examples_cache.delete(group) # flush cache end end |
#write_examples(examples) ⇒ Object
Writes the given examples into file(s) {path.first}/{group_name}{file_ext}. Already existing files will be overwritten!
114 115 116 117 118 119 |
# File 'lib/asciidoctor/doctest/io/base.rb', line 114 def write_examples(examples) examples.group_by(&:group_name).each do |group_name, exmpls| path = file_path(@path.first, group_name) File.write(path, serialize(exmpls)) end end |