evt-reflect 0.1.0.0 → 2.2.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee9d1cbec147b0821067b19f44ed87f9a775dae5fdbdddb7c0aec6ad1075cd38
4
- data.tar.gz: ef299e3d6a8a6ff804843ec2b7903429472c1a9dba1c58b4dca11ccbb5d6ea18
3
+ metadata.gz: ada05571eb5a91ea63c63d8759d19d34db5d6241aa26cdfbdb2f48930e23650e
4
+ data.tar.gz: 2f98c007347876f5a4643cb736ec2f296958c5e7fa565b46f3827c0a6b319e0e
5
5
  SHA512:
6
- metadata.gz: 98f99e913466aadd2bac00430a7f9882a7ef6a17c08e235d3d1f32b07c493b488a75f6c601cc847bb671a96747b94ded17f3d1a6f519c370776cf57deefad5aa
7
- data.tar.gz: 6c5ac0da38a42f352e9cd78d003ed5d66d7a447bf8ed4daee3dc5625090f8c5c6e63b6a1a5e982ba4065e53367cb8f3971b2fa92ff3d7d74778f98ebd89618ad
6
+ metadata.gz: 3a68f06bf7be25bd2dcaabc4b74c8642805586321fbe5ce390bfdce77111e3056fdaa310d5c2a5b5c34f5a6dd1a3f604f20016d357d253b184c422c11873660d
7
+ data.tar.gz: 25c76a793b5f2f7033a3a114b936b3cf19e3dd168e6a3083dae00dc2b02b81d88c419c5ac0def7afcd2d908f689058c36617edb31e37935007a572f109d658de
@@ -2,3 +2,4 @@ require 'securerandom'
2
2
 
3
3
  require 'reflect/controls/namespace'
4
4
  require 'reflect/controls/subject'
5
+ require 'reflect/controls/ancestor'
@@ -0,0 +1,17 @@
1
+ module Reflect
2
+ module Controls
3
+ module Ancestor
4
+ def self.example
5
+ Example.new
6
+ end
7
+
8
+ class Example
9
+ module SomeConstant
10
+ end
11
+
12
+ class Descendant < Example
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,21 +1,22 @@
1
1
  module Reflect
2
2
  Error = Class.new(RuntimeError)
3
3
 
4
- def self.call(subject, constant_name, strict: nil)
5
- Reflection.build(subject, constant_name, strict: strict)
4
+ def self.call(subject, constant_name, strict: nil, ancestors: nil)
5
+ Reflection.build(subject, constant_name, strict: strict, ancestors: ancestors)
6
6
  end
7
7
 
8
- def self.subject_constant(subject)
8
+ def self.constant(subject)
9
9
  [Module, Class].include?(subject.class) ? subject : subject.class
10
10
  end
11
11
 
12
- def self.get_constant(subject_constant, constant_name, strict: nil)
12
+ def self.get_constant(subject_constant, constant_name, strict: nil, ancestors: nil)
13
13
  strict = Reflection::Default.strict if strict.nil?
14
+ ancestors = Reflection::Default.ancestors if ancestors.nil?
14
15
 
15
16
  constant = nil
16
17
 
17
- if constant?(subject_constant, constant_name)
18
- constant = get_constant!(subject_constant, constant_name)
18
+ if constant?(subject_constant, constant_name, ancestors: ancestors)
19
+ constant = get_constant!(subject_constant, constant_name, ancestors: ancestors)
19
20
  end
20
21
 
21
22
  if constant.nil? && strict
@@ -25,11 +26,13 @@ module Reflect
25
26
  constant
26
27
  end
27
28
 
28
- def self.get_constant!(subject_constant, constant_name)
29
- subject_constant.const_get(constant_name, false)
29
+ def self.get_constant!(subject_constant, constant_name, ancestors: nil)
30
+ ancestors = Reflection::Default.ancestors if ancestors.nil?
31
+ subject_constant.const_get(constant_name, ancestors)
30
32
  end
31
33
 
32
- def self.constant?(subject_constant, constant_name)
33
- subject_constant.const_defined?(constant_name, false)
34
+ def self.constant?(subject_constant, constant_name, ancestors: nil)
35
+ ancestors = Reflection::Default.ancestors if ancestors.nil?
36
+ subject_constant.const_defined?(constant_name, ancestors)
34
37
  end
35
38
  end
@@ -1,68 +1,85 @@
1
1
  module Reflect
2
2
  class Reflection
3
3
  attr_reader :subject
4
- attr_reader :constant
4
+ attr_reader :target
5
+ alias :constant :target
5
6
  attr_reader :strict
6
7
 
7
8
  def subject_constant
8
- @subject_constant ||= Reflect.subject_constant(subject)
9
+ @subject_constant ||= Reflect.constant(subject)
9
10
  end
10
11
 
