class DissociatedIntrospection::RubyClass

Attributes

ruby_code[R]

Public Class Methods

new(ruby_code) click to toggle source

@param [DissociatedIntrospection::RubyCode, String, Parse::AST, Hash{source: String, parse_with_comments: [Boolean]}] ruby_code

# File lib/dissociated_introspection/ruby_class.rb, line 8
def initialize(ruby_code)
  @ruby_code = if ruby_code.is_a?(Hash) && ruby_code.key?(:source)
                 RubyCode.build_from_source(
                   ruby_code[:source],
                   parse_with_comments: ruby_code[:parse_with_comments]
                 )
               elsif ruby_code.is_a?(Hash) && ruby_code.key?(:ast)
                 RubyCode.build_from_ast(
                   ruby_code[:ast],
                   comments: ruby_code.fetch(:comments, [])
                 )
               else
                 ruby_code
               end
end

Public Instance Methods

change_class_name(class_name) click to toggle source

@return [DissociatedIntrospection::RubyClass]

# File lib/dissociated_introspection/ruby_class.rb, line 46
def change_class_name(class_name)
  nodes    = ast.to_a.dup
  nodes[0] = Parser::CurrentRuby.parse(class_name)
  new_ast  = ast.updated(nil, nodes, nil)
  self.class.new(ast: new_ast)
end
class?() click to toggle source
# File lib/dissociated_introspection/ruby_class.rb, line 26
def class?
  ast.type == :class
end
class_begin() click to toggle source

@return [AST]

# File lib/dissociated_introspection/ruby_class.rb, line 141
def class_begin
  find_class.children.find { |n| n.try(:type) == :begin } || find_class
end
class_defs() click to toggle source

@return [Array<DissociatedIntrospection::RubyClass::Def>]

# File lib/dissociated_introspection/ruby_class.rb, line 74
def class_defs
  ns = class_begin.children.select { |n| :defs == n.try(:type) }.map do |n|
    create_def(n.updated(:def, n.children[1..-1]))
  end
  ns2 = class_begin.children.select { |n| :sclass == n.try(:type) }.flat_map do |n|
    next create_def(n.children[1]) if n.children[1].type == :def
    n.children[1].children.select { |n| n.try(:type) == :def }.map(&method(:create_def))
  end
  [*ns, *ns2]
end
class_method_calls() click to toggle source

@return [DissociatedIntrospection::MethodCall]

# File lib/dissociated_introspection/ruby_class.rb, line 134
def class_method_calls
  class_begin.children.select { |n| n.try(:type) == :send }.map do |ast|
    MethodCall.new(RubyCode.build_from_ast(ast))
  end
end
class_name() click to toggle source

@return [String]

# File lib/dissociated_introspection/ruby_class.rb, line 31
def class_name
  Unparser.unparse(find_class.to_a[0])
end
defined_nested_classes() click to toggle source

@return [Array<DissociatedIntrospection::RubyCode>]

# File lib/dissociated_introspection/ruby_class.rb, line 125
def defined_nested_classes
  class_begin.children.select { |n| n.try(:type) == :class }.map do |m|
    RubyCode.build_from_ast(
      m
    )
  end
end
defined_nested_modules() click to toggle source

@return [Array<DissociatedIntrospection::RubyCode>]

# File lib/dissociated_introspection/ruby_class.rb, line 116
def defined_nested_modules
  class_begin.children.select { |n| n.try(:type) == :module }.map do |m|
    RubyCode.build_from_ast(
      m
    )
  end
end
defs() click to toggle source

@return [Array<DissociatedIntrospection::RubyClass::Def>]

# File lib/dissociated_introspection/ruby_class.rb, line 69
def defs
  class_begin.children.select { |n| n.try(:type) == :def }.map(&method(:create_def))
end
inspect_methods(type=:instance_methods) click to toggle source

@private

# File lib/dissociated_introspection/ruby_class.rb, line 86
def inspect_methods(type=:instance_methods)
  public_send(if type == :instance_methods
                :defs
              elsif [:methods, :class_methods].include?(type)
                :class_defs
              end)
end
modify_parent_class(parent_class) click to toggle source

@return [DissociatedIntrospection::RubyClass]

# File lib/dissociated_introspection/ruby_class.rb, line 54
def modify_parent_class(parent_class)
  if parent_class?
    class_node    = find_class.to_a.dup
    class_node[1] = Parser::CurrentRuby.parse(parent_class.to_s)
    new_ast       = find_class.updated(nil, class_node, nil)
  else
    nodes    = ast.to_a.dup
    nodes[1] = nodes[0].updated(:const, [nil, parent_class.to_sym])
    new_ast  = ast.updated(nil, nodes, nil)
  end

  self.class.new(RubyCode.build_from_ast(new_ast, comments: comments))
end
module_nesting() click to toggle source

@return [Array]

# File lib/dissociated_introspection/ruby_class.rb, line 103
def module_nesting
  ary = []
  m   = ast
  while m
    next unless (m = depth_first_search(m, :module, :class))
    name = m.to_a[0].to_a[1]
    ary << name unless name.nil?
    m = m.to_a[1]
  end
  ary
end
parent_class?() click to toggle source
# File lib/dissociated_introspection/ruby_class.rb, line 40
def parent_class?
  return false unless find_class
  find_class.to_a[1].try(:type) == :const
end
parent_class_name() click to toggle source

@return [String]

# File lib/dissociated_introspection/ruby_class.rb, line 36
def parent_class_name
  Unparser.unparse(find_class.to_a[1])
end
scrub_inner_classes() click to toggle source

@return [RubyClass]

# File lib/dissociated_introspection/ruby_class.rb, line 95
def scrub_inner_classes
  self.class.new RubyCode.build_from_ast(
    scrub_inner_classes_ast,
    comments: comments
  )
end

Private Instance Methods

create_def(n) click to toggle source
# File lib/dissociated_introspection/ruby_class.rb, line 147
def create_def(n)
  CreateDef.new(n, comments).create
end
find_class() click to toggle source
# File lib/dissociated_introspection/ruby_class.rb, line 157
def find_class
  depth_first_search(ast, :class) || ast
end
scrub_inner_classes_ast() click to toggle source
# File lib/dissociated_introspection/ruby_class.rb, line 151
def scrub_inner_classes_ast
  find_class.updated(find_class.type,
                     class_begin.updated(class_begin.type,
                                         class_begin.children.reject { |n| n.try(:type) == :class }))
end