Class: Rjq::ModuleResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/rjq/modules.rb

Defined Under Namespace

Classes: ResolvedModule

Constant Summary collapse

DEFAULT_MAX_BYTES =
1_048_576

Instance Method Summary collapse

Constructor Details

#initialize(paths: [], use_default_paths: true, max_bytes: DEFAULT_MAX_BYTES) ⇒ ModuleResolver

Returns a new instance of ModuleResolver.



11
12
13
14
15
16
17
18
19
20
# File 'lib/rjq/modules.rb', line 11

def initialize(paths: [], use_default_paths: true, max_bytes: DEFAULT_MAX_BYTES)
  configured = paths.map { |path| File.expand_path(path) }
  if use_default_paths
    configured.concat(ENV.fetch('JQ_LIBRARY_PATH', '').split(File::PATH_SEPARATOR).reject(&:empty?))
    configured.concat([File.expand_path('~/.jq'), File.expand_path('~/.rjq')])
  end
  @roots = configured.uniq.freeze
  @max_bytes = max_bytes
  @cache = {}
end

Instance Method Details

#initial_metadataObject



35
36
37
# File 'lib/rjq/modules.rb', line 35

def 
  {}
end

#resolve(name, from: nil, metadata: {}, data: false) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rjq/modules.rb', line 22

def resolve(name, from: nil, metadata: {}, data: false)
  validate_name!(name)
  extension = data ? '.json' : '.jq'
  candidates(name, extension, from, ).each do |candidate|
    next unless File.file?(candidate)

    path = File.realpath(candidate)
    validate_root!(path)
    return cached_source(name, path, data)
  end
  raise CompileError, "module #{name.inspect} not found"
end