Class: Watchful::Action

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Action

Returns a new instance of Action.

Raises:

  • (ArgumentError)


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(options = {})
	
	options[:dependencies] ||= []
	
	@enabled = true
	
	PROPERTIES.each do |key|
		self.instance_variable_set('@' + key.to_s, options[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

.have_command?(cmd) ⇒ Boolean

todo: better have_command?

Returns:

  • (Boolean)


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

#command_string(input_path) ⇒ Object



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

#enabled?Boolean

Returns:

  • (Boolean)


15
# File 'lib/watchful/action.rb', line 15

def enabled?; @enabled ;end

#has_dependencies?Boolean

Returns:

  • (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.expand_path(d)))
	end
	# todo: more detailed messages about missing dependencies
	puts "Missing dependencies for action \"#{@name}\"" unless have_all
	return have_all
end

#input_file?(path) ⇒ Boolean

does the given path look like a path to which this action could be applied?

Returns:

  • (Boolean)


80
81
82
# File 'lib/watchful/action.rb', line 80

def input_file?(path)
	Watchful::compound_extension_of(path) == @in
end

#output_file?(path) ⇒ Boolean

does the given path look like a path to which this action might write output?

Returns:

  • (Boolean)


75
76
77
# File 'lib/watchful/action.rb', line 75

def output_file?(path)
	Watchful::compound_extension_of(path) == @out
end

#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