class Pio::Options

User options utility.

Public Class Methods

mandatory_option(name) click to toggle source
# File lib/pio/options.rb, line 6
def self.mandatory_option(name)
  if const_defined?(:MANDATORY_OPTIONS)
    const_get(:MANDATORY_OPTIONS) << name
  else
    const_set(:MANDATORY_OPTIONS, [name])
  end
end
new(options) click to toggle source
# File lib/pio/options.rb, line 19
def initialize(options)
  validate options
  @options = options
end
option(name) click to toggle source
# File lib/pio/options.rb, line 14
def self.option(name)
  const_set(:OPTIONS, []) unless const_defined?(:OPTIONS)
  const_get(:OPTIONS) << name
end

Private Instance Methods

check_existence(user_options, key) click to toggle source
# File lib/pio/options.rb, line 62
def check_existence(user_options, key)
  value = user_options.fetch(key) do |missing_key|
    raise ArgumentError, "The #{missing_key} option should be passed."
  end
  return if value
  raise(ArgumentError, "The #{key} option shouldn't be #{value.inspect}.")
end
check_mandatory(user_options) click to toggle source
# File lib/pio/options.rb, line 56
def check_mandatory(user_options)
  self.class.const_get(:MANDATORY_OPTIONS).each do |each|
    check_existence(user_options, each)
  end
end
check_unknown(user_options) click to toggle source
# File lib/pio/options.rb, line 49
def check_unknown(user_options)
  valid_options = mandatory_options + options
  user_options.keys.each do |each|
    raise "Unknown option: #{each}." unless valid_options.include?(each)
  end
end
mandatory_options() click to toggle source
# File lib/pio/options.rb, line 31
def mandatory_options
  klass = self.class
  if klass.const_defined?(:MANDATORY_OPTIONS)
    klass.const_get(:MANDATORY_OPTIONS)
  else
    []
  end
end
options() click to toggle source
# File lib/pio/options.rb, line 40
def options
  klass = self.class
  if klass.const_defined?(:OPTIONS)
    klass.const_get(:OPTIONS)
  else
    []
  end
end
validate(user_options) click to toggle source
# File lib/pio/options.rb, line 26
def validate(user_options)
  check_unknown user_options
  check_mandatory user_options
end