Class: CachedNestedFileReader
- Includes:
- Exceptions
- Defined in:
- lib/cached_nested_file_reader.rb
Overview
The CachedNestedFileReader class provides functionality to read file lines with the ability to process ‘#import filename’ directives. When such a directive is encountered in a file, the corresponding ‘filename’ is read and its contents are inserted at that location. This class caches read files to avoid re-reading the same file multiple times. It allows clients to read lines with or without providing a block.
Instance Method Summary collapse
- #error_handler(name = '', opts = {}) ⇒ Object
-
#initialize(import_pattern: /^ *#import (.+)$/) ⇒ CachedNestedFileReader
constructor
A new instance of CachedNestedFileReader.
- #readlines(filename, depth = 0, context: '', import_paths: nil, indention: '', &block) ⇒ Object
- #warn_format(name, message, opts = {}) ⇒ Object
Methods included from Exceptions
Constructor Details
#initialize(import_pattern: /^ *#import (.+)$/) ⇒ CachedNestedFileReader
Returns a new instance of CachedNestedFileReader.
23 24 25 26 |
# File 'lib/cached_nested_file_reader.rb', line 23 def initialize(import_pattern: /^ *#import (.+)$/) @file_cache = {} @import_pattern = import_pattern end |
Instance Method Details
#error_handler(name = '', opts = {}) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/cached_nested_file_reader.rb', line 28 def error_handler(name = '', opts = {}) Exceptions.error_handler( "CachedNestedFileReader.#{name} -- #{$!}", opts ) end |
#readlines(filename, depth = 0, context: '', import_paths: nil, indention: '', &block) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cached_nested_file_reader.rb', line 42 def readlines(filename, depth = 0, context: '', import_paths: nil, indention: '', &block) if @file_cache.key?(filename) @file_cache[filename].each(&block) if block return @file_cache[filename] end directory_path = File.dirname(filename) processed_lines = [] File.readlines(filename, chomp: true).each.with_index do |line, ind| if Regexp.new(@import_pattern) =~ line name_strip = $~[:name].strip import_indention = indention + $~[:indention] included_file_path = if name_strip =~ %r{^/} name_strip elsif import_paths find_files(name_strip, import_paths + [directory_path])&.first else File.join(directory_path, name_strip) end processed_lines += readlines(included_file_path, depth + 1, context: "#{filename}:#{ind + 1}", import_paths: import_paths, indention: import_indention, &block) else nested_line = NestedLine.new(line, depth, indention) processed_lines.push(nested_line) block&.call(nested_line) end end @file_cache[filename] = processed_lines rescue Errno::ENOENT # Exceptions.error_handler('readlines', { abort: true }) warn_format('readlines', "No such file -- #{filename} @@ #{context}", { abort: true }) end |
#warn_format(name, message, opts = {}) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/cached_nested_file_reader.rb', line 35 def warn_format(name, , opts = {}) Exceptions.warn_format( "CachedNestedFileReader.#{name} -- #{}", opts ) end |