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 +4 -4
- data/CHANGELOG.md +6 -0
- data/lib/core/loader/context.rb +43 -52
- data/lib/core/loader/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9650cdc4cda182f6e2603097b50e055c24d46701d899293b67408fd86cd61f04
|
4
|
+
data.tar.gz: 121fab36735f4e8b8ddd5b1709b0a1b34f7a6fc314a356dbe49a448b44f331a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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*
|
data/lib/core/loader/context.rb
CHANGED
@@ -8,7 +8,7 @@ module Core
|
|
8
8
|
module Loader
|
9
9
|
# [public]
|
10
10
|
#
|
11
|
-
class Context
|
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 =
|
20
|
-
@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
|
-
|
30
|
+
eval(@path.read, binding, @path.to_s)
|
30
31
|
end
|
31
32
|
|
32
|
-
def
|
33
|
-
|
34
|
-
|
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
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
48
|
-
|
49
|
-
|
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
|
-
|
54
|
-
|
55
|
-
|
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
|
-
|
59
|
+
type = case defined
|
60
|
+
when ::Class
|
61
|
+
"class"
|
62
|
+
when ::Module
|
63
|
+
"module"
|
64
|
+
end
|
58
65
|
|
59
|
-
|
60
|
-
|
61
|
-
|
66
|
+
eval_source = <<~SOURCE
|
67
|
+
#{header}#{type} #{defined}#{source}
|
68
|
+
end
|
69
|
+
SOURCE
|
62
70
|
|
63
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
142
|
-
|
132
|
+
catch :valid do
|
133
|
+
eval "BEGIN{throw :valid}\n#{string}"
|
143
134
|
end
|
144
135
|
|
145
136
|
true
|
data/lib/core/loader/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2022-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: core-extension
|