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

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!(messages, note=nil, io=$stderr)
  messages = messages['errors'] if messages.respond_to?(:[]) && messages.include?('errors')
  messages = messages['error']  if messages.respond_to?(:[]) && messages.include?('error')
  messages = [messages].flatten.compact - BOGUS_ERROR_MESSAGES
  messages << "An unknown error occured while processing your request." if messages.empty?
  if method(:exit).arity == -1
    puts_title(messages.size == 1 ? "Error" : "Errors", io)
    io.puts(note, "\n") if note
    messages.each do |message|
      puts_long("! #{message.lstrip}", io)
    end
    exit 1
  else
    raise messages.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