Class: HTTPDisk::Sloptions
- Inherits:
-
Object
- Object
- HTTPDisk::Sloptions
- Defined in:
- lib/httpdisk/sloptions.rb
Overview
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
Instance Attribute Summary collapse
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize {|_self| ... } ⇒ Sloptions
constructor
A new instance of Sloptions.
-
#on(flag, foptions = {}) ⇒ Object
_1.on and friends.
-
#parse(options) ⇒ Object
return parsed options.
Constructor Details
#initialize {|_self| ... } ⇒ Sloptions
Returns a new instance of Sloptions.
18 19 20 21 |
# File 'lib/httpdisk/sloptions.rb', line 18 def initialize @flags = {} yield(self) end |
Instance Attribute Details
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
12 13 14 |
# File 'lib/httpdisk/sloptions.rb', line 12 def flags @flags end |
Class Method Details
Instance Method Details
#on(flag, foptions = {}) ⇒ Object
_1.on and friends
27 28 29 30 31 |
# File 'lib/httpdisk/sloptions.rb', line 27 def on(flag, = {}) raise ":#{flag} already defined" if flags[flag] flags[flag] = end |
#parse(options) ⇒ Object
return parsed options
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/httpdisk/sloptions.rb', line 44 def parse() # defaults = defaults.merge(.compact) flags.each do |flag, | # nil check value = [flag] if value.nil? raise ArgumentError, ":#{flag} is required" if [:required] next end # type cast (for boolean) if [:type] == :boolean value = [flag] = !![flag] end # type check types = Array([:type]) raise ArgumentError, (flag, value, types) if !valid?(value, types) end # return end |