Class: MOSAIK::Resolver
- Inherits:
-
Object
- Object
- MOSAIK::Resolver
- Defined in:
- lib/mosaik/resolver.rb
Overview
Inference engine for resolving file paths to constant names, and vice versa
Instance Attribute Summary collapse
-
#collapsed ⇒ Object
readonly
Returns the value of attribute collapsed.
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#load_paths ⇒ Object
readonly
Returns the value of attribute load_paths.
-
#overrides ⇒ Object
readonly
Returns the value of attribute overrides.
Instance Method Summary collapse
- #collapse(*collapsed) ⇒ Object
-
#initialize(directory, load_paths, overrides = {}, collapsed = []) ⇒ Resolver
constructor
A new instance of Resolver.
- #override(**overrides) ⇒ Object
- #resolve_constant(constant_name) ⇒ Object
- #resolve_constant!(constant) ⇒ Object
- #resolve_file(abspath) ⇒ Object
- #resolve_file!(abspath) ⇒ Object
Constructor Details
#initialize(directory, load_paths, overrides = {}, collapsed = []) ⇒ Resolver
Returns a new instance of Resolver.
10 11 12 13 14 15 |
# File 'lib/mosaik/resolver.rb', line 10 def initialize(directory, load_paths, overrides = {}, collapsed = []) @directory = directory @load_paths = load_paths @overrides = overrides @collapsed = collapsed end |
Instance Attribute Details
#collapsed ⇒ Object (readonly)
Returns the value of attribute collapsed.
8 9 10 |
# File 'lib/mosaik/resolver.rb', line 8 def collapsed @collapsed end |
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
8 9 10 |
# File 'lib/mosaik/resolver.rb', line 8 def directory @directory end |
#load_paths ⇒ Object (readonly)
Returns the value of attribute load_paths.
8 9 10 |
# File 'lib/mosaik/resolver.rb', line 8 def load_paths @load_paths end |
#overrides ⇒ Object (readonly)
Returns the value of attribute overrides.
8 9 10 |
# File 'lib/mosaik/resolver.rb', line 8 def overrides @overrides end |
Instance Method Details
#collapse(*collapsed) ⇒ Object
21 22 23 |
# File 'lib/mosaik/resolver.rb', line 21 def collapse(*collapsed) @collapsed += collapsed end |
#override(**overrides) ⇒ Object
17 18 19 |
# File 'lib/mosaik/resolver.rb', line 17 def override(**overrides) @overrides.merge!(overrides) end |
#resolve_constant(constant_name) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mosaik/resolver.rb', line 56 def resolve_constant(constant_name) # Convert the constant name to a file path file = constant_name .split("::") .map { |c| underscore(c) } .join("/") # Generate potential paths paths = load_paths.flat_map do |load_path| # Regular file path abspath = File.join(directory, load_path, "#{file}.rb") # Directory path dirpath = File.join(directory, load_path, file) # Expanded file path expaths = collapsed.map do |dir| abspath.gsub(dir.split("/")[..-2].join("/"), dir) end [abspath, dirpath, *expaths] end # Check if the file or directory exists paths.uniq.find { |path| File.file?(path) || File.directory?(path) } end |
#resolve_constant!(constant) ⇒ Object
87 88 89 |
# File 'lib/mosaik/resolver.rb', line 87 def resolve_constant!(constant) resolve_constant(constant) || raise(ResolveError, "cannot resolve #{constant} in: #{load_paths.join(', ')}") end |
#resolve_file(abspath) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mosaik/resolver.rb', line 25 def resolve_file(abspath) # Get the file name without the extension file = File.basename(abspath, ".rb") # Get the path without the file name path = File.dirname(abspath) # Remove the collapsed directories collapsed.each do |dir| path = path.gsub(%r(#{dir}/?), dir.split("/")[..-2].join("/")) end # Remove the directory prefix path = path.gsub(%r(#{directory}/?), "") # Return unless the path is in the load paths return unless load_paths.any? { |load_path| path.start_with?(load_path) } # Remove the load path prefix (e.g. lib/) load_paths.each do |load_path| path = path.gsub(%r(#{load_path}/?), "") end # Convert the path and file name to a constant name path .split("/") .map { |p| camelize(p) } .append(camelize(file)) .join("::") end |
#resolve_file!(abspath) ⇒ Object
83 84 85 |
# File 'lib/mosaik/resolver.rb', line 83 def resolve_file!(abspath) resolve_file(abspath) || raise(ResolveError, "cannot resolve #{abspath} in: #{load_paths.join(', ')}") end |