Class: Pandocomatic::Command
- Inherits:
-
Object
- Object
- Pandocomatic::Command
- Defined in:
- lib/pandocomatic/command/command.rb
Overview
Command is a base class of all actions pandocomatic executes while converting a file or a directory of files.
Direct Known Subclasses
ConvertFileCommand, ConvertListCommand, CopyFileCommand, CreateLinkCommand, SkipCommand
Constant Summary collapse
- @@total =
rubocop:disable Style/ClassVars
0- @@dry_run =
false- @@quiet =
false- @@src_root =
'.'- @@modified_only =
false
Instance Attribute Summary collapse
-
#errors ⇒ Error[]
List of errors created while preparing and running a command.
-
#index ⇒ Number
The index of this Command in the list with all commands to run when running pandocomatic.
Class Method Summary collapse
-
.reset(configuration) ⇒ Object
Reset all Commands.
Instance Method Summary collapse
-
#all_errors ⇒ Error[]
Get all the errors generated while executing this Command.
-
#count ⇒ Number
The number of commands executed by this Command; a Command can have sub commands as well.
-
#directory? ⇒ Boolean
Is this Command converting a directory?.
-
#dry_run? ⇒ Boolean
Does this Command not actually execute?.
-
#errors? ⇒ Error[]
Has this Command run in any errors?.
-
#execute ⇒ Object
Execute this Command.
-
#file_modified?(src, dst) ⇒ Boolean
Is the source file newer than the destination file?.
-
#index_to_s ⇒ String
Convert this Command’s index to a string representation.
-
#initialize ⇒ Command
constructor
Create a new Command.
-
#make_quiet ⇒ Object
Make this Command run quietly.
-
#modified_only? ⇒ Boolean
Is this Command only executed on modified files?.
-
#multiple? ⇒ Boolean
Does this Command convert a file multiple times?.
-
#quiet? ⇒ Boolean
Is this Command executed silently?.
-
#run ⇒ Object
Actually run this Command.
-
#runnable? ⇒ Boolean
Are there any errors while configuring this Command? If not, this Command is runnable.
-
#skip? ⇒ Boolean
Will this Command be skipped, thus not executed?.
-
#src_root ⇒ String
Get the root directory of this Command’s conversion process.
-
#to_s ⇒ String
Create a String representation of this Command.
-
#uncount ⇒ Object
Decrement the total number of conversion commands by 1.
Constructor Details
#initialize ⇒ Command
Create a new Command
46 47 48 49 50 |
# File 'lib/pandocomatic/command/command.rb', line 46 def initialize @errors = [] @@total += 1 @index = @@total end |
Instance Attribute Details
#errors ⇒ Error[]
Returns list of errors created while preparing and running a command.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/pandocomatic/command/command.rb', line 34 class Command attr_reader :errors, :index # rubocop:disable Style/ClassVars @@total = 0 @@dry_run = false @@quiet = false @@src_root = '.' @@modified_only = false # Create a new Command def initialize @errors = [] @@total += 1 @index = @@total end # Reset all Commands # # @param configuration [Configuration] the configuration used to convert def self.reset(configuration) @@src_root = configuration.src_root @@dry_run = configuration.dry_run? @@quiet = configuration.quiet? @@modified_only = configuration.modified_only? @@total = 0 end # Get the root directory of this Command's conversion process # # @return [String] def src_root @@src_root end # Does this Command not actually execute? # # @return [Boolean] def dry_run? @@dry_run end # Is this Command executed silently? # # @return [Boolean] def quiet? @@quiet end # Is this Command only executed on modified files? # # @return [Boolean] def modified_only? @@modified_only end # The number of commands executed by this Command; a Command can have sub # commands as well. # # @return [Number] def count 1 end # Get all the errors generated while executing this Command # # @return [Error[]] def all_errors @errors end # Make this Command run quietly def make_quiet @@quiet = true end # Convert this Command's index to a string representation # # @return [String] def index_to_s (@@total - @index + 1).to_s.rjust(@@total.to_s.size) end # Execute this Command. A Command can be dry-run as well, in which it is # not actually run. def execute description = CommandPrinter.new(self) Pandocomatic::LOG.info description description.print unless quiet? run if !dry_run? && runnable? end # Actually run this Command def run; end # Are there any errors while configuring this Command? If not, this # Command is runnable. # # @return [Boolean] def runnable? !errors? end # Create a String representation of this Command # # @return [String] def to_s 'command' end # Is this Command converting a directory? # # @return [Boolean] false def directory? false end # Does this Command convert a file multiple times? # # @return [Boolean] false def multiple? false end # Will this Command be skipped, thus not executed? # # @return [Boolean] false def skip? false end # Decrement the total number of conversion commands by 1 def uncount @@total -= 1 end # rubocop:enable Style/ClassVars # Has this Command run in any errors? # # @return [Error[]] def errors? !@errors.empty? end # Is the source file newer than the destination file? # # @param src [String] the source file # @param dst [String] the destination file # # @return [Boolean] True if src has been modified after dst has been last def file_modified?(src, dst) !File.exist? dst or File.mtime(src) > File.mtime(dst) end end |
#index ⇒ Number
Returns the index of this Command in the list with all commands to run when running pandocomatic.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/pandocomatic/command/command.rb', line 34 class Command attr_reader :errors, :index # rubocop:disable Style/ClassVars @@total = 0 @@dry_run = false @@quiet = false @@src_root = '.' @@modified_only = false # Create a new Command def initialize @errors = [] @@total += 1 @index = @@total end # Reset all Commands # # @param configuration [Configuration] the configuration used to convert def self.reset(configuration) @@src_root = configuration.src_root @@dry_run = configuration.dry_run? @@quiet = configuration.quiet? @@modified_only = configuration.modified_only? @@total = 0 end # Get the root directory of this Command's conversion process # # @return [String] def src_root @@src_root end # Does this Command not actually execute? # # @return [Boolean] def dry_run? @@dry_run end # Is this Command executed silently? # # @return [Boolean] def quiet? @@quiet end # Is this Command only executed on modified files? # # @return [Boolean] def modified_only? @@modified_only end # The number of commands executed by this Command; a Command can have sub # commands as well. # # @return [Number] def count 1 end # Get all the errors generated while executing this Command # # @return [Error[]] def all_errors @errors end # Make this Command run quietly def make_quiet @@quiet = true end # Convert this Command's index to a string representation # # @return [String] def index_to_s (@@total - @index + 1).to_s.rjust(@@total.to_s.size) end # Execute this Command. A Command can be dry-run as well, in which it is # not actually run. def execute description = CommandPrinter.new(self) Pandocomatic::LOG.info description description.print unless quiet? run if !dry_run? && runnable? end # Actually run this Command def run; end # Are there any errors while configuring this Command? If not, this # Command is runnable. # # @return [Boolean] def runnable? !errors? end # Create a String representation of this Command # # @return [String] def to_s 'command' end # Is this Command converting a directory? # # @return [Boolean] false def directory? false end # Does this Command convert a file multiple times? # # @return [Boolean] false def multiple? false end # Will this Command be skipped, thus not executed? # # @return [Boolean] false def skip? false end # Decrement the total number of conversion commands by 1 def uncount @@total -= 1 end # rubocop:enable Style/ClassVars # Has this Command run in any errors? # # @return [Error[]] def errors? !@errors.empty? end # Is the source file newer than the destination file? # # @param src [String] the source file # @param dst [String] the destination file # # @return [Boolean] True if src has been modified after dst has been last def file_modified?(src, dst) !File.exist? dst or File.mtime(src) > File.mtime(dst) end end |
Class Method Details
.reset(configuration) ⇒ Object
Reset all Commands
55 56 57 58 59 60 61 |
# File 'lib/pandocomatic/command/command.rb', line 55 def self.reset(configuration) @@src_root = configuration.src_root @@dry_run = configuration.dry_run? @@quiet = configuration.quiet? @@modified_only = configuration.modified_only? @@total = 0 end |
Instance Method Details
#all_errors ⇒ Error[]
Get all the errors generated while executing this Command
102 103 104 |
# File 'lib/pandocomatic/command/command.rb', line 102 def all_errors @errors end |
#count ⇒ Number
The number of commands executed by this Command; a Command can have sub commands as well.
95 96 97 |
# File 'lib/pandocomatic/command/command.rb', line 95 def count 1 end |
#directory? ⇒ Boolean
Is this Command converting a directory?
148 149 150 |
# File 'lib/pandocomatic/command/command.rb', line 148 def directory? false end |
#dry_run? ⇒ Boolean
Does this Command not actually execute?
73 74 75 |
# File 'lib/pandocomatic/command/command.rb', line 73 def dry_run? @@dry_run end |
#errors? ⇒ Error[]
Has this Command run in any errors?
176 177 178 |
# File 'lib/pandocomatic/command/command.rb', line 176 def errors? !@errors.empty? end |
#execute ⇒ Object
Execute this Command. A Command can be dry-run as well, in which it is not actually run.
120 121 122 123 124 125 |
# File 'lib/pandocomatic/command/command.rb', line 120 def execute description = CommandPrinter.new(self) Pandocomatic::LOG.info description description.print unless quiet? run if !dry_run? && runnable? end |
#file_modified?(src, dst) ⇒ Boolean
Is the source file newer than the destination file?
186 187 188 |
# File 'lib/pandocomatic/command/command.rb', line 186 def file_modified?(src, dst) !File.exist? dst or File.mtime(src) > File.mtime(dst) end |
#index_to_s ⇒ String
Convert this Command’s index to a string representation
114 115 116 |
# File 'lib/pandocomatic/command/command.rb', line 114 def index_to_s (@@total - @index + 1).to_s.rjust(@@total.to_s.size) end |
#make_quiet ⇒ Object
Make this Command run quietly
107 108 109 |
# File 'lib/pandocomatic/command/command.rb', line 107 def make_quiet @@quiet = true end |
#modified_only? ⇒ Boolean
Is this Command only executed on modified files?
87 88 89 |
# File 'lib/pandocomatic/command/command.rb', line 87 def modified_only? @@modified_only end |
#multiple? ⇒ Boolean
Does this Command convert a file multiple times?
155 156 157 |
# File 'lib/pandocomatic/command/command.rb', line 155 def multiple? false end |
#quiet? ⇒ Boolean
Is this Command executed silently?
80 81 82 |
# File 'lib/pandocomatic/command/command.rb', line 80 def quiet? @@quiet end |
#run ⇒ Object
Actually run this Command
128 |
# File 'lib/pandocomatic/command/command.rb', line 128 def run; end |
#runnable? ⇒ Boolean
Are there any errors while configuring this Command? If not, this Command is runnable.
134 135 136 |
# File 'lib/pandocomatic/command/command.rb', line 134 def runnable? !errors? end |
#skip? ⇒ Boolean
Will this Command be skipped, thus not executed?
162 163 164 |
# File 'lib/pandocomatic/command/command.rb', line 162 def skip? false end |
#src_root ⇒ String
Get the root directory of this Command’s conversion process
66 67 68 |
# File 'lib/pandocomatic/command/command.rb', line 66 def src_root @@src_root end |
#to_s ⇒ String
Create a String representation of this Command
141 142 143 |
# File 'lib/pandocomatic/command/command.rb', line 141 def to_s 'command' end |
#uncount ⇒ Object
Decrement the total number of conversion commands by 1
167 168 169 |
# File 'lib/pandocomatic/command/command.rb', line 167 def uncount @@total -= 1 end |