Class: Truck::Context::ConstResolver
- Inherits:
-
Object
- Object
- Truck::Context::ConstResolver
- Defined in:
- lib/truck/const_resolver.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#current_path ⇒ Object
readonly
Returns the value of attribute current_path.
-
#expanded_const ⇒ Object
readonly
Returns the value of attribute expanded_const.
-
#skip_files ⇒ Object
readonly
Returns the value of attribute skip_files.
Instance Method Summary collapse
- #check_already_loaded ⇒ Object
- #check_loaded(rb_file) ⇒ Object
- #const_get ⇒ Object
- #each_autoload_path ⇒ Object
- #each_possible_rb_file ⇒ Object
- #each_rb_file_from_base_path(base_path) ⇒ Object
- #expected_const_defined_in_rb_file(rb_file, autoload_path: current_path) ⇒ Object
-
#initialize(context:, expanded_const:, skip_files:) ⇒ ConstResolver
constructor
A new instance of ConstResolver.
- #resolve ⇒ Object
- #resolve! ⇒ Object
- #walk_const_parts(const = expanded_const) ⇒ Object
Constructor Details
#initialize(context:, expanded_const:, skip_files:) ⇒ ConstResolver
Returns a new instance of ConstResolver.
8 9 10 11 12 |
# File 'lib/truck/const_resolver.rb', line 8 def initialize(context:, expanded_const:, skip_files:) @context = context @expanded_const = @skip_files = skip_files end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
6 7 8 |
# File 'lib/truck/const_resolver.rb', line 6 def context @context end |
#current_path ⇒ Object (readonly)
Returns the value of attribute current_path.
6 7 8 |
# File 'lib/truck/const_resolver.rb', line 6 def current_path @current_path end |
#expanded_const ⇒ Object (readonly)
Returns the value of attribute expanded_const.
6 7 8 |
# File 'lib/truck/const_resolver.rb', line 6 def @expanded_const end |
#skip_files ⇒ Object (readonly)
Returns the value of attribute skip_files.
6 7 8 |
# File 'lib/truck/const_resolver.rb', line 6 def skip_files @skip_files end |
Instance Method Details
#check_already_loaded ⇒ Object
64 65 66 67 68 69 |
# File 'lib/truck/const_resolver.rb', line 64 def check_already_loaded walk_const_parts.reduce context.mod do |mod, const_part| return nil unless mod.const_defined? const_part mod.const_get const_part end end |
#check_loaded(rb_file) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/truck/const_resolver.rb', line 71 def check_loaded(rb_file) expected_const = expected_const_defined_in_rb_file rb_file walk_const_parts(expected_const).reduce context.mod do |mod, const_part| mod.const_defined?(const_part) or raise AutoloadError.new(const: expected_const, rb_file: rb_file) mod.const_get const_part end end |
#const_get ⇒ Object
52 53 54 55 56 57 |
# File 'lib/truck/const_resolver.rb', line 52 def const_get walk_const_parts.reduce context.mod do |mod, const_part| return nil unless mod.const_defined?(const_part) mod.const_get const_part end end |
#each_autoload_path ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/truck/const_resolver.rb', line 36 def each_autoload_path context.autoload_paths.each do |autoload_path| @current_path = autoload_path yield end @current_path = nil end |
#each_possible_rb_file ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/truck/const_resolver.rb', line 26 def each_possible_rb_file each_autoload_path do base_path = current_path.join .to_snake_case each_rb_file_from_base_path base_path do |rb_file| next if skip_files.include? rb_file.to_s yield rb_file if File.exist?(rb_file) end end end |
#each_rb_file_from_base_path(base_path) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/truck/const_resolver.rb', line 44 def each_rb_file_from_base_path(base_path) base_path.ascend do |path| return if path == context.root rb_file = path.sub_ext '.rb' yield rb_file if rb_file.file? end end |
#expected_const_defined_in_rb_file(rb_file, autoload_path: current_path) ⇒ Object
80 81 82 83 84 |
# File 'lib/truck/const_resolver.rb', line 80 def expected_const_defined_in_rb_file(rb_file, autoload_path: current_path) rel_path = rb_file.sub_ext('').relative_path_from(autoload_path).to_s matcher = %r{\A(#{rel_path.to_camel_case})}i .match(matcher).captures.fetch 0 end |
#resolve ⇒ Object
14 15 16 |
# File 'lib/truck/const_resolver.rb', line 14 def resolve check_already_loaded or resolve! end |
#resolve! ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/truck/const_resolver.rb', line 18 def resolve! each_possible_rb_file do |rb_file| context.load_file rb_file check_loaded rb_file end const_get end |
#walk_const_parts(const = expanded_const) ⇒ Object
59 60 61 62 |
# File 'lib/truck/const_resolver.rb', line 59 def walk_const_parts(const = ) return to_enum(:walk_const_parts, const) if block_given? const.split '::' end |