Class: DissociatedIntrospection::RubyClass

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/dissociated_introspection/ruby_class.rb,
lib/dissociated_introspection/ruby_class/def.rb,
lib/dissociated_introspection/ruby_class/create_def.rb

Defined Under Namespace

Classes: CreateDef, Def

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ruby_code) ⇒ RubyClass

Returns a new instance of RubyClass.

Parameters:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#ruby_codeObject (readonly)

Returns the value of attribute ruby_code.



3
4
5
# File 'lib/dissociated_introspection/ruby_class.rb', line 3

def ruby_code
  @ruby_code
end

Instance Method Details

#change_class_name(class_name) ⇒ DissociatedIntrospection::RubyClass



46
47
48
49
50
51
# 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?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/dissociated_introspection/ruby_class.rb', line 26

def class?
  ast.type == :class
end

#class_beginAST

Returns:

  • (AST)


141
142
143
# 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_defsArray<DissociatedIntrospection::RubyClass::Def>



74
75
76
77
78
79
80
81
82
83
# 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_callsDissociatedIntrospection::MethodCall



134
135
136
137
138
# 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_nameString

Returns:

  • (String)


31
32
33
# File 'lib/dissociated_introspection/ruby_class.rb', line 31

def class_name
  Unparser.unparse(find_class.to_a[0])
end

#defined_nested_classesArray<DissociatedIntrospection::RubyCode>



125
126
127
128
129
130
131
# 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_modulesArray<DissociatedIntrospection::RubyCode>



116
117
118
119
120
121
122
# 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

#defsArray<DissociatedIntrospection::RubyClass::Def>



69
70
71
# 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) ⇒ Object



86
87
88
89
90
91
92
# 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) ⇒ DissociatedIntrospection::RubyClass



54
55
56
57
58
59
60
61
62
63
64
65
66
# 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_nestingArray[Symbol]

Returns:

  • (Array[Symbol])


103
104
105
106
107
108
109
110
111
112
113
# 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?Boolean

Returns:

  • (Boolean)


40
41
42
43
# 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_nameString

Returns:

  • (String)


36
37
38
# File 'lib/dissociated_introspection/ruby_class.rb', line 36

def parent_class_name
  Unparser.unparse(find_class.to_a[1])
end

#scrub_inner_classesRubyClass

Returns:



95
96
97
98
99
100
# 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