Class: Wizard::Spells::MakeFile
- Defined in:
- lib/wizard/spells/make_file.rb
Direct Known Subclasses
Constant Summary
Constants included from Helpers
Instance Attribute Summary collapse
-
#chmod ⇒ Object
readonly
Returns the value of attribute chmod.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#filename ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute filename.
Instance Method Summary collapse
-
#create_file! ⇒ Object
Create current performed file, write its content and set proper chmod.
-
#identical_content? ⇒ Boolean
Returns
true
when current file already exists and have the same content as given in initializer. -
#initialize(filename, content = nil, options = {}) ⇒ MakeFile
constructor
A new instance of MakeFile.
- #perform ⇒ Object
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 = {}) ⇒ MakeFile
Returns a new instance of MakeFile.
30 31 32 33 34 35 36 |
# File 'lib/wizard/spells/make_file.rb', line 30 def initialize(filename, content=nil, ={}) @filename = filename @content = content @chmod = [:mode] force! if [:force] end |
Instance Attribute Details
#chmod ⇒ Object (readonly)
Returns the value of attribute chmod.
27 28 29 |
# File 'lib/wizard/spells/make_file.rb', line 27 def chmod @chmod end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
27 28 29 |
# File 'lib/wizard/spells/make_file.rb', line 27 def content @content end |
#filename ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute filename.
27 28 29 |
# File 'lib/wizard/spells/make_file.rb', line 27 def filename @filename end |
Instance Method Details
#create_file! ⇒ Object
Create current performed file, write its content and set proper chmod.
52 53 54 55 56 57 |
# File 'lib/wizard/spells/make_file.rb', line 52 def create_file! if File.open(filename, "w+") {|f| f.write(content) if content } FileUtils.chmod(chmod, filename) if chmod return true end end |
#identical_content? ⇒ Boolean
Returns true
when current file already exists and have the same content as given in initializer.
61 62 63 |
# File 'lib/wizard/spells/make_file.rb', line 61 def identical_content? File.read(filename) == content end |
#perform ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/wizard/spells/make_file.rb', line 38 def perform if File.exist?(filename) return identical! if identical_content? return status if conflict! and !forced? end return conflict? ? updated! : created! if create_file! error! rescue Errno::EACCES noaccess! rescue Object error! end |