core-loader 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c5a9a91c437fb1cf4033d6fc70b98d4f9ab1784360838ce84e83ad6b10ad62ee
4
- data.tar.gz: 28995fde28e2505ca1072b26e5bbeb8333c55fd7f9a32cd176b78ac198166e79
3
+ metadata.gz: 9650cdc4cda182f6e2603097b50e055c24d46701d899293b67408fd86cd61f04
4
+ data.tar.gz: 121fab36735f4e8b8ddd5b1709b0a1b34f7a6fc314a356dbe49a448b44f331a9
5
5
  SHA512:
6
- metadata.gz: b3f580e3494670b72240c3be121bc9cb82da7e9d51a729d8c5c788e20d8a8adc5cfa96fd15dc2b93604d0db3cb40b6b4dce8231c2775060ad4df8d4d8b1a7463
7
- data.tar.gz: 8968a6d6a4622db48d41008f1df346fe9fb1cd2780ecfd56a883a0bed3a15474080a8c6ceb74cac000cd9e66d17e59ec69d1150a87cbbbb688ecb1bdfa3c7d26
6
+ metadata.gz: 2350e78fa80a8002d3a514a459520d2aeaad1fc16cd51bc34e2554cb4a01916df64c157553065448307f51196fe9386a42ecf81eb7681e5e0caefd5106fe336b
7
+ data.tar.gz: 1152ea9b2baf7d991dd1bdbb9380ee6b22d408b64268f15318de0d62336553bcda683114b2f255c9f895f8e86f90a5c96ba890fb257de7952b55e84db45fab11
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## [v0.1.1](https://github.com/metabahn/corerb/releases/tag/2022-01-08)
2
+
3
+ *released on 2022-01-08*
4
+
5
+ * `fix` [#117](https://github.com/metabahn/corerb/pull/117) Explicitly define makeables in loader, resolving some context issues ([bryanp](https://github.com/bryanp))
6
+
1
7
  ## [v0.1.0](https://github.com/metabahn/corerb/releases/tag/2022-01-07)
2
8
 
3
9
  *released on 2022-01-07*
@@ -8,7 +8,7 @@ module Core
8
8
  module Loader
9
9
  # [public]
10
10
  #
11
- class Context < BasicObject
11
+ class Context
12
12
  class << self
13
13
  def load(path:, root:, type:, target:, strict:)
14
14
  new(path: path, root: root, type: type, target: target, strict: strict).load
@@ -16,75 +16,66 @@ module Core
16
16
  end
17
17
 
18
18
  def initialize(path:, root:, type:, target:, strict:)
19
- @path = ::Kernel.Pathname(path)
20
- @root = ::Kernel.Pathname(root)
19
+ @path = Pathname(path)
20
+ @root = Pathname(root)
21
21
  @type = type.to_sym
22
22
  @target = target
23
23
  @strict = strict
24
+ prepare_to_load!
24
25
  end
25
26
 
26
27
  # [public]
27
28
  #
28
29
  def load
29
- ::Kernel.eval(@path.read, ::Kernel.binding, @path.to_s)
30
+ eval(@path.read, binding, @path.to_s)
30
31
  end
31
32
 
32
- def self.const_missing(name)
33
- ::Object.const_get(name)
34
- end
33
+ private def prepare_to_load!
34
+ @target.makeables.each do |makeable|
35
+ define_singleton_method(makeable) do |*name, **kwargs, &block|
36
+ if @strict && makeable != @type
37
+ raise "expected to define an object of type `#{@type}` but was `#{makeable}` (#{@path})"
38
+ end
35
39
 
36
- def method_missing(definable, *name, **kwargs, &block)
37
- if @target.makes?(definable)
38
- if @strict && definable != @type
39
- ::Kernel.raise "expected to define an object of type `#{@type}` but was `#{definable}` (#{@path})"
40
- end
40
+ definable_path = Pathname(@path.to_s.gsub(@root.to_s, ""))
41
+ expected_name = definable_path.dirname.join(
42
+ definable_path.basename(definable_path.extname)
43
+ ).to_s.split("/").reject(&:empty?).compact.map(&:to_sym)
41
44
 
42
- definable_path = ::Kernel.Pathname(@path.to_s.gsub(@root.to_s, ""))
43
- expected_name = definable_path.dirname.join(
44
- definable_path.basename(definable_path.extname)
45
- ).to_s.split("/").reject(&:empty?).compact.map(&:to_sym)
45
+ if name.empty?
46
+ name = expected_name
47
+ elsif @strict && name != expected_name
48
+ raise "expected to define an object named `#{expected_name.join(", ")}` but was `#{name.join(", ")}` (#{@path})"
49
+ end
46
50
 
47
- if name.empty?
48
- name = expected_name
49
- elsif @strict && name != expected_name
50
- ::Kernel.raise "expected to define an object named `#{expected_name.join(", ")}` but was `#{name.join(", ")}` (#{@path})"
51
- end
51
+ path, line = caller(1..1).first.split(":", 3)
52
+ location = Core::Define::Location.new(path: path, line: line)
53
+ defined = @target.make(makeable, *name, __location__: location, **kwargs)
52
54
 
53
- path, line = ::Kernel.caller(1..1).first.split(":", 3)
54
- location = ::Core::Define::Location.new(path: path, line: line)
55
- defined = @target.make(definable, *name, __location__: location, **kwargs)
55
+ if block
56
+ header = @path.read.each_line.take(block.source_location[1] - 1).join
57
+ source = extract_block_inner_source(block, @path)
56
58
 
57
- # TODO: Validate that definition name matches the expectation, and that it's the expected type.
59
+ type = case defined
60
+ when ::Class
61
+ "class"
62
+ when ::Module
63
+ "module"
64
+ end
58
65
 
59
- if block
60
- header = @path.read.each_line.take(block.source_location[1] - 1).join
61
- source = extract_block_inner_source(block, @path)
66
+ eval_source = <<~SOURCE
67
+ #{header}#{type} #{defined}#{source}
68
+ end
69
+ SOURCE
62
70
 
63
- type = case defined
64
- when ::Class
65
- "class"
66
- when ::Module
67
- "module"
71
+ eval(eval_source, TOPLEVEL_BINDING, @path.to_s)
68
72
  end
69
73
 
70
- eval_source = <<~SOURCE
71
- #{header}#{type} #{defined}#{source}
72
- end
73
- SOURCE
74
-
75
- ::Kernel.eval(eval_source, ::Kernel.const_get(:TOPLEVEL_BINDING), @path.to_s)
74
+ defined
76
75
  end
77
-
78
- defined
79
- else
80
- ::Kernel.public_send(definable, *name, **kwargs, &block)
81
76
  end
82
77
  end
83
78
 
84
- def respond_to_missing?(definable, include_private = false)
85
- @target.makes?(definable) || ::Kernel.respond_to?(definable, include_private)
86
- end
87
-
88
79
  private def extract_block_inner_source(block, path)
89
80
  source = extract_block_source(block, path).strip
90
81
 
@@ -95,7 +86,7 @@ module Core
95
86
  else
96
87
  # We should never get here, but raise an error just to be clear.
97
88
  #
98
- ::Kernel.raise "could not parse inner source of `#{path}`"
89
+ raise "could not parse inner source of `#{path}`"
99
90
  end
100
91
  end
101
92
 
@@ -113,7 +104,7 @@ module Core
113
104
 
114
105
  # We should never get here, but raise an error just to be clear.
115
106
  #
116
- ::Kernel.raise "could not find a complete expression in `#{path}`"
107
+ raise "could not find a complete expression in `#{path}`"
117
108
  end
118
109
 
119
110
  private def scan_to_complete_expression(source, matcher, ending_offset, path)
@@ -129,7 +120,7 @@ module Core
129
120
 
130
121
  # We should never get here, but raise an error just to be clear.
131
122
  #
132
- ::Kernel.raise "could not find a complete expression for `#{matcher}` in `#{path}`"
123
+ raise "could not find a complete expression for `#{matcher}` in `#{path}`"
133
124
  end
134
125
 
135
126
  # Based on https://github.com/banister/method_source.
@@ -138,8 +129,8 @@ module Core
138
129
  original_verbose = $VERBOSE
139
130
  $VERBOSE = nil
140
131
 
141
- ::Kernel.catch :valid do
142
- ::Kernel.eval "BEGIN{throw :valid}\n#{string}" # standard:disable Style/EvalWithLocation
132
+ catch :valid do
133
+ eval "BEGIN{throw :valid}\n#{string}"
143
134
  end
144
135
 
145
136
  true
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Core
4
4
  module Loader
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
6
6
 
7
7
  # [public]
8
8
  #
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: core-loader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Powell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-08 00:00:00.000000000 Z
11
+ date: 2022-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: core-extension