April 11, 2025
Have you ever found yourself doing the same boring edits across multiple Ruby files? Maybe it’s adding routes, controller filters, or setup lines, over and over again, like some modern-day config monk copying scripture. Well, I decided to stop suffering—and teach my Ruby scripts to inject like pros.
Meet: FlexibleInjector. A small tool with big dreams.
Looking to automate, standardize, or enhance your Ruby on Rails application?
The Problem
When you’re working with multiple apps, microservices, or simply tired of typing root ‘home#index’ for the 74th time, you start to wonder…
“Could I teach my code to inject code into other code?”
Yes. You can. Rails already has a secret weapon called inject_into_file, used deep in the bowels of Rails generators.
But it’s not a standalone spell. You can’t just call it from anywhere—it needs the right context. The right incantation. And some love.
The Solution: YAML-Fueled File Injection
What if we could feed inject_into_file with a YAML file, defining what to inject, where, and how? We’d get a readable, clean, declarative way to describe boring file mutations.
Example config:
- file: routes.rb
content: " root 'home#index'\n"
message: insert
after: "Rails.application.routes.draw do"
- file: application_controller.rb
content: " before_action :authenticate_user!\n"
message: insert
before: "end"
- file: setup.rb
content: "# Setup done here\n"
message: append
Simple, right? Now let’s bring it to life.
The Ruby Wizardry
require 'yaml'
require 'thor'
require 'thor/actions'
class FlexibleInjector < Thor::Group
include Thor::Actions
def self.source_root
Dir.pwd
end
def apply_from_config
injections = YAML.load_file('file_injections.yml')
injections.each do |injection|
file_path = injection['file']
content = injection['content'].to_s.gsub(/\A\n+/, '').chomp + "\n"
message = (injection['message'] || 'insert').to_sym
options = {
after: injection['after'],
before: injection['before']
}.compact
case message
when :insert
say_status :insert, file_path
inject_into_file(file_path, content, options)
when :append
say_status :append, file_path
inject_into_file(file_path, content, after: nil)
else
say_status :skip, "#{file_path} — unknown message: #{message}", :yellow
end
end
end
end
FlexibleInjector.start
That’s it. You just made a CLI-friendly YAML-injected code wizard.
Why This Is Awesome
-
Declarative : You describe what you want, not how.
-
Reusable : Store snippets, reuse across projects.
-
Safe : Uses Thor’s trusted inject_into_file under the hood.
-
Focused : No full-on generators or Rails plugins needed.
-
Extendable : Easily plug into any automation workflow.
What’s Next?
I’m planning to roll this feature into the next version of Trix Genius, my automation companion tool for Ruby/Rails projects. This will streamline setup tasks even further—no manual editing, no hunting through files. Just describe, run, done.
You’ll be able to:
- Automate onboarding for new projects
- Inject default config or policy files
- Speed up repetitive dev tasks with ease
And all of that with a single YAML file.
TL;DR
- I built a YAML-powered file injector using inject_into_file
- It supports before, after, and append strategies
- It saved me time and mental space
- It’s heading to Trix Genius as part of its evolution
- You can try it too—and tweak it for your own dev rituals
Top comments (0)