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

Defined Under Namespace

Classes: Def

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ruby_code) ⇒ RubyClass

Returns a new instance of RubyClass.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dissociated_introspection/ruby_class.rb', line 7

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) ⇒ Object



42
43
44
45
46
47
# File 'lib/dissociated_introspection/ruby_class.rb', line 42

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)


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

def class?
  ast.type == :class
end

#class_beginObject



85
86
87
# File 'lib/dissociated_introspection/ruby_class.rb', line 85

def class_begin
  find_class.children.find { |n| n.try(:type) == :begin } || find_class
end

#class_defsObject



67
68
69
70
71
72
73
74
75
# File 'lib/dissociated_introspection/ruby_class.rb', line 67

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|
    n.children[1].children.select { |n| n.try(:type) == :def }.map(&method(:create_def))
  end
  [*ns, *ns2]
end

#class_method_callsObject



109
110
111
112
113
# File 'lib/dissociated_introspection/ruby_class.rb', line 109

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_nameObject



29
30
31
# File 'lib/dissociated_introspection/ruby_class.rb', line 29

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

#defsObject



63
64
65
# File 'lib/dissociated_introspection/ruby_class.rb', line 63

def defs
  class_begin.children.select { |n| n.try(:type) == :def }.map(&method(:create_def))
end

#inspect_methods(type = :instance_methods) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/dissociated_introspection/ruby_class.rb', line 77

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) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dissociated_introspection/ruby_class.rb', line 49

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_nestingObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dissociated_introspection/ruby_class.rb', line 96

def module_nesting
  ary = []
  m   = ast
  while m
    if (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
  end
  ary
end

#parent_class?Boolean

Returns:

  • (Boolean)


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

def parent_class?
  return false unless find_class
  find_class.to_a[1].try(:type) == :const
end

#parent_class_nameObject



33
34
35
# File 'lib/dissociated_introspection/ruby_class.rb', line 33

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

#scrub_inner_classesObject



89
90
91
92
93
94
# File 'lib/dissociated_introspection/ruby_class.rb', line 89

def scrub_inner_classes
  self.class.new RubyCode.build_from_ast(
    scrub_inner_classes_ast,
    comments: comments
  )
end