Class: Thor::Actions::InjectIntoFile

Inherits:
EmptyDirectory show all
Defined in:
lib/thor/actions/inject_into_file.rb

Overview

:nodoc:

Instance Attribute Summary (collapse)

Attributes inherited from EmptyDirectory

#base, #config, #destination, #given_destination, #relative_destination

Instance Method Summary (collapse)

Methods inherited from EmptyDirectory

#exists?

Constructor Details

- (InjectIntoFile) initialize(base, destination, data, config)



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/thor/actions/inject_into_file.rb', line 38

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

- (Object) behavior (readonly)

Returns the value of attribute behavior



36
37
38
# File 'lib/thor/actions/inject_into_file.rb', line 36

def behavior
  @behavior
end

- (Object) flag (readonly)

Returns the value of attribute flag



36
37
38
# File 'lib/thor/actions/inject_into_file.rb', line 36

def flag
  @flag
end

- (Object) replacement (readonly)

Returns the value of attribute replacement



36
37
38
# File 'lib/thor/actions/inject_into_file.rb', line 36

def replacement
  @replacement
end

Instance Method Details

- (Object) invoke!



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/thor/actions/inject_into_file.rb', line 51

def invoke!
  say_status :invoke

  content = if @behavior == :after
    '\0' + replacement
  else
    replacement + '\0'
  end

  replace!(/#{flag}/, content, config[:force])
end

- (Object) revoke!



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/thor/actions/inject_into_file.rb', line 63

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