Sha256: 9f9f4ed5a2e35b3ca90eece0a09e0bc653826b85f2f01b5c14b4d6d9ccefb602

Contents?: true

Size: 1.28 KB

Versions: 5

Compression:

Stored size: 1.28 KB

Contents

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

      module ClassMethods
        # Load paths are used to search for imports and extends.
        def load_paths
          @load_paths ||= Set.new
        end
      end

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

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

      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 = self.class.load_paths.flat_map do |load_path|
          Dir["#{load_path}/**/*.rb"].select do |file_path|
            file_path.end_with?(file_name)
          end
        end
        raise "Multiple matches: #{matches}" if matches.size > 1
        raise "File not found #{file_name}" if matches.empty?

        matches.first
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
avro-builder-0.14.0 lib/avro/builder/file_handler.rb
avro-builder-0.13.0 lib/avro/builder/file_handler.rb
avro-builder-0.12.0 lib/avro/builder/file_handler.rb
avro-builder-0.11.0 lib/avro/builder/file_handler.rb
avro-builder-0.10.0 lib/avro/builder/file_handler.rb