Class: Freyia::Automations::CreateFile
- Inherits:
-
EmptyDirectory
- Object
- EmptyDirectory
- Freyia::Automations::CreateFile
- 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
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Attributes inherited from EmptyDirectory
#base, #config, #destination, #given_destination, #relative_destination
Instance Method Summary collapse
- #call ⇒ Object
-
#identical? ⇒ Boolean
Checks if the content of the file at the destination is identical to the rendered result.
-
#initialize(base, destination, data, **config) ⇒ CreateFile
constructor
A new instance of CreateFile.
-
#render ⇒ Object
Holds the content to be added to the file.
Methods inherited from EmptyDirectory
Constructor Details
#initialize(base, destination, data, **config) ⇒ CreateFile
Returns a new instance of CreateFile.
32 33 34 35 |
# File 'lib/freyia/automations/create_file.rb', line 32 def initialize(base, destination, data, **config) @data = data super(base, destination, **config) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
30 31 32 |
# File 'lib/freyia/automations/create_file.rb', line 30 def data @data end |
Instance Method Details
#call ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/freyia/automations/create_file.rb', line 52 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.
41 42 43 44 |
# File 'lib/freyia/automations/create_file.rb', line 41 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 |
#render ⇒ Object
Holds the content to be added to the file.
48 49 50 |
# File 'lib/freyia/automations/create_file.rb', line 48 def render @render ||= data.is_a?(Proc) ? data.() : data end |