11
- def initialize(subject, constant, strict)
12
+ def initialize(subject, target, strict)
12
13
  @subject = subject
13
- @constant = constant
14
+ @target = target
14
15
  @strict = strict
15
16
  end
16
17
 
18
+ def self.build(subject, constant_name, strict: nil, ancestors: nil)
19
+ strict = Default.strict if strict.nil?
20
+ ancestors = Default.ancestors if ancestors.nil?
21
+
22
+ subject_constant = Reflect.constant(subject)
23
+
24
+ target = Reflect.get_constant(subject_constant, constant_name, strict: strict, ancestors: ancestors)
25
+ return nil if target.nil?
26
+
27
+ new(subject, target, strict)
28
+ end
29
+
17
30
  def call(method_name, arg=nil)
18
- unless constant.respond_to?(method_name)
19
- raise Reflect::Error, "Constant #{constant.name} does not define method #{method_name}"
31
+ unless target.respond_to?(method_name)
32
+ target_name = Reflect.constant(target).name
33
+ raise Reflect::Error, "#{target_name} does not define method #{method_name}"
20
34
  end
21
35
 
22
36
  arg ||= subject
23
37
 
24
- constant.send(method_name, arg)
38
+ target.send(method_name, arg)
25
39
  end
26
40
 
27
- def self.build(subject, constant_name, strict: nil)
28
- strict = Default.strict if strict.nil?
41
+ def target_accessor?(name, subject=nil)
42
+ subject ||= constant
43
+ subject.respond_to?(name)
44
+ end
29
45
 
30
- subject_constant = Reflect.subject_constant(subject)
46
+ def get(accessor_name, strict: nil, coerce_constant: nil)
47
+ strict = self.strict if strict.nil?
48
+ coerce_constant = true if coerce_constant.nil?
31
49
 
32
- constant = Reflect.get_constant(subject_constant, constant_name, strict: strict)
33
- return nil if constant.nil?
50
+ target = get_target(accessor_name, strict: strict)
51
+ return nil if target.nil?
34
52
 
35
- instance = new(subject, constant, strict)
36
- end
53
+ if coerce_constant
54
+ target = Reflect.constant(target)
55
+ end
37
56
 
38
- def constant_accessor?(name)
39
- constant.respond_to?(name)
57
+ self.class.new(subject, target, strict)
40
58
  end
41
59
 
42
- def get(accessor_name)
43
- result = get_constant(accessor_name)
44
- return nil if result.nil?
60
+ def get_target(accessor_name, strict: nil)
61
+ strict = self.strict if strict.nil?
45
62
 
46
- constant = Reflect.subject_constant(result)
47
- self.class.new(subject, constant, strict)
48
- end
49
-
50
- def get_constant(accessor_name)
51
- if !constant_accessor?(accessor_name)
63
+ if !target_accessor?(accessor_name)
52
64
  if strict
53
- raise Reflect::Error, "Constant #{constant.name} does not have accessor #{accessor_name}"
65
+ target_name = Reflect.constant(target).name
66
+ raise Reflect::Error, "#{target_name} does not have accessor #{accessor_name}"
54
67
  else
55
68
  return nil
56
69
  end
57
70
  end
58
71
 
59
- constant.send(accessor_name)
72
+ target.send(accessor_name)
60
73
  end
61
74
 
62
75
  module Default
63
76
  def self.strict
64
77
  true
65
78
  end
79
+
80
+ def self.ancestors
81
+ false
82
+ end
66
83
  end
67
84
  end
68
85
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evt-reflect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.0
4
+ version: 2.2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Eventide Project
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-04 00:00:00.000000000 Z
11
+ date: 2021-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test_bench
@@ -32,6 +32,7 @@ extra_rdoc_files: []
32
32
  files:
33
33
  - lib/reflect.rb
34
34
  - lib/reflect/controls.rb
35
+ - lib/reflect/controls/ancestor.rb
35
36
  - lib/reflect/controls/namespace.rb
36
37
  - lib/reflect/controls/subject.rb
37
38
  - lib/reflect/reflect.rb
@@ -40,7 +41,7 @@ homepage: https://github.com/eventide-project/reflect
40
41
  licenses:
41
42
  - MIT
42
43
  metadata: {}
43
- post_install_message:
44
+ post_install_message:
44
45
  rdoc_options: []
45
46
  require_paths:
46
47
  - lib
@@ -55,9 +56,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
56
  - !ruby/object:Gem::Version
56
57
  version: '0'
57
58
  requirements: []
58
- rubyforge_project:
59
- rubygems_version: 2.7.3
60
- signing_key:
59
+ rubygems_version: 3.1.2
60
+ signing_key:
61
61
  specification_version: 4
62
62
  summary: Reflection of inner namespaces used for protocol discovery
63
63
  test_files: []