Class: Thor::Actions::InjectIntoFile
- Inherits:
-
EmptyDirectory
- Object
- EmptyDirectory
- Thor::Actions::InjectIntoFile
- Defined in:
- lib/thor/actions/inject_into_file.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#behavior ⇒ Object
readonly
Returns the value of attribute behavior.
-
#flag ⇒ Object
readonly
Returns the value of attribute flag.
-
#replacement ⇒ Object
readonly
Returns the value of attribute replacement.
Attributes inherited from EmptyDirectory
#base, #config, #destination, #given_destination, #relative_destination
Instance Method Summary collapse
-
#initialize(base, destination, data, config) ⇒ InjectIntoFile
constructor
A new instance of InjectIntoFile.
- #invoke! ⇒ Object
- #revoke! ⇒ Object
Methods inherited from EmptyDirectory
Constructor Details
#initialize(base, destination, data, config) ⇒ InjectIntoFile
Returns a new instance of InjectIntoFile.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/thor/actions/inject_into_file.rb', line 69 def initialize(base, destination, data, config) super(base, destination, {verbose: true}.merge(config)) @behavior, @flag = if @config.key?(:after) [:after, @config.delete(:after)] else [:before, @config.delete(:before)] end @replacement = data.is_a?(Proc) ? data.call : data @flag = Regexp.escape(@flag) unless @flag.is_a?(Regexp) @error_on_no_change = @config.fetch(:error_on_no_change, false) end |
Instance Attribute Details
#behavior ⇒ Object (readonly)
Returns the value of attribute behavior.
67 68 69 |
# File 'lib/thor/actions/inject_into_file.rb', line 67 def behavior @behavior end |
#flag ⇒ Object (readonly)
Returns the value of attribute flag.
67 68 69 |
# File 'lib/thor/actions/inject_into_file.rb', line 67 def flag @flag end |
#replacement ⇒ Object (readonly)
Returns the value of attribute replacement.
67 68 69 |
# File 'lib/thor/actions/inject_into_file.rb', line 67 def replacement @replacement end |
Instance Method Details
#invoke! ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/thor/actions/inject_into_file.rb', line 83 def invoke! content = if @behavior == :after '\0' + replacement else replacement + '\0' end if exists? if replace!(/#{flag}/, content, config[:force]) say_status(:invoke) elsif @error_on_no_change raise Thor::Error, "The content of #{destination} did not change" elsif replacement_present? say_status(:unchanged, color: :blue) else say_status(:unchanged, warning: WARNINGS[:unchanged_no_flag], color: :red) end else unless pretend? raise Thor::Error, "The file #{ destination } does not appear to exist" end end end |
#revoke! ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/thor/actions/inject_into_file.rb', line 107 def revoke! say_status :revoke regexp = if @behavior == :after content = '\1\2' /(#{flag})(.*)(#{Regexp.escape(replacement)})/m else content = '\2\3' /(#{Regexp.escape(replacement)})(.*)(#{flag})/m end replace!(regexp, content, true) end |