Module: DbAgile::Command::Robust

Includes:
DbAgile::Core::IO::Robustness
Included in:
DbAgile::Command
Defined in:
lib/dbagile/command/robust.rb

Instance Method Summary collapse

Methods included from DbAgile::Core::IO::Robustness

#has_database!, #valid_database_name!, #valid_database_uri!, #valid_schema_files!

Instance Method Details

#ambigous_argument_list!Object

Raises an OptionParser::AmbiguousArgument

Raises:

  • (OptionParser::AmbiguousArgument)


16
17
18
# File 'lib/dbagile/command/robust.rb', line 16

def ambigous_argument_list!
  raise OptionParser::AmbiguousArgument, "Ambiguous options"
end

#assumption_error!(msg) ⇒ Object

Raises a DbAgile::AssumptionFailedError with a specific message



80
81
82
# File 'lib/dbagile/command/robust.rb', line 80

def assumption_error!(msg)
  raise DbAgile::AssumptionFailedError, msg
end

#bad_argument_list!(rest, expected_name = nil) ⇒ Object

Raises an OptionParser::InvalidArgument



7
8
9
10
11
12
13
# File 'lib/dbagile/command/robust.rb', line 7

def bad_argument_list!(rest, expected_name = nil)
  if rest.empty?
    raise OptionParser::MissingArgument, "#{expected_name}"
  else
    raise OptionParser::InvalidArgument, "#{rest.join(' ')}"
  end
end

#has_command!(name, env) ⇒ DbAgile::Command

Asserts that a command exists or raises a NoSuchCommandError.

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dbagile/command/robust.rb', line 64

def has_command!(name, env)
  cmd = DbAgile::Command.command_for(name, env)
  if cmd.nil?
    DbAgile::Command::CATEGORIES.each{|c|
      cmd = DbAgile::Command.command_for("#{c}:#{name}", env)
      return cmd if cmd
    }
    raise DbAgile::NoSuchCommandError, "No such command #{name.inspect}" 
  else
    cmd
  end
end

#is_in!(name, value, candidates) ⇒ Object

Asserts that an argument matches some candidates or raises a OptionParser::InvalidArgument error.



45
46
47
48
49
50
51
# File 'lib/dbagile/command/robust.rb', line 45

def is_in!(name, value, candidates)
  value = valid_argument_list!([ value ], candidates.first.class)
  unless candidates.include?(value)
    raise OptionParser::InvalidArgument, "Expected one of #{candidates.inspect} for #{name}"
  end
  value
end

#valid_argument_list!(rest, *types) ⇒ Object

Parses pending arguments, assert that it contains exactly types.size arguments, and convert them to types.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dbagile/command/robust.rb', line 22

def valid_argument_list!(rest, *types)
  if rest.size == types.size
    result = rest.zip(types).collect do |arg, type|
      if String == type
        arg.to_s
      elsif Symbol == type
        arg.to_sym
      elsif Integer
        arg.to_i
      else
        raise OptionParser::InvalidArgument, arg
      end
    end
    result.size == 1 ? result[0] : result
  elsif rest.size < types.size
    raise OptionParser::MissingArgument
  else
    raise OptionParser::NeedlessArgument, rest
  end
end

#valid_read_file!(file) ⇒ Object

Checks that a file exists and can be read or raises an IO error

Raises:

  • (IOError)


54
55
56
57
# File 'lib/dbagile/command/robust.rb', line 54

def valid_read_file!(file)
  raise IOError, "Unable to read #{file}" unless File.file?(file) and File.readable?(file)
  file
end