Module: MiGA::Common::WithOption

Included in:
Dataset, Project
Defined in:
lib/miga/common/with_option.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#all_optionsObject



22
23
24
# 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) ⇒ Object



38
39
40
41
42
# 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) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/miga/common/with_option.rb', line 8

def option(key)
  assert_has_option(key)
  opt = (key)
  value = opt.nil? ? option_by_default(key) : opt
  value = value[self] if value.is_a?(Proc)
  value
end

#option?(key) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/miga/common/with_option.rb', line 26

def option?(key)
  !self.class.OPTIONS[key.to_sym].nil?
end

#option_by_default(key) ⇒ Object



34
35
36
# 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) ⇒ Object



30
31
32
# File 'lib/miga/common/with_option.rb', line 30

def (key)
  [key]
end

#option_from_string(key, value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object



16
17
18
19
20
# File 'lib/miga/common/with_option.rb', line 16

def set_option(key, value, from_string = false)
  [key] = assert_valid_option_value(key, value, from_string)
  save
  option(key)
end