Class: Rcli::Actions::CreateFile
- Inherits:
-
EmptyDirectory
- Object
- EmptyDirectory
- Rcli::Actions::CreateFile
- Defined in:
- lib/core/actions/create_file.rb,
lib/core/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
-
#data ⇒ Object
readonly
:nodoc:.
Attributes inherited from EmptyDirectory
#base, #config, #destination, #given_destination, #relative_destination
Instance Method Summary collapse
-
#contents ⇒ Object
Holds the content to be added to the file.
- #create ⇒ 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.
- #invoke! ⇒ Object
-
#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.
36 37 38 39 40 41 |
# File 'lib/core/actions/create_file.rb', line 36 def initialize(destination, data) @data = data @destination = destination @empty_dir = EmptyDirectory.new(File.dirname(destination)) end |
Instance Attribute Details
#data ⇒ Object (readonly)
:nodoc:
34 35 36 |
# File 'lib/core/actions/create_file.rb', line 34 def data @data end |
Instance Method Details
#contents ⇒ Object
Holds the content to be added to the file.
54 55 56 57 58 59 60 |
# File 'lib/core/actions/create_file.rb', line 54 def contents @render ||= if data.is_a?(Proc) data.call else data end end |
#create ⇒ Object
62 63 64 65 66 67 |
# File 'lib/core/actions/create_file.rb', line 62 def create @empty_dir.create File.open(@destination, 'wb') { |f| f.write contents } 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.
48 49 50 |
# File 'lib/core/actions/create_file.rb', line 48 def identical? exists? && File.binread(destination) == render end |
#invoke! ⇒ Object
56 57 58 59 60 61 |
# File 'lib/core/thor_actions/create_file.rb', line 56 def invoke! invoke_with_conflict_check do FileUtils.mkdir_p(File.dirname(destination)) File.open(destination, 'wb') { |f| f.write render } end end |
#render ⇒ Object
Holds the content to be added to the file.
48 49 50 51 52 53 54 |
# File 'lib/core/thor_actions/create_file.rb', line 48 def render @render ||= if data.is_a?(Proc) data.call else data end end |