class HammerCLI::Options::Validators::DSL::BaseConstraint

Attributes

rejected_msg[R]
required_msg[R]

Public Class Methods

new(option_definitions, option_values, to_check) click to toggle source
# File lib/hammer_cli/options/validators/dsl.rb, line 8
def initialize(option_definitions, option_values, to_check)
  @to_check = to_check
  @rejected_msg = ""
  @required_msg = ""

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

Public Instance Methods

exist?() click to toggle source
# File lib/hammer_cli/options/validators/dsl.rb, line 29
def exist?
  raise NotImplementedError
end
rejected(args={}) click to toggle source
# File lib/hammer_cli/options/validators/dsl.rb, line 19
def rejected(args={})
  msg = args[:msg] || rejected_msg % option_switches.join(", ")
  raise HammerCLI::Options::Validators::ValidationError.new(msg) if exist?
end
required(args={}) click to toggle source
# File lib/hammer_cli/options/validators/dsl.rb, line 24
def required(args={})
  msg = args[:msg] || required_msg % option_switches.join(", ")
  raise HammerCLI::Options::Validators::ValidationError.new(msg) unless exist?
end

Protected Instance Methods

get_option(name) click to toggle source
# File lib/hammer_cli/options/validators/dsl.rb, line 35
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/options/validators/dsl.rb, line 41
def get_option_value(name)
  @option_values[name] || @option_values[name.to_s]
end
option_passed?(option_name) click to toggle source
# File lib/hammer_cli/options/validators/dsl.rb, line 45
def option_passed?(option_name)
  !get_option_value(option_name).nil?
end
option_switches(opts=nil) click to toggle source
# File lib/hammer_cli/options/validators/dsl.rb, line 49
def option_switches(opts=nil)
  opts ||= @to_check
  opts.collect do |option_name|
    get_option(option_name).long_switch || get_option(option_name).switches.first
  end
end