module MiGA::Common::WithOption

Helper module including specific functions to handle objects that have configurable options. The class including this module must implement the methods .OPTIONS, #metadata, and #save.

Public Instance Methods

all_options() click to toggle source
# File lib/miga/common/with_option.rb, line 22
def all_options
  Hash[self.class.OPTIONS.each_key.map { |key| [key, option(key)] }]
end
assert_has_option(key) click to toggle source
# File lib/miga/common/with_option.rb, line 38
def assert_has_option(key)
  opt = self.class.OPTIONS[key.to_sym]
  raise "Unrecognized option: #{key}" if opt.nil?
  opt
end
assert_valid_option_value(key, value, from_string = false) click to toggle source
# File lib/miga/common/with_option.rb, line 44
def assert_valid_option_value(key, value, from_string = false)
  opt = assert_has_option(key)
  value = option_from_string(key, value) if from_string

  # nil is always valid, and so are supported tokens
  return value if value.nil? || opt[:tokens]&.include?(value)

  if opt[:type] && !value.is_a?(opt[:type])
    raise "Invalid value type for #{key}: #{value.class}, not #{opt[:type]}"
  end

  if opt[:in] && !opt[:in].include?(value)
    raise "Value out of range for #{key}: #{value}, not in #{opt[:in]}"
  end

  value
end
option(key) click to toggle source
# File lib/miga/common/with_option.rb, line 8
def option(key)
  assert_has_option(key)
  opt = option_by_metadata(key)
  value = opt.nil? ? option_by_default(key) : opt
  value = value[self] if value.is_a?(Proc)
  value
end
option?(key) click to toggle source
# File lib/miga/common/with_option.rb, line 26
def option?(key)
  !self.class.OPTIONS[key.to_sym].nil?
end
option_by_default(key) click to toggle source
# File lib/miga/common/with_option.rb, line 34
def option_by_default(key)
  self.class.OPTIONS[key.to_sym][:default]
end
option_by_metadata(key) click to toggle source
# File lib/miga/common/with_option.rb, line 30
def option_by_metadata(key)
  metadata[key]
end
option_from_string(key, value) click to toggle source
# File lib/miga/common/with_option.rb, line 62
def option_from_string(key, value)
  opt = assert_has_option(key)

  if ['', 'nil'].include?(value)
    nil
  elsif opt[:tokens]&.include?(value)
    value
  elsif opt[:type]&.equal?(Float)
    raise "Not a float: #{value}" unless value =~ /^-?\.?\d/
    value.to_f
  elsif opt[:type]&.equal?(Integer)
    raise "Not an integer: #{value}" unless value =~ /^-?\d/
    value.to_i
  elsif opt[:in]&.include?(true) && value == 'true'
    true
  elsif opt[:in]&.include?(false) && value == 'false'
    false
  else
    value
  end
end
set_option(key, value, from_string = false) click to toggle source
# File lib/miga/common/with_option.rb, line 16
def set_option(key, value, from_string = false)
  metadata[key] = assert_valid_option_value(key, value, from_string)
  save
  option(key)
end