class DissociatedIntrospection::Inspection

Attributes

file[R]
parent_class_replacement[R]

Public Class Methods

new(file:, parent_class_replacement: :RecordingParent) click to toggle source

@param file [File] @optional parent_class_replacement [Symbol]

# File lib/dissociated_introspection/inspection.rb, line 8
def initialize(file:, parent_class_replacement: :RecordingParent)
  @file                     = file
  @parent_class_replacement = parent_class_replacement
end

Public Instance Methods

class_macros() click to toggle source

@return [Array]

# File lib/dissociated_introspection/inspection.rb, line 19
def class_macros
  get_class.__missing_class_macros__
end
extended_modules() click to toggle source

@return [Array<Module>]

# File lib/dissociated_introspection/inspection.rb, line 24
def extended_modules
  find_class_macro_by_type(:extend) { |a| add_method_referenced_name a.first }
end
get_class() click to toggle source

@return [Class]

# File lib/dissociated_introspection/inspection.rb, line 14
def get_class
  @get_class ||= get_sandbox_class
end
included_modules() click to toggle source

@return [Array<Module>]

# File lib/dissociated_introspection/inspection.rb, line 29
def included_modules
  find_class_macro_by_type(:include) { |a| add_method_referenced_name a.first }
end
locally_defined_constants(type=nil) click to toggle source

@optional type [Module, Class, Any] @return [Hash{Symbol => Any}>]

# File lib/dissociated_introspection/inspection.rb, line 45
def locally_defined_constants(type=nil)
  symbol_consts = get_class.constants - get_class.__missing_constants__.keys - [:BasicObject]
  consts = symbol_consts.each_with_object({}){|c, hash| hash[c] = get_class.const_get(c) }
  type ? consts.select { |s, c| c.is_a?(type) } : consts
end
missing_constants() click to toggle source

@return [Hash{String => Module}]

# File lib/dissociated_introspection/inspection.rb, line 39
def missing_constants
  get_class.__missing_constants__
end
parsed_source() click to toggle source

@return [DissociatedIntrospection::RubyClass]

# File lib/dissociated_introspection/inspection.rb, line 52
def parsed_source
  @parsed_source ||= RubyClass.new(source: file.read, parse_with_comments: true)
end
prepend_modules() click to toggle source

@return [Array<Module>]

# File lib/dissociated_introspection/inspection.rb, line 34
def prepend_modules
  find_class_macro_by_type(:prepend) { |a| add_method_referenced_name a.first }
end
sandbox_module() click to toggle source

@return [Module]

# File lib/dissociated_introspection/inspection.rb, line 57
def sandbox_module
  @sandbox_module ||= Module.new
end

Private Instance Methods

add_method_referenced_name(_module) click to toggle source
# File lib/dissociated_introspection/inspection.rb, line 63
def add_method_referenced_name(_module)
  _class_name_ = parsed_source.class_name

  _module.define_singleton_method(:referenced_name) do
    n = name.split("::")
    n = n.drop(1) if n.first =~ /#<Module:.*>/
    n = n.drop(1) if n.first == _class_name_
    return n.join("::")
  end
  _module
end
find_class_macro_by_type(type) { |values.first| ... } click to toggle source
# File lib/dissociated_introspection/inspection.rb, line 75
def find_class_macro_by_type(type)
  get_class.__missing_class_macros__.select { |h| h.keys.first == type }.map { |h| yield(h.values.first.first) }
end
get_sandbox_class() click to toggle source
# File lib/dissociated_introspection/inspection.rb, line 79
def get_sandbox_class
  modified_class_source = parsed_source.modify_parent_class(parent_class_replacement)
  path                  = if file.is_a? Pathname
                            file.to_s
                          else
                            file.path
                          end
  load_sandbox(OpenStruct.new(read: modified_class_source.source, path: path))
end
load_sandbox(file) click to toggle source
# File lib/dissociated_introspection/inspection.rb, line 89
def load_sandbox(file)
  @klass ||= EvalSandbox.new(file: file, module_namespace: sandbox_module).call
end