Class: TTY::File::CreateFile

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/tty/file/create_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, relative_path, content, options = {}) ⇒ CreateFile

Returns a new instance of CreateFile.



14
15
16
17
18
19
20
# File 'lib/tty/file/create_file.rb', line 14

def initialize(base, relative_path, content, options = {})
  @base    = base
  @content = content
  @options = options
  @relative_path = convert_encoded_path(relative_path)
  @prompt  = TTY::Prompt.new
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



10
11
12
# File 'lib/tty/file/create_file.rb', line 10

def content
  @content
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/tty/file/create_file.rb', line 10

def options
  @options
end

#promptObject (readonly)

Returns the value of attribute prompt.



10
11
12
# File 'lib/tty/file/create_file.rb', line 10

def prompt
  @prompt
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



10
11
12
# File 'lib/tty/file/create_file.rb', line 10

def relative_path
  @relative_path
end

Instance Method Details

#callObject

Create a file



37
38
39
40
41
42
43
# File 'lib/tty/file/create_file.rb', line 37

def call
  detect_collision do
    FileUtils.mkdir_p(::File.dirname(relative_path))
    ::File.open(relative_path, 'wb') { |f| f.write(content) }
  end
  relative_path
end

#contextObject



22
23
24
# File 'lib/tty/file/create_file.rb', line 22

def context
  options[:context] || @base
end

#exist?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/tty/file/create_file.rb', line 26

def exist?
  ::File.exist?(relative_path)
end

#identical?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/tty/file/create_file.rb', line 30

def identical?
  ::File.binread(relative_path) == content
end