Class: Thor::Actions::EmptyDirectory
- Inherits:
-
Object
- Object
- Thor::Actions::EmptyDirectory
- Defined in:
- lib/thor/actions/empty_directory.rb
Overview
Class which holds create directory logic. This is the base class for other actions like create_file and directory.
This implementation is based in Templater actions, created by Jonas Nicklas and Michael S. Klishin under MIT LICENSE.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
:nodoc:.
-
#config ⇒ Object
readonly
:nodoc:.
-
#destination ⇒ Object
:nodoc:.
-
#given_destination ⇒ Object
readonly
:nodoc:.
-
#relative_destination ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#convert_encoded_instructions(filename) ⇒ Object
protected
Filenames in the encoded form are converted.
-
#exists? ⇒ Boolean
Checks if the destination file already exists.
-
#initialize(base, destination, config = {}) ⇒ EmptyDirectory
constructor
Initializes given the source and destination.
- #invoke! ⇒ Object
-
#invoke_with_conflict_check(&block) ⇒ Object
protected
Receives a hash of options and just execute the block if some conditions are met.
-
#on_conflict_behavior ⇒ Object
protected
What to do when the destination file already exists.
- #on_file_clash_behavior ⇒ Object protected
-
#pretend? ⇒ Boolean
protected
Shortcut for pretend.
- #revoke! ⇒ Object
-
#say_status(status, color) ⇒ Object
protected
Shortcut to say_status shell method.
Constructor Details
#initialize(base, destination, config = {}) ⇒ EmptyDirectory
Initializes given the source and destination.
Parameters
- base<Thor::Base>
-
A Thor::Base instance
- source<String>
-
Relative path to the source of this file
- destination<String>
-
Relative path to the destination of this file
- config<Hash>
-
give :verbose => false to not log the status.
34 35 36 37 38 |
# File 'lib/thor/actions/empty_directory.rb', line 34 def initialize(base, destination, config = {}) @base = base @config = {:verbose => true}.merge(config) self.destination = destination end |
Instance Attribute Details
#base ⇒ Object (readonly)
:nodoc:
24 25 26 |
# File 'lib/thor/actions/empty_directory.rb', line 24 def base @base end |
#config ⇒ Object (readonly)
:nodoc:
24 25 26 |
# File 'lib/thor/actions/empty_directory.rb', line 24 def config @config end |
#destination ⇒ Object
:nodoc:
24 25 26 |
# File 'lib/thor/actions/empty_directory.rb', line 24 def destination @destination end |
#given_destination ⇒ Object (readonly)
:nodoc:
24 25 26 |
# File 'lib/thor/actions/empty_directory.rb', line 24 def given_destination @given_destination end |
#relative_destination ⇒ Object (readonly)
:nodoc:
24 25 26 |
# File 'lib/thor/actions/empty_directory.rb', line 24 def relative_destination @relative_destination end |
Instance Method Details
#convert_encoded_instructions(filename) ⇒ Object (protected)
Filenames in the encoded form are converted. If you have a file:
%file_name%.rb
It calls #file_name from the base and replaces %-string with the return value (should be String) of #file_name:
user.rb
The method referenced can be either public or private.
103 104 105 106 107 108 |
# File 'lib/thor/actions/empty_directory.rb', line 103 def convert_encoded_instructions(filename) filename.gsub(/%(.*?)%/) do |initial_string| method = $1.strip base.respond_to?(method, true) ? base.send(method) : initial_string end end |
#exists? ⇒ Boolean
Checks if the destination file already exists.
Returns
- Boolean
-
true if the file exists, false otherwise.
45 46 47 |
# File 'lib/thor/actions/empty_directory.rb', line 45 def exists? ::File.exist?(destination) end |
#invoke! ⇒ Object
49 50 51 52 53 54 |
# File 'lib/thor/actions/empty_directory.rb', line 49 def invoke! invoke_with_conflict_check do require "fileutils" ::FileUtils.mkdir_p(destination) end end |
#invoke_with_conflict_check(&block) ⇒ Object (protected)
Receives a hash of options and just execute the block if some conditions are met.
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/thor/actions/empty_directory.rb', line 113 def invoke_with_conflict_check(&block) if exists? on_conflict_behavior(&block) else yield unless pretend? say_status :create, :green end destination rescue Errno::EISDIR, Errno::EEXIST on_file_clash_behavior end |
#on_conflict_behavior ⇒ Object (protected)
What to do when the destination file already exists.
132 133 134 |
# File 'lib/thor/actions/empty_directory.rb', line 132 def on_conflict_behavior say_status :exist, :blue end |
#on_file_clash_behavior ⇒ Object (protected)
126 127 128 |
# File 'lib/thor/actions/empty_directory.rb', line 126 def on_file_clash_behavior say_status :file_clash, :red end |
#pretend? ⇒ Boolean (protected)
Shortcut for pretend.
67 68 69 |
# File 'lib/thor/actions/empty_directory.rb', line 67 def pretend? base.[:pretend] end |
#revoke! ⇒ Object
56 57 58 59 60 61 |
# File 'lib/thor/actions/empty_directory.rb', line 56 def revoke! say_status :remove, :red require "fileutils" ::FileUtils.rm_rf(destination) if !pretend? && exists? given_destination end |
#say_status(status, color) ⇒ Object (protected)
Shortcut to say_status shell method.
138 139 140 |
# File 'lib/thor/actions/empty_directory.rb', line 138 def say_status(status, color) base.shell.say_status status, relative_destination, color if config[:verbose] end |