Class: Wizard::Spells::UpdateFile
- Defined in:
- lib/wizard/spells/update_file.rb
Constant Summary
Constants included from Helpers
Instance Attribute Summary collapse
-
#after ⇒ Object
readonly
Returns the value of attribute after.
-
#before ⇒ Object
readonly
Returns the value of attribute before.
-
#chmod ⇒ Object
readonly
Returns the value of attribute chmod.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#replace ⇒ Object
readonly
Returns the value of attribute replace.
Instance Method Summary collapse
-
#initialize(filename, content = nil, options = {}) ⇒ UpdateFile
constructor
A new instance of UpdateFile.
- #perform_with_content_update ⇒ Object (also: #perform)
-
#rebuild_content ⇒ Object
Build new content based on original one, including specified update directives.
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, ={}) super(filename, nil, ) @content = content @before = [:before] @after = [:after] @replace = [:replace] end |
Instance Attribute Details
#after ⇒ Object (readonly)
Returns the value of attribute after.
40 41 42 |
# File 'lib/wizard/spells/update_file.rb', line 40 def after @after end |
#before ⇒ Object (readonly)
Returns the value of attribute before.
40 41 42 |
# File 'lib/wizard/spells/update_file.rb', line 40 def before @before end |
#chmod ⇒ Object (readonly)
Returns the value of attribute chmod.
40 41 42 |
# File 'lib/wizard/spells/update_file.rb', line 40 def chmod @chmod end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
40 41 42 |
# File 'lib/wizard/spells/update_file.rb', line 40 def content @content end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
40 41 42 |
# File 'lib/wizard/spells/update_file.rb', line 40 def filename @filename end |
#replace ⇒ Object (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_update ⇒ Object 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_content ⇒ Object
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 |