core-loader 0.1.0 → 0.2.0

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: 1484ad0d5e2736ca252c0ec67308556dd0b45bffdfb8cd51756f1a4007d87bc6
4
+ data.tar.gz: beb2f418301e05f53bed4dec557126cd37fd4633b6769987718e1243e06296a2
5
5
  SHA512:
6
- metadata.gz: b3f580e3494670b72240c3be121bc9cb82da7e9d51a729d8c5c788e20d8a8adc5cfa96fd15dc2b93604d0db3cb40b6b4dce8231c2775060ad4df8d4d8b1a7463
7
- data.tar.gz: 8968a6d6a4622db48d41008f1df346fe9fb1cd2780ecfd56a883a0bed3a15474080a8c6ceb74cac000cd9e66d17e59ec69d1150a87cbbbb688ecb1bdfa3c7d26
6
+ metadata.gz: 9f5746e5b02cd234aa4a3b741505fb284d1c21cc8f803e59fe23e5294da1530d6d1e0679550e07cabda01a6047e348d65c6dc2f62959792a73f71cf239bdc3d3
7
+ data.tar.gz: 629be77c6e72453a2ecd2c811324fcc162e387883d582f27ff3c61fa6d2dccbe9956b985bc5a1b1ef9ac77d7edbaea6c37e6f94f664a1e27e21f3e5866d6e564
data/CHANGELOG.md CHANGED
@@ -1,20 +1,33 @@
1
- ## [v0.1.0](https://github.com/metabahn/corerb/releases/tag/2022-01-07)
1
+ ## [v0.2.0](https://github.com/bryanp/corerb/releases/tag/2023-12-24)
2
+
3
+ *released on 2023-12-24*
4
+
5
+ * `dep` [#143](https://github.com/bryanp/corerb/pull/143) Deprecate `Is::*` and `Refine::*` namespaces ([bryanp](https://github.com/bryanp))
6
+ * `dep` [#135](https://github.com/bryanp/corerb/pull/135) Remove Ruby 2 support ([bryanp](https://github.com/bryanp))
7
+
8
+ ## [v0.1.1](https://github.com/bryanp/corerb/releases/tag/2022-01-08)
9
+
10
+ *released on 2022-01-08*
11
+
12
+ * `fix` [#117](https://github.com/bryanp/corerb/pull/117) Explicitly define makeables in loader, resolving some context issues ([bryanp](https://github.com/bryanp))
13
+
14
+ ## [v0.1.0](https://github.com/bryanp/corerb/releases/tag/2022-01-07)
2
15
 
3
16
  *released on 2022-01-07*
4
17
 
5
- * `chg` [#115](https://github.com/metabahn/corerb/pull/115) Make the loader context configurable ([bryanp](https://github.com/bryanp))
6
- * `chg` [#114](https://github.com/metabahn/corerb/pull/114) Add an explicit strict option to loader ([bryanp](https://github.com/bryanp))
18
+ * `chg` [#115](https://github.com/bryanp/corerb/pull/115) Make the loader context configurable ([bryanp](https://github.com/bryanp))
19
+ * `chg` [#114](https://github.com/bryanp/corerb/pull/114) Add an explicit strict option to loader ([bryanp](https://github.com/bryanp))
7
20
 
8
- ## [v0.0.1](https://github.com/metabahn/corerb/releases/tag/2021-11-23.1)
21
+ ## [v0.0.1](https://github.com/bryanp/corerb/releases/tag/2021-11-23.1)
9
22
 
10
23
  *released on 2021-11-23*
11
24
 
12
- * `chg` [#109](https://github.com/metabahn/corerb/pull/109) Prefer `__send__` to `send` ([bryanp](https://github.com/bryanp))
25
+ * `chg` [#109](https://github.com/bryanp/corerb/pull/109) Prefer `__send__` to `send` ([bryanp](https://github.com/bryanp))
13
26
 
14
- ## [v0.0.0](https://github.com/metabahn/corerb/releases/tag/2021-11-02)
27
+ ## [v0.0.0](https://github.com/bryanp/corerb/releases/tag/2021-11-02)
15
28
 
16
29
  *released on 2021-11-02*
17
30
 
18
- * `add` [#94](https://github.com/metabahn/corerb/pull/94) Initial implementation of the core-loader gem ([bryanp](https://github.com/bryanp))
31
+ * `add` [#94](https://github.com/bryanp/corerb/pull/94) Initial implementation of the core-loader gem ([bryanp](https://github.com/bryanp))
19
32
 
20
33
 
@@ -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.2.0"
6
6
 
7
7
  # [public]
8
8
  #
data/lib/core/loader.rb CHANGED
@@ -1,7 +1,90 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "core/extension"
4
+ require "core/state"
5
+
3
6
  module Core
7
+ # [public]
8
+ #
4
9
  module Loader
10
+ require_relative "loader/context"
5
11
  require_relative "loader/version"
12
+
13
+ extend Core::Extension
14
+
15
+ extends dependencies: [Core::State]
16
+
17
+ extends :definition do
18
+ # [public]
19
+ #
20
+ def loads(definable, path, pattern: "*.rb", strict: true, context: Core::Loader::Context)
21
+ unless ancestors.include?(Core::Factory) && makes?(definable)
22
+ raise "cannot load unknown object `#{definable}`"
23
+ end
24
+
25
+ __loads__[definable.to_sym] = {
26
+ type: definable,
27
+ path: Pathname(path),
28
+ pattern: pattern,
29
+ strict: strict,
30
+ context: context
31
+ }
32
+ end
33
+
34
+ # [public]
35
+ #
36
+ def load(reload: false)
37
+ loadable = []
38
+
39
+ __loads__.each_pair do |definable, options|
40
+ find_loadable_paths(options[:path], options, loadable)
41
+ end
42
+
43
+ while (loadable_path, loadable_options = loadable.shift)
44
+ if reload || !loaded?(loadable_path)
45
+ loadable_options[:context].load(
46
+ path: loadable_path,
47
+ root: loadable_options[:path],
48
+ type: loadable_options[:type],
49
+ strict: loadable_options[:strict],
50
+ target: self
51
+ )
52
+
53
+ __loaded_paths__ << loadable_path
54
+ end
55
+ end
56
+ end
57
+
58
+ # [public]
59
+ #
60
+ def reload
61
+ load(reload: true)
62
+ end
63
+
64
+ # [public]
65
+ #
66
+ def reset
67
+ __loaded_paths__.clear
68
+ end
69
+
70
+ private def loaded?(path)
71
+ __loaded_paths__.include?(Pathname(path))
72
+ end
73
+
74
+ private def find_loadable_paths(path, options, loadable)
75
+ path.glob(options[:pattern]).sort.each do |each_path|
76
+ loadable << [each_path, options]
77
+ end
78
+
79
+ path.glob("*").select(&:directory?).sort.each do |each_path|
80
+ find_loadable_paths(each_path, options, loadable)
81
+ end
82
+ end
83
+ end
84
+
85
+ applies do
86
+ state :__loads__, :class, default: {}
87
+ state :__loaded_paths__, :class, default: []
88
+ end
6
89
  end
7
90
  end
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.2.0
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: 2023-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: core-extension
@@ -16,44 +16,44 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.3'
19
+ version: '0.5'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.3'
26
+ version: '0.5'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: core-factory
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.0'
33
+ version: '0.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.0'
40
+ version: '0.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: core-state
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.1'
47
+ version: '0.2'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0.1'
54
+ version: '0.2'
55
55
  description: Load Ruby objects from the filesystem.
56
- email: bryan@metabahn.com
56
+ email: bryan@bryanp.org
57
57
  executables: []
58
58
  extensions: []
59
59
  extra_rdoc_files: []
@@ -63,8 +63,7 @@ files:
63
63
  - lib/core/loader.rb
64
64
  - lib/core/loader/context.rb
65
65
  - lib/core/loader/version.rb
66
- - lib/is/loadable.rb
67
- homepage: https://github.com/metabahn/corerb/
66
+ homepage: https://github.com/bryanp/corerb/
68
67
  licenses:
69
68
  - MPL-2.0
70
69
  metadata: {}
@@ -76,14 +75,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
75
  requirements:
77
76
  - - ">="
78
77
  - !ruby/object:Gem::Version
79
- version: '2.7'
78
+ version: '3.0'
80
79
  required_rubygems_version: !ruby/object:Gem::Requirement
81
80
  requirements:
82
81
  - - ">="
83
82
  - !ruby/object:Gem::Version
84
83
  version: '0'
85
84
  requirements: []
86
- rubygems_version: 3.2.22
85
+ rubygems_version: 3.5.1
87
86
  signing_key:
88
87
  specification_version: 4
89
88
  summary: Load Ruby objects from the filesystem.
data/lib/is/loadable.rb DELETED
@@ -1,89 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "is/extension"
4
- require "is/stateful"
5
-
6
- require_relative "../core/loader/context"
7
-
8
- module Is
9
- # [public]
10
- #
11
- module Loadable
12
- extend Is::Extension
13
-
14
- extends dependencies: [Is::Stateful]
15
-
16
- extends :definition do
17
- # [public]
18
- #
19
- def loads(definable, path, pattern: "*.rb", strict: true, context: Core::Loader::Context)
20
- unless ancestors.include?(Is::Factory) && makes?(definable)
21
- raise "cannot load unknown object `#{definable}`"
22
- end
23
-
24
- __loads__[definable.to_sym] = {
25
- type: definable,
26
- path: Pathname(path),
27
- pattern: pattern,
28
- strict: strict,
29
- context: context
30
- }
31
- end
32
-
33
- # [public]
34
- #
35
- def load(reload: false)
36
- loadable = []
37
-
38
- __loads__.each_pair do |definable, options|
39
- find_loadable_paths(options[:path], options, loadable)
40
- end
41
-
42
- while (loadable_path, loadable_options = loadable.shift)
43
- if reload || !loaded?(loadable_path)
44
- loadable_options[:context].load(
45
- path: loadable_path,
46
- root: loadable_options[:path],
47
- type: loadable_options[:type],
48
- strict: loadable_options[:strict],
49
- target: self
50
- )
51
-
52
- __loaded_paths__ << loadable_path
53
- end
54
- end
55
- end
56
-
57
- # [public]
58
- #
59
- def reload
60
- load(reload: true)
61
- end
62
-
63
- # [public]
64
- #
65
- def reset
66
- __loaded_paths__.clear
67
- end
68
-
69
- private def loaded?(path)
70
- __loaded_paths__.include?(Pathname(path))
71
- end
72
-
73
- private def find_loadable_paths(path, options, loadable)
74
- path.glob(options[:pattern]).sort.each do |each_path|
75
- loadable << [each_path, options]
76
- end
77
-
78
- path.glob("*").select(&:directory?).sort.each do |each_path|
79
- find_loadable_paths(each_path, options, loadable)
80
- end
81
- end
82
- end
83
-
84
- applies do
85
- state :__loads__, :class, default: {}
86
- state :__loaded_paths__, :class, default: []
87
- end
88
- end
89
- end