Class: MKBrut::Ops::InsertRoute
- Inherits:
-
PrismParsingOp
- Object
- BaseOp
- PrismParsingOp
- MKBrut::Ops::InsertRoute
- Defined in:
- lib/mkbrut/ops/insert_route.rb
Instance Method Summary collapse
- #call ⇒ Object
- #find_routes_block(class_node) ⇒ Object
-
#initialize(project_root:, code:) ⇒ InsertRoute
constructor
A new instance of InsertRoute.
Methods inherited from BaseOp
dry_run=, dry_run?, #dry_run?, fileutils_args, #fileutils_args
Constructor Details
#initialize(project_root:, code:) ⇒ InsertRoute
Returns a new instance of InsertRoute.
2 3 4 5 |
# File 'lib/mkbrut/ops/insert_route.rb', line 2 def initialize(project_root:, code:) @file = project_root / "app" / "src" / "app.rb" @code = code end |
Instance Method Details
#call ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/mkbrut/ops/insert_route.rb', line 7 def call if dry_run? puts "Would insert route:\n#{@code}\ninto #{@file}" return end app_class_node = find_class(class_name: "App") routes_block = find_routes_block(app_class_node) if !routes_block raise "'App' in '#{@file}' did not have a routes block, so we cannot insert a new route" end end_offset = routes_block.block.location.end_offset indented_line = " #{@code}\n " new_source = @source.dup.insert(end_offset - 3, indented_line) File.open(@file, "w") do |file| file.puts new_source end end |
#find_routes_block(class_node) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mkbrut/ops/insert_route.rb', line 29 def find_routes_block(class_node) statements = case class_node.body when Prism::StatementsNode class_node.body.body when nil [] else [class_node.body] end statements.detect do |statement| if statement.is_a?(Prism::CallNode) if statement.name == :routes statement.block else false end else false end end end |