evt-constant 2.1.1.0 → 2.2.1.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: d0e34b89c8c9aec6ef54da871a992f086616ceee7f411ee1b43bf070cc9b80de
4
- data.tar.gz: 34e6bf8e3b33b7fd2260e75400a9eed3ac82ebe8e3d86ff7b94b82877c3ec7ad
3
+ metadata.gz: ff234cd73757812512933c025d3b6145ec09d50c86638db927ca5000feee1f6c
4
+ data.tar.gz: 89749a6d6d39f569b37e3a9ed092565c516a76c0e1fe34431b8efd551f4db096
5
5
  SHA512:
6
- metadata.gz: 52f10483f068718f662c3471ab19b397dc33deaf477bd741b5359b7d8e71e8daff8df05a014c0cd5e6f04a427aa0fdc95b835f03dfafd2f06152fb7596041ac8
7
- data.tar.gz: 86d07e6c53ec8cdbda773639f61e0a82ed9f7b20375fa966f1289a3f7e48c23147b2911c55ac703c2106becf7f4ed424ccfbeded5cb622b77b7d1c6974f50392
6
+ metadata.gz: f92b015b8fcbe17dd1ea04aa55f2855754882a1d951a95f3939728b76946a684de6236a35d45caed723247f2475aa9d4b0d131f6de464c225b17277d7937edef
7
+ data.tar.gz: 10ffaabb126ff50093b231337ff3e77b0295b028e1ca40a9bd6a4282c8768a386adf019b2644d0e2d0aa43cd268bb56531920574b1f27cf4f49a5dce6bcf8a99
@@ -3,8 +3,8 @@ require "open3"
3
3
  module Constant
4
4
  module Controls
5
5
  module Script
6
- def self.top_level_import(origin_name: nil, inner_constants: nil)
7
- origin_name ||= "SomeOrigin"
6
+ def self.top_level_import(source_name: nil, inner_constants: nil)
7
+ source_name ||= "SomeSource"
8
8
  inner_constants ||= ["SomeInnerConstant"]
9
9
 
10
10
  inner_constant_definitions = inner_constants.map do |inner_constant_name|
@@ -21,13 +21,45 @@ module Constant
21
21
  source = <<~RUBY
22
22
  require "constant"
23
23
 
24
- module #{origin_name}
24
+ module #{source_name}
25
25
  #{definitions}
26
26
  end
27
27
 
28
28
  include Constant::Import
29
29
 
30
- import #{origin_name}
30
+ import #{source_name}
31
+
32
+ #{resolutions}
33
+ RUBY
34
+
35
+ Example.new(source)
36
+ end
37
+
38
+ def self.top_level_refinement_import(source_name: nil, inner_constants: nil)
39
+ source_name ||= "SomeSource"
40
+ inner_constants ||= ["SomeInnerConstant"]
41
+
42
+ inner_constant_definitions = inner_constants.map do |inner_constant_name|
43
+ "#{inner_constant_name} = ::Module.new"
44
+ end
45
+
46
+ inner_constant_resolutions = inner_constants.map do |inner_constant_name|
47
+ "puts #{inner_constant_name}.name"
48
+ end
49
+
50
+ definitions = inner_constant_definitions.join("\n")
51
+ resolutions = inner_constant_resolutions.join("\n")
52
+
53
+ source = <<~RUBY
54
+ require "constant"
55
+
56
+ module #{source_name}
57
+ #{definitions}
58
+ end
59
+
60
+ using Constant::Import
61
+
62
+ import #{source_name}
31
63
 
32
64
  #{resolutions}
33
65
  RUBY
@@ -41,13 +73,7 @@ module Constant
41
73
  initializer :source
42
74
 
43
75
  def run
44
- load_path_options = $LOAD_PATH.map do |load_path_entry|
45
- "-I#{load_path_entry}"
46
- end
47
-
48
- command = ["ruby", *load_path_options, "-e", source]
49
-
50
- output, error_output, status = Open3.capture3(*command)
76
+ output, error_output, status = execute
51
77
 
52
78
  if not status.success?
53
79
  raise "Script failed\n\n#{source}\n#{error_output}"
@@ -55,6 +81,26 @@ module Constant
55
81
 
56
82
  output.split("\n")
57
83
  end
