Class: DissociatedIntrospection::RubyClass
- Inherits:
-
Object
- Object
- DissociatedIntrospection::RubyClass
- Defined in:
- lib/dissociated_introspection/ruby_class.rb
Defined Under Namespace
Classes: Def
Instance Method Summary collapse
- #change_class_name(class_name) ⇒ Object
- #class_begin ⇒ Object
- #class_name ⇒ Object
- #defs ⇒ Object
- #has_parent_class? ⇒ Boolean
-
#initialize(source: nil, ast: nil) ⇒ RubyClass
constructor
A new instance of RubyClass.
- #is_class? ⇒ Boolean
- #modify_parent_class(parent_class) ⇒ Object
- #parent_class_name ⇒ Object
- #scrub_inner_classes ⇒ Object
- #to_ruby_str ⇒ Object
- #wrap_in_modules(modules) ⇒ Object
Constructor Details
#initialize(source: nil, ast: nil) ⇒ RubyClass
Returns a new instance of RubyClass.
5 6 7 8 9 10 11 |
# File 'lib/dissociated_introspection/ruby_class.rb', line 5 def initialize(source: nil, ast: nil) @source = source @ast = ast if source.nil? && ast.nil? raise ArgumentError.new "#{self.class.name} require either source or ast to be given as named arguments." end end |
Instance Method Details
#change_class_name(class_name) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/dissociated_introspection/ruby_class.rb', line 30 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_begin ⇒ Object
80 81 82 |
# File 'lib/dissociated_introspection/ruby_class.rb', line 80 def class_begin find_class.children.find{|n| n.try(:type) == :begin} end |
#class_name ⇒ Object
17 18 19 |
# File 'lib/dissociated_introspection/ruby_class.rb', line 17 def class_name Unparser.unparse(find_class.to_a[0]) end |
#defs ⇒ Object
76 77 78 |
# File 'lib/dissociated_introspection/ruby_class.rb', line 76 def defs class_begin.children.select { |n| n.try(:type) == :def }.map{|n| Def.new(ast: n)} end |
#has_parent_class? ⇒ Boolean
25 26 27 28 |
# File 'lib/dissociated_introspection/ruby_class.rb', line 25 def has_parent_class? return false if find_class.nil? find_class.to_a[1].try(:type) == :const end |
#is_class? ⇒ Boolean
13 14 15 |
# File 'lib/dissociated_introspection/ruby_class.rb', line 13 def is_class? ast.type == :class end |
#modify_parent_class(parent_class) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/dissociated_introspection/ruby_class.rb', line 37 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(ast: new_ast) end |
#parent_class_name ⇒ Object
21 22 23 |
# File 'lib/dissociated_introspection/ruby_class.rb', line 21 def parent_class_name Unparser.unparse(find_class.to_a[1]) end |
#scrub_inner_classes ⇒ Object
88 89 90 |
# File 'lib/dissociated_introspection/ruby_class.rb', line 88 def scrub_inner_classes self.class.new(ast: find_class.updated(find_class.type, class_begin.updated(class_begin.type, class_begin.children.reject { |n| n.try(:type) == :class }))) end |
#to_ruby_str ⇒ Object
84 85 86 |
# File 'lib/dissociated_introspection/ruby_class.rb', line 84 def to_ruby_str Unparser.unparse(ast) end |
#wrap_in_modules(modules) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/dissociated_introspection/ruby_class.rb', line 92 def wrap_in_modules(modules) return self if modules.nil? || modules.empty? ruby_string = to_ruby_str modules.split("::").reverse.each do |module_name| ruby_string = wrap_module(module_name, ruby_string) end wrapped_ast = Parser::CurrentRuby.parse(ruby_string) self.class.new(ast: wrapped_ast) end |