Skip to content

Commit 793e42f

Browse files
committed
Merge pull request #515 from revans/inject-into-module
Inject into Modules
2 parents 7febbb2 + 1710af1 commit 793e42f

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

lib/thor/actions/file_manipulation.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,29 @@ def inject_into_class(path, klass, *args, &block)
204204
insert_into_file(path, *(args << config), &block)
205205
end
206206

207+
# Injects text right after the module definition. Since it depends on
208+
# insert_into_file, it's reversible.
209+
#
210+
# ==== Parameters
211+
# path<String>:: path of the file to be changed
212+
# module_name<String|Class>:: the module to be manipulated
213+
# data<String>:: the data to append to the class, can be also given as a block.
214+
# config<Hash>:: give :verbose => false to not log the status.
215+
#
216+
# ==== Examples
217+
#
218+
# inject_into_module "app/helpers/application_helper.rb", ApplicationHelper, " def help; 'help'; end\n"
219+
#
220+
# inject_into_module "app/helpers/application_helper.rb", ApplicationHelper do
221+
# " def help; 'help'; end\n"
222+
# end
223+
#
224+
def inject_into_module(path, module_name, *args, &block)
225+
config = args.last.is_a?(Hash) ? args.pop : {}
226+
config.merge!(:after => /module #{module_name}\n|module #{module_name} .*\n/)
227+
insert_into_file(path, *(args << config), &block)
228+
end
229+
207230
# Run a regular expression replacement on a file.
208231
#
209232
# ==== Parameters

spec/actions/file_manipulation_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "helper"
22

33
class Application; end
4+
module ApplicationHelper; end
45

56
describe Thor::Actions do
67
def runner(options = {})
@@ -340,6 +341,31 @@ def file
340341
expect(File.binread(file)).to eq("class Application < Base\nend\n")
341342
end
342343
end
344+
345+
describe "#inject_into_module" do
346+
def file
347+
File.join(destination_root, "application_helper.rb")
348+
end
349+
350+
it "appends content to a module" do
351+
action :inject_into_module, "application_helper.rb", ApplicationHelper, " def help; 'help'; end\n"
352+
expect(File.binread(file)).to eq("module ApplicationHelper\n def help; 'help'; end\nend\n")
353+
end
354+
355+
it "accepts a block" do
356+
action(:inject_into_module, "application_helper.rb", ApplicationHelper) { " def help; 'help'; end\n" }
357+
expect(File.binread(file)).to eq("module ApplicationHelper\n def help; 'help'; end\nend\n")
358+
end
359+
360+
it "logs status" do
361+
expect(action(:inject_into_module, "application_helper.rb", ApplicationHelper, " def help; 'help'; end\n")).to eq(" insert application_helper.rb\n")
362+
end
363+
364+
it "does not append if class name does not match" do
365+
action :inject_into_module, "application_helper.rb", "App", " def help; 'help'; end\n"
366+
expect(File.binread(file)).to eq("module ApplicationHelper\nend\n")
367+
end
368+
end
343369
end
344370

345371
describe "when adjusting comments" do

spec/fixtures/application_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module ApplicationHelper
2+
end

0 commit comments

Comments
 (0)