Class: Core::Loader::Context
- Inherits:
- BasicObject
- Defined in:
- lib/core/loader/context.rb
Overview
- public
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(path:, root:, type:, target:) ⇒ Context
constructor
A new instance of Context.
-
#load ⇒ Object
[public].
- #method_missing(definable, *name, **kwargs, &block) ⇒ Object
- #respond_to_missing?(definable, include_private = false) ⇒ Boolean
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(definable, *name, **kwargs, &block) ⇒ Object
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/core/loader/context.rb', line 29 def method_missing(definable, *name, **kwargs, &block) if @target.makes?(definable) unless definable == @type ::Kernel.raise "expected to define an object of type `#{@type}` but was `#{definable}` (#{@path})" end definable_path = ::Kernel.Pathname(@path.to_s.gsub(@root.to_s, "")) expected_name = definable_path.dirname.join( definable_path.basename(definable_path.extname) ).to_s.split("/").reject(&:empty?).compact.map(&:to_sym) if name.empty? name = expected_name elsif name != expected_name ::Kernel.raise "expected to define an object named `#{expected_name.join(", ")}` but was `#{name.join(", ")}` (#{@path})" end path, line = ::Kernel.caller(1..1).first.split(":", 3) location = ::Core::Define::Location.new(path: path, line: line) defined = @target.make(definable, *name, __location__: location, **kwargs) # TODO: Validate that definition name matches the expectation, and that it's the expected type. if block header = @path.read.each_line.take(block.source_location[1] - 1).join source = extract_block_inner_source(block, @path) type = case defined when ::Class "class" when ::Module "module" end eval_source = <<~SOURCE #{header}#{type} #{defined}#{source} end SOURCE ::Kernel.eval(eval_source, ::Kernel.const_get("TOPLEVEL_BINDING"), @path.to_s) end defined else ::Kernel.public_send(definable, *name, **kwargs, &block) end end |
Class Method Details
.const_missing(name) ⇒ Object
25 26 27 |
# File 'lib/core/loader/context.rb', line 25 def self.const_missing(name) ::Object.const_get(name) end |
Instance Method Details
#load ⇒ Object
- public
21 22 23 |
# File 'lib/core/loader/context.rb', line 21 def load ::Kernel.eval(@path.read, ::Kernel.binding, @path.to_s) end |
#respond_to_missing?(definable, include_private = false) ⇒ Boolean
77 78 79 |
# File 'lib/core/loader/context.rb', line 77 def respond_to_missing?(definable, include_private = false) @target.makes?(definable) || ::Kernel.respond_to?(definable, include_private) end |