Class: MKBrut::Ops::AddMethod
- Inherits:
-
PrismParsingOp
- Object
- BaseOp
- PrismParsingOp
- MKBrut::Ops::AddMethod
- Defined in:
- lib/mkbrut/ops/add_method.rb
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(file:, class_name:, code:) ⇒ AddMethod
constructor
A new instance of AddMethod.
Methods inherited from BaseOp
dry_run=, dry_run?, #dry_run?, fileutils_args, #fileutils_args
Constructor Details
#initialize(file:, class_name:, code:) ⇒ AddMethod
Returns a new instance of AddMethod.
2 3 4 5 6 |
# File 'lib/mkbrut/ops/add_method.rb', line 2 def initialize(file:, class_name:, code:) @file = file @class_name = class_name @code = code.gsub(/^\n\s*$/,"").gsub(/\n$/,"") end |
Instance Method Details
#call ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mkbrut/ops/add_method.rb', line 8 def call if dry_run? puts "Would add method:\n#{@code}\nto #{@class_name} in '#{@file}'" return end class_node = find_class(class_name: @class_name, assumed_body: false) insert_offset = nil class_body_nodes = case class_node.body when Prism::StatementsNode class_node.body.body when nil [] else [class_node.body] end class_body_nodes.each do |node| if node.is_a?(Prism::CallNode) && node.name == "private" insert_offset = node.location.start_offset break end end if insert_offset.nil? # Use the final end of the class insert_offset = class_node.location.end_offset - 3 end class_start_line = class_node.location.start_line class_indent = @source.lines[class_start_line - 1][/^\s*/] || "" method_indent = class_indent + " " indented_method_code = @code.lines.map { |line| method_indent + line }.join insert_text = "\n" + indented_method_code + "\n" updated_source = @source.dup.insert(insert_offset, insert_text) File.write(@file, updated_source) updated_source end |