Class: Wizard::Spells::MakeFile

Inherits:
Base
  • Object
show all
Defined in:
lib/wizard/spells/make_file.rb

Direct Known Subclasses

CompileTemplate, UpdateFile

Constant Summary

Constants included from Helpers

Helpers::COLORS

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})
  @filename = filename
  @content  = content 
  @chmod    = options[:mode]
  
  force! if options[:force]
end

Instance Attribute Details

#chmodObject (readonly)

Returns the value of attribute chmod.



27
28
29
# File 'lib/wizard/spells/make_file.rb', line 27

def chmod
  @chmod
end

#contentObject (readonly)

Returns the value of attribute content.



27
28
29
# File 'lib/wizard/spells/make_file.rb', line 27

def content
  @content
end

#filenameObject (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.

Returns:

  • (Boolean)


61
62
63
# File 'lib/wizard/spells/make_file.rb', line 61

def identical_content?
  File.read(filename) == content
end

#performObject



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