Class: MKBrut::Ops::InsertCodeInMethod
- Inherits:
-
PrismParsingOp
- Object
- BaseOp
- PrismParsingOp
- MKBrut::Ops::InsertCodeInMethod
- Defined in:
- lib/mkbrut/ops/insert_code_in_method.rb
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(file:, class_name:, method_name:, class_method: false, code:, ignore_if_file_not_found: false, where: :end) ⇒ InsertCodeInMethod
constructor
A new instance of InsertCodeInMethod.
Methods inherited from BaseOp
dry_run=, dry_run?, #dry_run?, fileutils_args, #fileutils_args
Constructor Details
#initialize(file:, class_name:, method_name:, class_method: false, code:, ignore_if_file_not_found: false, where: :end) ⇒ InsertCodeInMethod
Returns a new instance of InsertCodeInMethod.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/mkbrut/ops/insert_code_in_method.rb', line 2 def initialize(file:, class_name:, method_name:, class_method: false, code:, ignore_if_file_not_found: false, where: :end) @file = file @class_name = class_name @method_name = method_name.to_sym @class_method = class_method @code = code @ignore_if_file_not_found = ignore_if_file_not_found @where = where end |
Instance Method Details
#call ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mkbrut/ops/insert_code_in_method.rb', line 18 def call if !@file.exist? && @ignore_if_file_not_found return end method_node = find_method(class_name: @class_name, method_name: @method_name, class_method: @class_method) insertion_point = if @where == :start insertion_point_for_code_at_start_of_method(method_node: method_node) else insertion_point_for_code_at_end_of_method(method_node: method_node) end indented_code = indent_code_for_method(method_node: method_node) new_source = @source.dup.insert(insertion_point, indented_code) File.write(@file, new_source) end |