Class: DissociatedIntrospection::RubyClass

Inherits:
Object
  • Object
show all
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.



6
7
8
9
10
11
12
13
14
15
# File 'lib/dissociated_introspection/ruby_class.rb', line 6

def initialize(ruby_code)
  @ruby_code = if ruby_code.is_a?(Hash) && ruby_code.has_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.has_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

#astObject



17
18
19
# File 'lib/dissociated_introspection/ruby_class.rb', line 17

def ast
  ruby_code.ast
end

#change_class_name(class_name) ⇒ Object



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

def change_class_name(class_name)
  reset_nodes
  nodes[0] = Parser::CurrentRuby.parse(class_name)
  new_ast  = ast.updated(nil, nodes, nil)
  self.class.new(ast: new_ast)
end

#class_beginObject



76
77
78
# File 'lib/dissociated_introspection/ruby_class.rb', line 76

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

#class_nameObject



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

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

#commentsObject



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

def comments
  ruby_code.comments
end

#defsObject



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

def defs
  class_begin.children.select { |n| n.try(:type) == :def }.map do |n|
    def_comments = comments.select do |comment|
      comment.location.last_line+1 == n.location.first_line
    end
    Def.new(RubyCode.build_from_ast(n, comments: def_comments))
  end
end

#has_parent_class?Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/dissociated_introspection/ruby_class.rb', line 41

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

#is_class?Boolean

Returns:

  • (Boolean)


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

def is_class?
  ast.type == :class
end

#modify_parent_class(parent_class) ⇒ Object



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)
  reset_nodes
  if has_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[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



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dissociated_introspection/ruby_class.rb', line 89

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_nameObject



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

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

#scrub_inner_classesObject



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

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

#sourceObject



21
22
23
# File 'lib/dissociated_introspection/ruby_class.rb', line 21

def source
  ruby_code.source
end

#to_ruby_strObject



80
81
82
# File 'lib/dissociated_introspection/ruby_class.rb', line 80

def to_ruby_str
  source
end