Class: Texd::LookupContext
- Inherits:
-
Object
- Object
- Texd::LookupContext
- 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
-
#paths ⇒ Object
readonly
A list of directories, in priority order, to search files in.
Instance Method Summary collapse
-
#find(name) ⇒ Pathname
Performs file look up in the configured paths.
-
#initialize(paths) ⇒ LookupContext
constructor
A new instance of LookupContext.
Constructor Details
#initialize(paths) ⇒ LookupContext
Returns a new instance of LookupContext.
36 37 38 |
# File 'lib/texd/lookup_context.rb', line 36 def initialize(paths) @paths = (paths || []).map { |path| (path) } end |
Instance Attribute Details
#paths ⇒ Object (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.
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/texd/lookup_context.rb', line 45 def find(name) return (name) if File.absolute_path?(name) paths.each do |path| candidate = path.join(name). 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 |