class HammerCLI::Validator::BaseConstraint

Attributes

rejected_msg[R]
required_msg[R]

Public Class Methods

new(options, to_check) click to toggle source
# File lib/hammer_cli/validator.rb, line 13
def initialize(options, to_check)
  @to_check = to_check
  @rejected_msg = ""
  @required_msg = ""

  @options = options.inject({}) do |hash, opt|
    hash.update({opt.attribute.attribute_name => opt})
  end
end

Public Instance Methods

exist?() click to toggle source
# File lib/hammer_cli/validator.rb, line 33
def exist?
  raise NotImplementedError
end
rejected(args={}) click to toggle source
# File lib/hammer_cli/validator.rb, line 23
def rejected(args={})
  msg = args[:msg] || rejected_msg % option_switches.join(", ")
  raise ValidationError.new(msg) if exist?
end
required(args={}) click to toggle source
# File lib/hammer_cli/validator.rb, line 28
def required(args={})
  msg = args[:msg] || required_msg % option_switches.join(", ")
  raise ValidationError.new(msg) unless exist?
end

Protected Instance Methods

get_option(name) click to toggle source
# File lib/hammer_cli/validator.rb, line 39
def get_option(name)
  name = name.to_s
  raise _("Unknown option name '%s'") % name unless @options.has_key? name
  @options[name]
end
get_option_value(name) click to toggle source
# File lib/hammer_cli/validator.rb, line 45
def get_option_value(name)
  opt = get_option(name)
  value = opt.get
  if value.nil?
    defaults = opt.command.send(:context)[:defaults]
    value = defaults.get_defaults(name.to_s) if defaults
  end
  value
end
option_passed?(option_name) click to toggle source
# File lib/hammer_cli/validator.rb, line 55
def option_passed?(option_name)
  !get_option_value(option_name).nil?
end
option_switches(opts=nil) click to toggle source
# File lib/hammer_cli/validator.rb, line 59
def option_switches(opts=nil)
  opts ||= @to_check
  opts.collect do |option_name|
    get_option(option_name).attribute.long_switch || get_option(option_name).attribute.switches.first
  end
end