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
-
#replace!(regexp, string, force) ⇒ Object
protected
Adds the content to the file.
- #revoke! ⇒ Object
- #say_status(behavior) ⇒ Object protected
Methods inherited from EmptyDirectory
#convert_encoded_instructions, #exists?, #invoke_with_conflict_check, #on_conflict_behavior, #on_file_clash_behavior, #pretend?
Constructor Details
#initialize(base, destination, data, config) ⇒ InjectIntoFile
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/thor/actions/inject_into_file.rb', line 34 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) end |
Instance Attribute Details
#behavior ⇒ Object (readonly)
Returns the value of attribute behavior.
32 33 34 |
# File 'lib/thor/actions/inject_into_file.rb', line 32 def behavior @behavior end |
#flag ⇒ Object (readonly)
Returns the value of attribute flag.
32 33 34 |
# File 'lib/thor/actions/inject_into_file.rb', line 32 def flag @flag end |
#replacement ⇒ Object (readonly)
Returns the value of attribute replacement.
32 33 34 |
# File 'lib/thor/actions/inject_into_file.rb', line 32 def replacement @replacement end |
Instance Method Details
#invoke! ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/thor/actions/inject_into_file.rb', line 47 def invoke! say_status :invoke content = if @behavior == :after '\0' + replacement else replacement + '\0' end if exists? replace!(/#{flag}/, content, config[:force]) else unless pretend? raise Thor::Error, "The file #{ destination } does not appear to exist" end end end |
#replace!(regexp, string, force) ⇒ Object (protected)
Adds the content to the file.
99 100 101 102 103 104 105 106 |
# File 'lib/thor/actions/inject_into_file.rb', line 99 def replace!(regexp, string, force) return if pretend? content = File.read(destination) if force || !content.include?(replacement) content.gsub!(regexp, string) File.open(destination, "wb") { |file| file.write(content) } end end |
#revoke! ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/thor/actions/inject_into_file.rb', line 65 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 |
#say_status(behavior) ⇒ Object (protected)
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/thor/actions/inject_into_file.rb', line 81 def say_status(behavior) status = if behavior == :invoke if flag == /\A/ :prepend elsif flag == /\z/ :append else :insert end else :subtract end super(status, config[:verbose]) end |