Class: Texd::LookupContext

Inherits:
Object
  • Object
show all
Defined in:
lib/texd/lookup_context.rb

Overview

LookupContext is used to find files accross gem, engine and application barriers. This allows other gems and engines to define additional paths to look into, when searching for files.

This mechanism is useful, if you want to build a Rails engine to provide default files, and reuse those defaults accross multiple Rails apps.

To add a file to the loolup path set, configure Texd:

# in lib/yourgem/railtie.rb
module Yourgem::Railtie < Rails::Railtie
  initializer "configure Texd" do
    Texd.configure do |config|
      config.lookup_paths << Pathname.new(__dir__).join("../../app/tex")
    end
  end
end

Then files in your ‘app/tex/` directory will be used, if they are not found in the host application’s ‘app/tex/` directory (“app/tex” is just a convention, you could add arbitrary directories).

Defined Under Namespace

Classes: MissingFileError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths) ⇒ LookupContext

Returns a new instance of LookupContext.

Parameters:

  • paths (Array<String, Pathname>)

    is a set of paths to search files in; usually configured with ‘Texd.config.lookup_paths`



36
37
38
# File 'lib/texd/lookup_context.rb', line 36

def initialize(paths)
  @paths = (paths || []).map { |path| expand(path) }
end

Instance Attribute Details

#pathsObject (readonly)

A list of directories, in priority order, to search files in.



32
33
34
# File 'lib/texd/lookup_context.rb', line 32

def paths
  @paths
end

Instance Method Details

#find(name) ⇒ Pathname

Performs file look up in the configured paths.

Parameters:

  • name (String, Pathname)

    of file to find.

Returns:

  • (Pathname)

    path to file, when found

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/texd/lookup_context.rb', line 45

def find(name)
  return expand(name) if File.absolute_path?(name)

  paths.each do |path|
    candidate = path.join(name).expand_path
    return candidate if candidate.exist?
  end

  msg  = format "file %p not found\nsearch paths:\n\t", name
  msg << paths.join("\n\t")
  raise MissingFileError, msg
end