class Reflective::Type

Public Class Methods

for(name) click to toggle source
# File lib/reflective/type.rb, line 5
def self.for(name)
  constant = Object.const_get(name, false)
  new(constant)
end
new(constant) click to toggle source
# File lib/reflective/type.rb, line 10
def initialize(constant)
  @constant = constant
end

Public Instance Methods

class() click to toggle source
# File lib/reflective/type.rb, line 41
def class
  Kernel.instance_method(:class).bind(@constant).call
end
constants() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
name() click to toggle source
# File lib/reflective/type.rb, line 37
def name
  Module.instance_method(:name).bind(@constant).call
end
public?() click to toggle source
# 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
superclass() click to toggle source
# File lib/reflective/type.rb, line 45
def superclass
  return unless Class == @constant
  Class.instance_method(:superclass).bind(@constant).call
end

Private Instance Methods

method_names_by_visibility(mod) click to toggle source
# File lib/reflective/type.rb, line 86
def method_names_by_visibility(mod)
  {
    public: Module.instance_method(:public_instance_methods).bind(mod).call,
    protected: Module.instance_method(:protected_instance_methods).bind(mod).call,
    private: Module.instance_method(:private_instance_methods).bind(mod).call,
  }
end
methods_for(mod, own:, visibility:) click to toggle source
# File lib/reflective/type.rb, line 69
def methods_for(mod, own:, visibility:)
  method_names_by_visibility(mod)
    .delete_if { |vis, _method_list| !visibility.include?(vis) }
    .flat_map do |visibility, method_list|
      method_list
        .sort!
        .map do |name|
          next if name == :initialize
          MethodInfo.new(mod, name, visibility: visibility)
        end
        .compact
        .select do |method_info|
          !own || method_info.owner == mod
        end
    end
end