Class: Wizard::Spells::UpdateFile

Inherits:
MakeFile show all
Defined in:
lib/wizard/spells/update_file.rb

Constant Summary

Constants included from Helpers

Helpers::COLORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from MakeFile

#create_file!, #identical_content?

Methods inherited from Base

all_forced?, attr_status, #force!, force_all!, #force_all!, #forced?, #status, #status?

Methods included from Helpers

#adjust, #colorize, #console_width, #print, #say, #say!

Constructor Details

#initialize(filename, content = nil, options = {}) ⇒ UpdateFile

Returns a new instance of UpdateFile.



43
44
45
46
47
48
49
50
# File 'lib/wizard/spells/update_file.rb', line 43

def initialize(filename, content=nil, options={})
  super(filename, nil, options)

  @content = content
  @before  = options[:before]
  @after   = options[:after]
  @replace = options[:replace]
end

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



40
41
42
# File 'lib/wizard/spells/update_file.rb', line 40

def after
  @after
end

#beforeObject (readonly)

Returns the value of attribute before.



40
41
42
# File 'lib/wizard/spells/update_file.rb', line 40

def before
  @before
end

#chmodObject (readonly)

Returns the value of attribute chmod.



40
41
42
# File 'lib/wizard/spells/update_file.rb', line 40

def chmod
  @chmod
end

#contentObject (readonly)

Returns the value of attribute content.



40
41
42
# File 'lib/wizard/spells/update_file.rb', line 40

def content
  @content
end

#filenameObject (readonly)

Returns the value of attribute filename.



40
41
42
# File 'lib/wizard/spells/update_file.rb', line 40

def filename
  @filename
end

#replaceObject (readonly)

Returns the value of attribute replace.



40
41
42
# File 'lib/wizard/spells/update_file.rb', line 40

def replace
  @replace
end

Instance Method Details

#perform_with_content_updateObject Also known as: perform



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/wizard/spells/update_file.rb', line 52

def perform_with_content_update
  if File.exist?(filename)
    force! and rebuild_content and perform_without_content_update
  elsif content && !before && !after && !replace
    perform_without_content_update
  else
    missing!
  end
rescue Object
  error!
end

#rebuild_contentObject

Build new content based on original one, including specified update directives. All directives can’t be mixed, which mean that only one update can ba applied on the file at one time. If none directive is specified, then whole file content will be replaced with specified in constructor.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/wizard/spells/update_file.rb', line 72

def rebuild_content
  if (before || after) && !replace
    content = File.read(filename)
    
    return @content = [@content, content].join("\n") if after == :BOF
    return @content = [content, @content].join("\n") if before == :EOF
    
    content = content.sub(/\r\n/, "\n").sub(/\r/, "\n").split(/\n/)
    content.each_with_index do |line, id|
      content[id] = [@content, line].join("\n") if !after && before && line =~ before
      content[id] = [line, @content].join("\n") if !before && after && line =~ after
    end
    
    @content = content.join("\n")
  elsif replace
    @content = File.read(filename).sub(replace, @content)
  else
    @content
  end
end