Module: Flucti::Utilities::Miscellaneous
- Extended by:
- Miscellaneous
- Included in:
- Flucti::Utilities, Miscellaneous
- Defined in:
- lib/flucti/utilities/miscellaneous.rb
Constant Summary collapse
- BOGUS_ERROR_MESSAGES =
['error']
Instance Method Summary collapse
- #clean_name(name) ⇒ Object
- #command(task_name = nil) ⇒ Object
- #error!(messages, note = nil, io = $stderr) ⇒ Object
- #sh(command) ⇒ Object
- #try_save(*resources) ⇒ Object
Instance Method Details
#clean_name(name) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/flucti/utilities/miscellaneous.rb', line 56 def clean_name(name) name.to_s. gsub(/["']/, ''). gsub(/[^a-z0-9]+/i, "-"). gsub(/^\-|\-$/, ''). downcase end |
#command(task_name = nil) ⇒ Object
36 37 38 39 |
# File 'lib/flucti/utilities/miscellaneous.rb', line 36 def command(task_name=nil) @program ||= ((path = Pathname($0)).absolute? ? path.basename : path).to_s [@program, task_name].compact.join(' ') end |
#error!(messages, note = nil, io = $stderr) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/flucti/utilities/miscellaneous.rb', line 19 def error!(, note=nil, io=$stderr) = ['errors'] if .respond_to?(:[]) && .include?('errors') = ['error'] if .respond_to?(:[]) && .include?('error') = [].flatten.compact - BOGUS_ERROR_MESSAGES << "An unknown error occured while processing your request." if .empty? if method(:exit).arity == -1 puts_title(.size == 1 ? "Error" : "Errors", io) io.puts(note, "\n") if note .each do || puts_long("! #{.lstrip}", io) end exit 1 else raise .map { |msg| msg.split("\n") }.flatten.map { |line| line.strip }.join(' ') end end |
#sh(command) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/flucti/utilities/miscellaneous.rb', line 41 def sh(command) command = command.to_s displayable_command = command[0,20] displayable_command += "..." if command.length > 20 print "Running locally: #{displayable_command} " succeeded = system(command) if succeeded print "Done\n" else raise "command #{q displayable_command} failed (status: #{$?.exitstatus})" end end |
#try_save(*resources) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/flucti/utilities/miscellaneous.rb', line 8 def try_save(*resources) resources.each do |resource| begin resource.save rescue WebService::ResourceInvalid error! $!.response.data, "(While attempting to save resource #{q resource}: #{resource.inspect}.)" end end yield end |