Module: Avro::Builder::FileHandler

Included in:
DSL
Defined in:
lib/avro/builder/file_handler.rb

Overview

TODO: eventually this should be refactored into something standalone instead of a module that is included to provide the file handling methods.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

FileNotFoundError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


14
15
16
# File 'lib/avro/builder/file_handler.rb', line 14

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#find_file(name) ⇒ Object

Raises:


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/avro/builder/file_handler.rb', line 24

def find_file(name)
  # Ensure that the file_name that is searched for begins with a slash (/)
  # and ends with a .rb extension. Additionally, if the name contains
  # a namespace then ensure that periods (.) are replaced by forward
  # slashes. E.g. for 'test.example' search for '/test/example.rb'.
  file_name = "/#{name.to_s.tr('.', '/').sub(/^\//, '').sub(/\.rb$/, '')}.rb"
  matches = real_load_paths.flat_map do |load_path|
    Dir["#{load_path}/**/*.rb"].select do |file_path|
      file_path.end_with?(file_name)
    end
  end.uniq
  raise "Multiple matches: #{matches}" if matches.size > 1
  raise FileNotFoundError.new("File not found #{file_name}") if matches.empty?

  matches.first
end

#read_file(name) ⇒ Object


18
19
20
# File 'lib/avro/builder/file_handler.rb', line 18

def read_file(name)
  File.read(find_file(name))
end