class HTTPDisk::Sloptions
Like Slop
, but for sanity checking method options. Useful for library entry points that want to be strict. Example usage:
options = Sloptions.new(options)
do
_1.boolean :force _1.integer :retries, required: true _1.string :hello, default: 'world' ...
end
Attributes
flags[R]
Public Class Methods
new() { |self| ... }
click to toggle source
# File lib/httpdisk/sloptions.rb, line 18 def initialize @flags = {} yield(self) end
parse(options, &block)
click to toggle source
# File lib/httpdisk/sloptions.rb, line 14 def self.parse(options, &block) Sloptions.new(&block).parse(options) end
Public Instance Methods
on(flag, foptions = {})
click to toggle source
_1.on and friends
# File lib/httpdisk/sloptions.rb, line 27 def on(flag, foptions = {}) raise ":#{flag} already defined" if flags[flag] flags[flag] = foptions end
parse(options)
click to toggle source
return parsed options
# File lib/httpdisk/sloptions.rb, line 44 def parse(options) # defaults options = defaults.merge(options.compact) flags.each do |flag, foptions| # nil check value = options[flag] if value.nil? raise ArgumentError, ":#{flag} is required" if foptions[:required] next end # type cast (for boolean) if foptions[:type] == :boolean value = options[flag] = !!options[flag] end # type check types = Array(foptions[:type]) raise ArgumentError, error_message(flag, value, types) if !valid?(value, types) end # return options end
Protected Instance Methods
defaults()
click to toggle source
# File lib/httpdisk/sloptions.rb, line 73 def defaults flags.map { |flag, foptions| [flag, foptions[:default]] }.to_h.compact end
error_message(flag, value, valid)
click to toggle source
nice error message for when value is invalid
# File lib/httpdisk/sloptions.rb, line 96 def error_message(flag, value, valid) classes = valid.compact.map do s = _1.to_s s = s.downcase if s =~ /\b(Array|Float|Hash|Integer|String|Symbol)\b/ s end.join('/') "expected :#{flag} to be #{classes}, not #{value.inspect}" end
valid?(value, types)
click to toggle source
does value match valid?
# File lib/httpdisk/sloptions.rb, line 78 def valid?(value, types) types.any? do case _1 when :array then true if value.is_a?(Array) when :boolean then true # in Ruby everything is a boolean when :float then true if value.is_a?(Float) || value.is_a?(Integer) when :hash then true if value.is_a?(Hash) when :integer then true if value.is_a?(Integer) when :string then true if value.is_a?(String) when :symbol then true if value.is_a?(Symbol) when Class then true if value.is_a?(_1) # for custom checks else raise "unknown flag type #{_1.inspect}" end end end