Class: Pineapples::Actions::CreateFile

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

Direct Known Subclasses

CopyFile

Constant Summary

Constants inherited from Action

Action::STATUS_COLORS

Instance Attribute Summary collapse

Attributes inherited from EmptyDirectory

#skip, #target

Attributes inherited from Action

#colors, #generator, #options

Instance Method Summary collapse

Methods inherited from EmptyDirectory

#exists?, #revoke!

Methods inherited from Action

colors, inherited, #skip?, status_color

Constructor Details

#initialize(generator, target, content, options = {}) ⇒ CreateFile

Returns a new instance of CreateFile.



63
64
65
66
# File 'lib/pineapples/actions/create_file.rb', line 63

def initialize(generator, target, content, options = {})
  super(generator, target, options)
  @content = content
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



56
57
58
# File 'lib/pineapples/actions/create_file.rb', line 56

def content
  @content
end

Instance Method Details

#identical?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/pineapples/actions/create_file.rb', line 68

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

#invoke!Object



76
77
78
79
80
81
82
# File 'lib/pineapples/actions/create_file.rb', line 76

def invoke!
  invoke_with_conflict_check do
    FileUtils.mkdir_p(File.dirname(target.fullpath))
    File.open(target.fullpath, 'wb') { |file| file.write render }
  end
  target.given
end

#renderObject



72
73
74
# File 'lib/pineapples/actions/create_file.rb', line 72

def render
  @render ||= content.is_a?(Proc) ? content.call : content.to_s
end