84
+
85
+ def error
86
+ _, error_output, status = execute
87
+
88
+ if status.success?
89
+ raise "Script succeeded\n\n#{source}"
90
+ end
91
+
92
+ error_output
93
+ end
94
+
95
+ def execute
96
+ load_path_options = $LOAD_PATH.map do |load_path_entry|
97
+ "-I#{load_path_entry}"
98
+ end
99
+
100
+ command = ["ruby", *load_path_options, "-e", source]
101
+
102
+ Open3.capture3(*command)
103
+ end
58
104
  end
59
105
  end
60
106
  end
@@ -2,16 +2,12 @@ module Constant
2
2
  module Import
3
3
  module Macro
4
4
  def __import_constant(origin_constant, **kwargs)
5
- destination = self
6
-
7
- if not destination.is_a?(::Module)
8
- destination = destination.class
9
- end
10
-
11
- Import.(origin_constant, destination, **kwargs)
5
+ Import.(origin_constant, self, **kwargs)
12
6
  end
13
7
 
14
8
  alias import __import_constant
15
9
  end
10
+
11
+ private_constant :Macro
16
12
  end
17
13
  end
@@ -1,14 +1,28 @@
1
1
  module Constant
2
2
  module Import
3
3
  def self.included(base)
4
+ if base.equal?(::Object)
5
+ raise Constant::Error, "Constant::Import cannot be included at the top level, where the destination is Object. Activate the refinement instead: using Constant::Import"
6
+ end
7
+
4
8
  base.extend(Macro)
9
+ end
5
10
 
6
- if base.equal?(::Object)
7
- TOPLEVEL_BINDING.receiver.extend(Macro)
11
+ refine ::Object do
12
+ def __import_constant(origin_constant, **kwargs)
13
+ Import.(origin_constant, self, **kwargs)
8
14
  end
15
+
16
+ alias import __import_constant
9
17
  end
10
18
 
11
- def self.call(origin_constant, destination_constant, **kwargs)
19
+ def self.call(origin_constant, destination_constant, only: nil, except: nil, override_ancestor: nil, **kwargs)
20
+ override_ancestor ||= false
21
+
22
+ if not destination_constant.is_a?(::Module)
23
+ destination_constant = destination_constant.class
24
+ end
25
+
12
26
  alias_name = kwargs[:alias]
13
27
 
14
28
  if alias_name.nil? && destination_constant.ancestors.include?(origin_constant)
@@ -25,6 +39,54 @@ module Constant
25
39
 
26
40
  import_constant_names = origin_constant.constants(inherit)
27
41
 
42
+ only_constant_names = Array(only)
43
+ only_constant_names = only_constant_names.map { |only_constant_name| only_constant_name.to_sym }
44
+
45
+ except_constant_names = Array(except)
46
+ except_constant_names = except_constant_names.map { |except_constant_name| except_constant_name.to_sym }
47
+
48
+ conflicting_constant_names = only_constant_names & except_constant_names
49
+
50
+ if conflicting_constant_names.any?
51
+ conflicting_constant_name = conflicting_constant_names.first
52
+ raise Constant::Error, "#{conflicting_constant_name} is named in both only: and except:"
53
+ end
54
+
55
+ if only_constant_names.any?
56
+ undefined_constant_names = only_constant_names - import_constant_names
57
+
58
+ if undefined_constant_names.any?
59
+ undefined_constant_name = undefined_constant_names.first
60
+ raise Constant::Error, "#{undefined_constant_name} is not defined on #{origin_constant}"
61
+ end
62
+
63
+ import_constant_names = import_constant_names & only_constant_names
64
+ end
65
+
66
+ import_constant_names = import_constant_names - except_constant_names
67
+
68
+ if override_ancestor
69
+ collision_constants = [target]
70
+ else
71
+ collision_constants = target.ancestors
72
+ end
73
+
74
+ import_constant_names.each do |import_constant_name|
75
+ defining_constant = collision_constants.find do |collision_constant|
76
+ collision_constant.const_defined?(import_constant_name, false)
77
+ end
78
+
79
+ if defining_constant.nil?
80
+ next
81
+ end
82
+
83
+ if defining_constant.equal?(target)
84
+ raise Constant::Error, "#{import_constant_name} is already defined on #{target} (imported from #{origin_constant})"
85
+ end
86
+
87
+ raise Constant::Error, "#{import_constant_name} is inherited by #{target} from #{defining_constant} (imported from #{origin_constant})"
88
+ end
89
+
28
90
  imported_constants = import_constant_names.map do |import_constant_name|
29
91
  import_constant = origin_constant.const_get(import_constant_name, inherit)
30
92
  target.const_set(import_constant_name, import_constant)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evt-constant
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1.0
4
+ version: 2.2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Eventide Project