Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

funroll loops

  • Loading branch information...
commit 71edcbbf043059b85c262404c83008812aafb51b 1 parent 444627c
@timcharper authored
Showing with 16 additions and 18 deletions.
  1. +16 −18 generators/role_generator_helpers.rb
View
34 generators/role_generator_helpers.rb
@@ -1,26 +1,24 @@
module RoleGeneratorHelpers
def insert_content_after(filename, regexp, content_for_insertion, options = {})
+ return false unless File.exist?(filename)
content = File.read(filename)
- options[:unless] ||= lambda {false }
+ options[:unless] ||= lambda { false }
# already have the function? Don't generate it twice
- unless options[:unless].call(content)
- # find the line that has the model declaration
- lines = content.split("\n")
- found_line = nil
+ return false if options[:unless].call(content)
+ # find the line that has the model declaration
+ lines = content.split("\n")
+ found_line = nil
+
+ 0.upto(lines.length-1) {|line_number|
+ found_line = line_number if regexp.match(lines[line_number])
+ }
+ if found_line
+ # insert the rest of these lines after the found line
+ lines.insert(found_line+1, content_for_insertion)
+ content = lines * "\n"
- 0.upto(lines.length-1) {|line_number|
- found_line = line_number if regexp.match(lines[line_number])
- }
- if found_line
- # insert the rest of these lines after the found line
- lines.insert(found_line+1, content_for_insertion)
- content = lines * "\n"
-
- File.open(filename, "w") {|f| f.puts content }
- return true
- end
- else
- return false
+ File.open(filename, "w") {|f| f.puts content }
+ return true
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.