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) ⇒ DissociatedIntrospection::RubyClass



45
46
47
48
49
50
# File 'lib/dissociated_introspection/ruby_class.rb', line 45

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_defsDissociatedIntrospection::RubyClass::Def



73
74
75
76
77
78
79
80
81
82
# File 'lib/dissociated_introspection/ruby_class.rb', line 73

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



132
133
134
135
136
# File 'lib/dissociated_introspection/ruby_class.rb', line 132

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)


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

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

#defined_nested_classesArray<DissociatedIntrospection::RubyCode>



123
124
125
126
127
128
129
# File 'lib/dissociated_introspection/ruby_class.rb', line 123

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>



114
115
116
117
118
119
120
# File 'lib/dissociated_introspection/ruby_class.rb', line 114

def defined_nested_modules
  class_begin.children.select { |n| n.try(:type) == :module }.map do |m|
    RubyCode.build_from_ast(
      m
    )
  end
end

#defsDissociatedIntrospection::RubyClass::Def



68
69
70
# File 'lib/dissociated_introspection/ruby_class.rb', line 68

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

#inspect_methods(type = :instance_methods) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/dissociated_introspection/ruby_class.rb', line 84

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



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dissociated_introspection/ruby_class.rb', line 53

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])


101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dissociated_introspection/ruby_class.rb', line 101

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)


39
40
41
42
# File 'lib/dissociated_introspection/ruby_class.rb', line 39

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

#parent_class_nameString

Returns:

  • (String)


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

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

#scrub_inner_classesRubyClass

Returns:



93
94
95
96
97
98
# File 'lib/dissociated_introspection/ruby_class.rb', line 93

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