Class: Thor::Actions::CreateFile

Inherits:
EmptyDirectory show all
Defined in:
lib/vendor/thor/lib/thor/actions/create_file.rb

Overview

AddFile is a subset of Template, which instead of rendering a file with ERB, it gets the content from the user.

Instance Attribute Summary collapse

Attributes inherited from EmptyDirectory

#base, #config, #destination, #given_destination, #relative_destination

Instance Method Summary collapse

Methods inherited from EmptyDirectory

#exists?, #revoke!

Constructor Details

#initialize(base, destination, data, config = {}) ⇒ CreateFile

Returns a new instance of CreateFile.



34
35
36
37
# File 'lib/vendor/thor/lib/thor/actions/create_file.rb', line 34

def initialize(base, destination, data, config={})
  @data = data
  super(base, destination, config)
end

Instance Attribute Details

#dataObject (readonly)

:nodoc:



32
33
34
# File 'lib/vendor/thor/lib/thor/actions/create_file.rb', line 32

def data
  @data
end

Instance Method Details

#identical?Boolean

Checks if the content of the file at the destination is identical to the rendered result.

Returns

Boolean

true if it is identical, false otherwise.

Returns:

  • (Boolean)


44
45
46
# File 'lib/vendor/thor/lib/thor/actions/create_file.rb', line 44

def identical?
  exists? && File.binread(destination) == render
end

#invoke!Object



58
59
60
61
62
63
64
# File 'lib/vendor/thor/lib/thor/actions/create_file.rb', line 58

def invoke!
  invoke_with_conflict_check do
    FileUtils.mkdir_p(File.dirname(destination))
    File.open(destination, 'wb') { |f| f.write render }
  end
  given_destination
end

#renderObject

Holds the content to be added to the file.



50
51
52
53
54
55
56
# File 'lib/vendor/thor/lib/thor/actions/create_file.rb', line 50

def render
  @render ||= if data.is_a?(Proc)
    data.call
  else
    data
  end
end