Class: Reflective::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/reflective/type.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constant) ⇒ Type

Returns a new instance of Type.



10
11
12
# File 'lib/reflective/type.rb', line 10

def initialize(constant)
  @constant = constant
end

Class Method Details

.for(name) ⇒ Object



5
6
7
8
# File 'lib/reflective/type.rb', line 5

def self.for(name)
  constant = Object.const_get(name, false)
  new(constant)
end

Instance Method Details

#classObject



41
42
43
# File 'lib/reflective/type.rb', line 41

def class
  Kernel.instance_method(:class).bind(@constant).call
end

#constantsObject



14
15
16
17
18
19
# File 'lib/reflective/type.rb', line 14

def constants
  constants = Module.instance_method(:constants).bind(@constant).call(false)
  constants.map do |const|
    [const, @constant.const_get(const, false)]
  end.to_h
end

#equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/reflective/type.rb', line 50

def equal?(other)
  BasicObject.instance_method(:equal?).bind(@constant).call(other)
end

#methods(own: true, visibility: [:public, :protected, :private], kind: :instance) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/reflective/type.rb', line 21

def methods(own: true, visibility: [:public, :protected, :private], kind: :instance)
  kind = Array(kind)
  visibility = Array(visibility)
  methods = []

  if kind.include?(:instance)
    methods.concat(methods_for(@constant, own: own, visibility: visibility))
  end

  if kind.include?(:class)
    methods.concat(methods_for(@constant.singleton_class, own: own, visibility: visibility))
  end

  methods
end

#nameObject



37
38
39
# File 'lib/reflective/type.rb', line 37

def name
  Module.instance_method(:name).bind(@constant).call
end

#public?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/reflective/type.rb', line 54

def public?
  constant_name = name
  return false unless constant_name

  begin
    # can't use !! here because the constant might override ! and mess with us
    eval(constant_name) # rubocop:disable Security/Eval
    true
  rescue NameError
    false
  end
end

#superclassObject



45
46
47
48
# File 'lib/reflective/type.rb', line 45

def superclass
  return unless Class == @constant
  Class.instance_method(:superclass).bind(@constant).call
end