Class: Freyia::Automations::CreateFile

Inherits:
EmptyDirectory show all
Defined in:
lib/freyia/automations/create_file.rb

Overview

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

Direct Known Subclasses

CreateLink

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.



37
38
39
40
# File 'lib/freyia/automations/create_file.rb', line 37

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

Instance Attribute Details

#dataObject (readonly)

:nodoc:



35
36
37
# File 'lib/freyia/automations/create_file.rb', line 35

def data
  @data
end

Instance Method Details

#callObject



62
63
64
65
66
67
68
69
# File 'lib/freyia/automations/create_file.rb', line 62

def call
  invoke_with_conflict_check do
    require "fileutils"
    FileUtils.mkdir_p(File.dirname(destination))
    File.open(destination, "wb", config[:perm]) { |f| f.write render }
  end
  given_destination
end

#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)


47
48
49
50
# File 'lib/freyia/automations/create_file.rb', line 47

def identical?
  # binread uses ASCII-8BIT, so to avoid false negatives, the string must use the same
  exists? && File.binread(destination) == String.new(render).force_encoding("ASCII-8BIT")
end

#renderObject

Holds the content to be added to the file.



54
55
56
57
58
59
60
# File 'lib/freyia/automations/create_file.rb', line 54

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