Class: Watchful::Action
- Inherits:
-
Object
- Object
- Watchful::Action
- Defined in:
- lib/watchful/action.rb
Constant Summary collapse
- PROPERTIES =
todo: support blocks for @in, @out, @command, @dependencies
[:name, :dependencies, :command, :in, :out]
Class Method Summary collapse
-
.have_command?(cmd) ⇒ Boolean
todo: better have_command?.
Instance Method Summary collapse
- #command_string(input_path) ⇒ Object
- #enabled? ⇒ Boolean
- #has_dependencies? ⇒ Boolean
-
#initialize(options = {}) ⇒ Action
constructor
A new instance of Action.
-
#input_file?(path) ⇒ Boolean
does the given path look like a path to which this action could be applied?.
-
#output_file?(path) ⇒ Boolean
does the given path look like a path to which this action might write output?.
-
#output_path_for(source_path) ⇒ Object
given an input file, return the path of an output file.
Constructor Details
permalink #initialize(options = {}) ⇒ Action
Returns a new instance of Action.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/watchful/action.rb', line 17 def initialize( = {}) [:dependencies] ||= [] @enabled = true PROPERTIES.each do |key| self.instance_variable_set('@' + key.to_s, [key]) end @dependencies = [@dependencies] if @dependencies.kind_of? String raise ArgumentError.new("Dependencies option must be a string or array") if not @dependencies.kind_of? Array [@in, @out].each do |i| @enabled = false unless i and i.instance_of?(String) and i.starts_with?('.') end @enabled = false unless @command.instance_of?(String) @enabled = false unless self.has_dependencies? end |
Class Method Details
permalink .have_command?(cmd) ⇒ Boolean
todo: better have_command?
56 57 58 59 60 61 62 63 64 |
# File 'lib/watchful/action.rb', line 56 def Action.have_command?(cmd) raise 'Argument must be a string' if not cmd.kind_of? String raise 'Argument cannot be an empty string' if cmd.empty? ENV['PATH'].split(':').each do |dir| return true if File.exists?("#{dir}/#{cmd}") end false end |
Instance Method Details
permalink #command_string(input_path) ⇒ Object
[View source]
51 52 53 |
# File 'lib/watchful/action.rb', line 51 def command_string(input_path) Kernel.sprintf(@command, input_path, self.output_path_for(input_path)) end |
permalink #enabled? ⇒ Boolean
15 |
# File 'lib/watchful/action.rb', line 15 def enabled?; @enabled ;end |
permalink #has_dependencies? ⇒ Boolean
41 42 43 44 45 46 47 48 49 |
# File 'lib/watchful/action.rb', line 41 def has_dependencies? return true if @dependencies.empty? have_all = @dependencies.any? do |d| (Action.have_command?(d)) || (File.exists?(File.(d))) end # todo: more detailed messages about missing dependencies puts "Missing dependencies for action \"#{@name}\"" unless have_all return have_all end |
permalink #input_file?(path) ⇒ Boolean
does the given path look like a path to which this action could be applied?
80 81 82 |
# File 'lib/watchful/action.rb', line 80 def input_file?(path) Watchful::compound_extension_of(path) == @in end |
permalink #output_file?(path) ⇒ Boolean
does the given path look like a path to which this action might write output?
75 76 77 |
# File 'lib/watchful/action.rb', line 75 def output_file?(path) Watchful::compound_extension_of(path) == @out end |
permalink #output_path_for(source_path) ⇒ Object
given an input file, return the path of an output file
67 68 69 70 71 72 |
# File 'lib/watchful/action.rb', line 67 def output_path_for(source_path) unless self.input_file?(source_path) raise ArgumentError.new("#{source_path} is not an input file for action #{@name}") end return File.dirname(source_path) + '/' + File.basename(source_path, @in) + @out end |