evt-reflect 0.0.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 +7 -0
- data/lib/reflect/controls/namespace.rb +11 -0
- data/lib/reflect/controls/subject.rb +30 -0
- data/lib/reflect/controls.rb +4 -0
- data/lib/reflect/reflect.rb +35 -0
- data/lib/reflect/reflection.rb +62 -0
- data/lib/reflect.rb +2 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1381ac92991e6aafc877e6812e0baf2c77c7b6519345e0c23d6e7fd28832ef95
|
4
|
+
data.tar.gz: 00c084d9fc282dae0140555bf4a86611541b4e27deaadfc18cfd4199303e8659
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 18b3ab2ac9cf473ce165335ae03040daca0407daabdff9fffa31d2da42d8f193190492f1ef7e20b8ebefe9d2fad00e35ccade44ebc376aa5767c36ca117c0d92
|
7
|
+
data.tar.gz: b87b09a92bddc35bf7d75ac6381af84bf8ededd17af7a2dc6a78d17f7515f006c15bc0224c19b353848018901a094fbce61c3cdc5c6f4b26df635da10b348a79
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Reflect
|
2
|
+
module Controls
|
3
|
+
module Subject
|
4
|
+
def self.example
|
5
|
+
Example.new
|
6
|
+
end
|
7
|
+
|
8
|
+
class Example
|
9
|
+
module SomeConstant
|
10
|
+
def self.some_accessor
|
11
|
+
SomeInnerConstant
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.some_object_accessor
|
15
|
+
SomeInnerClass.new
|
16
|
+
end
|
17
|
+
|
18
|
+
module SomeInnerConstant
|
19
|
+
end
|
20
|
+
|
21
|
+
class SomeInnerClass
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module ConstantWithoutAccessor
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Reflect
|
2
|
+
Error = Class.new(RuntimeError)
|
3
|
+
|
4
|
+
def self.call(subject, constant_name, strict: nil)
|
5
|
+
Reflection.build(subject, constant_name, strict: strict)
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.subject_constant(subject)
|
9
|
+
[Module, Class].include?(subject.class) ? subject : subject.class
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.get_constant(subject_constant, constant_name, strict: nil)
|
13
|
+
strict = Reflection::Default.strict if strict.nil?
|
14
|
+
|
15
|
+
constant = nil
|
16
|
+
|
17
|
+
if constant?(subject_constant, constant_name)
|
18
|
+
constant = get_constant!(subject_constant, constant_name)
|
19
|
+
end
|
20
|
+
|
21
|
+
if constant.nil? && strict
|
22
|
+
raise Reflect::Error, "Namespace #{constant_name} is not defined in #{subject_constant.name}"
|
23
|
+
end
|
24
|
+
|
25
|
+
constant
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.get_constant!(subject_constant, constant_name)
|
29
|
+
subject_constant.const_get(constant_name, false)
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.constant?(subject_constant, constant_name)
|
33
|
+
subject_constant.const_defined?(constant_name, false)
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Reflect
|
2
|
+
class Reflection
|
3
|
+
attr_reader :subject
|
4
|
+
attr_reader :constant
|
5
|
+
attr_reader :strict
|
6
|
+
|
7
|
+
def subject_constant
|
8
|
+
@subject_constant ||= Reflect.subject_constant(subject)
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(subject, constant, strict)
|
12
|
+
@subject = subject
|
13
|
+
@constant = constant
|
14
|
+
@strict = strict
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.call(subject, constant_name, strict: nil)
|
18
|
+
build(subject, constant_name, strict: strict)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.build(subject, constant_name, strict: nil)
|
22
|
+
strict = Default.strict if strict.nil?
|
23
|
+
|
24
|
+
subject_constant = Reflect.subject_constant(subject)
|
25
|
+
|
26
|
+
constant = Reflect.get_constant(subject_constant, constant_name, strict: strict)
|
27
|
+
return nil if constant.nil?
|
28
|
+
|
29
|
+
instance = new(subject, constant, strict)
|
30
|
+
end
|
31
|
+
|
32
|
+
def constant_accessor?(name)
|
33
|
+
constant.respond_to?(name)
|
34
|
+
end
|
35
|
+
|
36
|
+
def get(accessor_name)
|
37
|
+
result = get_constant(accessor_name)
|
38
|
+
return nil if result.nil?
|
39
|
+
|
40
|
+
constant = Reflect.subject_constant(result)
|
41
|
+
self.class.new(subject, constant, strict)
|
42
|
+
end
|
43
|
+
|
44
|
+
def get_constant(accessor_name)
|
45
|
+
if !constant_accessor?(accessor_name)
|
46
|
+
if strict
|
47
|
+
raise Reflect::Error, "Constant #{constant.name} does not have accessor #{accessor_name}"
|
48
|
+
else
|
49
|
+
return nil
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
constant.send(accessor_name)
|
54
|
+
end
|
55
|
+
|
56
|
+
module Default
|
57
|
+
def self.strict
|
58
|
+
true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/reflect.rb
ADDED
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: evt-reflect
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- The Eventide Project
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: test_bench
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: " "
|
28
|
+
email: opensource@eventide-project.org
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/reflect.rb
|
34
|
+
- lib/reflect/controls.rb
|
35
|
+
- lib/reflect/controls/namespace.rb
|
36
|
+
- lib/reflect/controls/subject.rb
|
37
|
+
- lib/reflect/reflect.rb
|
38
|
+
- lib/reflect/reflection.rb
|
39
|
+
homepage: https://github.com/eventide-project/reflect
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata: {}
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: 2.3.3
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 2.7.3
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: Reflection of inner namespaces used for protocol discovery
|
63
|
+
test_files: []
|