evt-constant 2.1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 37688b1a76f8bdfc93bbbcd855c899a1b8a7d62f474e222f1f21e54c9190894a
4
+ data.tar.gz: d524e8c0d42196c7ae0ed435cb5f12ed582b519d1e9f2e32020447bc24f860c8
5
+ SHA512:
6
+ metadata.gz: 625602ca4e22aef84ec15503580c73df54af1b6df8b65cd7027f32d6e86de16ddc9174f59f7be511b142584e549dc287046f308e380f2fe4ebe5deb5e5884b2c
7
+ data.tar.gz: e6ff42d4593cbc572b44d2382c8e221c28b87ce8e25c979836c0224f44094a9b7700e4ddaf5dc35b93b3e6f2d722be59f2d09be0284e6e1303dd28e8a76aff23
@@ -0,0 +1,16 @@
1
+ module Constant
2
+ module Coerce
3
+ refine Kernel do
4
+ def Constant(value, namespace=nil, inherit: nil)
5
+ if value.is_a?(Constant)
6
+ value
7
+ elsif value.is_a?(::Module) || value.is_a?(::String) || value.is_a?(::Symbol)
8
+ Constant.get(value, namespace, inherit: inherit)
9
+ else
10
+ type_name = value.nil? ? "nil" : value.class
11
+ raise TypeError, "can't convert #{type_name} into Constant"
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,72 @@
1
+ module Constant
2
+ Error = Class.new(RuntimeError)
3
+
4
+ class << self
5
+ alias __name name
6
+ end
7
+
8
+ def self.get(value, namespace=nil, inherit: nil)
9
+ namespace ||= Object
10
+
11
+ if value.is_a?(::Module)
12
+ Constant::Module.build(value)
13
+ else
14
+ if namespace.is_a?(::Module) || namespace.is_a?(Constant)
15
+ namespace_constant = Constant::Module.build(namespace)
16
+ else
17
+ namespace_constant = get(namespace)
18
+ end
19
+
20
+ namespace_constant.get(value, inherit: inherit)
21
+ end
22
+ end
23
+
24
+ def self.name(mod)
25
+ name = mod.name
26
+
27
+ if name.nil?
28
+ return nil
29
+ end
30
+
31
+ name.rpartition("::").last
32
+ end
33
+
34
+ def self.namespace(mod)
35
+ name = mod.name
36
+
37
+ if name.nil?
38
+ return nil
39
+ end
40
+
41
+ namespace_name = name.rpartition("::").first
42
+
43
+ if namespace_name.empty?
44
+ Constant::Module.new(Object)
45
+ else
46
+ namespace_mod = Object.const_get(namespace_name)
47
+ Constant::Module.new(namespace_mod)
48
+ end
49
+ end
50
+
51
+ def self.defined?(name, namespace_name_or_module=nil, inherit: nil)
52
+ namespace_name_or_module ||= Object
53
+
54
+ namespace_constant = get(namespace_name_or_module)
55
+ namespace_constant.get(name, inherit: inherit)
56
+ true
57
+ rescue Constant::Error # #get raises when the name is undefined, or its path runs through a literal
58
+ false
59
+ end
60
+
61
+ def ==(other)
62
+ other.is_a?(Constant) && identity == other.identity
63
+ end
64
+
65
+ def eql?(other)
66
+ self == other
67
+ end
68
+
69
+ def hash
70
+ identity.hash
71
+ end
72
+ end
@@ -0,0 +1,55 @@
1
+ module Constant
2
+ module Controls
3
+ module Constant
4
+ def self.example(name: nil, randomize_name: nil, inner_constants: nil, ancestor: nil)
5
+ inner_constants ||= []
6
+
7
+ mod = ::Module.new
8
+
9
+ name ||= "ExampleModule"
10
+
11
+ if [nil, true].include?(randomize_name)
12
+ name = "#{name}_#{SecureRandom.hex(2).upcase}"
13
+ end
14
+
15
+ Object.const_set(name, mod)
16
+
17
+ if not inner_constants.empty?
18
+ add_inner_constants(mod, inner_constants)
19
+ end
20
+
21
+ if not ancestor.nil?
22
+ add_ancestors(mod, ancestor)
23
+ end
24
+
25
+ mod
26
+ end
27
+
28
+ def self.add_ancestors(mod, ancestors)
29
+ ancestors.each do |ancestor_name, ancestor_inner_constants|
30
+ ancestor_module = example(name: ancestor_name, inner_constants: ancestor_inner_constants)
31
+ mod.include(ancestor_module)
32
+ end
33
+ end
34
+
35
+ def self.add_inner_constants(mod, inner_constants)
36
+ if inner_constants.is_a?(Hash)
37
+ inner_constants.each do |inner_constant_name, inner_constant_value|
38
+ if inner_constant_value.is_a?(Hash)
39
+ inner_mod = ::Module.new
40
+ mod.const_set(inner_constant_name, inner_mod)
41
+ add_inner_constants(inner_mod, inner_constant_value)
42
+ else
43
+ mod.const_set(inner_constant_name, inner_constant_value)
44
+ end
45
+ end
46
+ else
47
+ inner_constants.each do |inner_constant_name|
48
+ inner_constant_value = ::Module.new
49
+ mod.const_set(inner_constant_name, inner_constant_value)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ require "securerandom"
2
+
3
+ require "constant/controls/constant"
@@ -0,0 +1,9 @@
1
+ module Constant
2
+ module Define
3
+ def self.call(constant_name, destination_constant, constant_value=nil)
4
+ constant_value = ::Module.new if constant_value.nil?
5
+ destination_constant.const_set(constant_name, constant_value)
6
+ constant_value
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module Constant
2
+ module Import
3
+ module Macro
4
+ def __import_constant(origin_constant, **kwargs)
5
+ Import.(origin_constant, self, **kwargs)
6
+ end
7
+
8
+ alias import __import_constant
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,35 @@
1
+ module Constant
2
+ module Import
3
+ Error = Class.new(RuntimeError)
4
+
5
+ def self.included(base)
6
+ base.extend(Macro)
7
+ end
8
+
9
+ def self.call(origin_constant, destination_constant, **kwargs)
10
+ alias_name = kwargs[:alias]
11
+
12
+ if alias_name.nil? && destination_constant.ancestors.include?(origin_constant)
13
+ raise Error, "#{destination_constant} already includes #{origin_constant}"
14
+ end
15
+
16
+ target = destination_constant
17
+
18
+ if not alias_name.nil?
19
+ target = Define.(alias_name, destination_constant)
20
+ end
21
+
22
+ inherit = false
23
+
24
+ import_constant_names = origin_constant.constants(inherit)
25
+
26
+ imported_constants = import_constant_names.map do |import_constant_name|
27
+ import_constant = origin_constant.const_get(import_constant_name, inherit)
28
+ target.const_set(import_constant_name, import_constant)
29
+ import_constant
30
+ end
31
+
32
+ imported_constants
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,38 @@
1
+ module Constant
2
+ class Literal
3
+ include Constant
4
+ include Initializer
5
+
6
+ initializer :name, :value, :namespace
7
+
8
+ def self.build(name, value, namespace)
9
+ name = name.to_s
10
+ namespace = Constant::Module.build(namespace)
11
+ new(name, value, namespace)
12
+ end
13
+
14
+ def full_name
15
+ if namespace.value.equal?(Object)
16
+ name
17
+ else
18
+ "#{namespace.full_name}::#{name}"
19
+ end
20
+ end
21
+
22
+ def identity
23
+ full_name
24
+ end
25
+
26
+ def constants(inherit: nil)
27
+ []
28
+ end
29
+
30
+ def get(name, inherit: nil)
31
+ raise Constant::Error, "Literal constants are primitive values. They don't support inner constants. #{name} is not defined in #{full_name}."
32
+ end
33
+
34
+ def defined?(name_or_module, inherit: nil)
35
+ false
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,101 @@
1
+ module Constant
2
+ class Module
3
+ include Constant
4
+ include Initializer
5
+
6
+ initializer :value
7
+
8
+ def name
9
+ Constant.name(value)
10
+ end
11
+
12
+ def full_name
13
+ value.name
14
+ end
15
+
16
+ def namespace
17
+ Constant.namespace(value)
18
+ end
19
+
20
+ def self.build(mod)
21
+ if mod.is_a?(Constant)
22
+ mod
23
+ else
24
+ new(mod)
25
+ end
26
+ end
27
+
28
+ def get(name, inherit: nil)
29
+ inherit ||= false
30
+
31
+ name = name.to_s
32
+ head, _, rest = name.partition("::")
33
+
34
+ if not rest.empty?
35
+ head_constant = get(head, inherit: inherit)
36
+ return head_constant.get(rest, inherit: inherit)
37
+ end
38
+
39
+ if not value.const_defined?(name, inherit)
40
+ raise Constant::Error, "#{name} is not defined in #{value}"
41
+ end
42
+
43
+ resolved = value.const_get(name, inherit)
44
+
45
+ if resolved.is_a?(::Module)
46
+ Constant::Module.build(resolved)
47
+ else
48
+ Constant::Literal.build(name, resolved, self)
49
+ end
50
+ end
51
+
52
+ def constants(include_literal_constants: nil, inherit: nil)
53
+ include_literal_constants ||= false
54
+ inherit ||= false
55
+
56
+ constant_symbols = value.constants(inherit)
57
+
58
+ constant_symbols.filter_map do |constant_name|
59
+ resolved = value.const_get(constant_name, inherit)
60
+
61
+ if resolved.is_a?(::Module)
62
+ Constant::Module.build(resolved)
63
+ elsif include_literal_constants
64
+ Constant::Literal.build(constant_name, resolved, self)
65
+ end
66
+ end
67
+ end
68
+
69
+ def constant_names(include_literal_constants: nil, inherit: nil)
70
+ include_literal_constants ||= false
71
+ inherit ||= false
72
+
73
+ constant_symbols = value.constants(inherit)
74
+
75
+ constant_symbols.filter_map do |constant_symbol|
76
+ resolved = value.const_get(constant_symbol, inherit)
77
+
78
+ if resolved.is_a?(::Module) || include_literal_constants
79
+ constant_symbol.to_s
80
+ end
81
+ end
82
+ end
83
+
84
+ def defined?(name_or_module, inherit: nil)
85
+ if name_or_module.is_a?(::Module)
86
+ inherit ||= false
87
+
88
+ value.constants(inherit).any? do |constant_name|
89
+ resolved = value.const_get(constant_name, inherit)
90
+ resolved.equal?(name_or_module)
91
+ end
92
+ else
93
+ Constant.defined?(name_or_module, value, inherit: inherit)
94
+ end
95
+ end
96
+
97
+ def identity
98
+ value
99
+ end
100
+ end
101
+ end
data/lib/constant.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "initializer"
2
+
3
+ require "constant/constant"
4
+ require "constant/module"
5
+ require "constant/literal"
6
+ require "constant/coerce"
7
+ require "constant/define"
8
+ require "constant/import"
9
+ require "constant/import/macro"
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: evt-constant
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - The Eventide Project
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: evt-initializer
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: test_bench
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ description: Utilities for working with constants
41
+ email: opensource@eventide-project.org
42
+ executables: []
43
+ extensions: []
44
+ extra_rdoc_files: []
45
+ files:
46
+ - lib/constant.rb
47
+ - lib/constant/coerce.rb
48
+ - lib/constant/constant.rb
49
+ - lib/constant/controls.rb
50
+ - lib/constant/controls/constant.rb
51
+ - lib/constant/define.rb
52
+ - lib/constant/import.rb
53
+ - lib/constant/import/macro.rb
54
+ - lib/constant/literal.rb
55
+ - lib/constant/module.rb
56
+ homepage: https://github.com/eventide-project/constant
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '4'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 4.0.3
75
+ specification_version: 4
76
+ summary: ''
77
+ test_files: []