Module: Resourceful::OptionsInterpretation

Included in:
HttpAccessor
Defined in:
lib/resourceful/options_interpretation.rb

Overview

Declarative way of interpreting options hashes

include OptionsInterpretion
def my_method(opts = {})
  extract_opts(opts) do |opts|
    host = opts.extract(:host)
    port = opts.extract(:port, :default => 80) {|p| Integer(p)}
  end
end

Defined Under Namespace

Classes: OptionsInterpreter

Instance Method Summary collapse

Instance Method Details

#extract_opts(opts) {|interpeter| ... } ⇒ Object

Interpret an options hash

Parameters:

  • opts (Hash)

    The options to interpret.

Yields:

  • block that used to interpreter options hash

Yield Parameters:

  • interpeter (Resourceful::OptionsInterpretion::OptionsInterpreter)

    An interpreter that can be used to extract option information from the options hash.



22
23
24
25
26
27
28
29
30
# File 'lib/resourceful/options_interpretation.rb', line 22

def extract_opts(opts, &blk)
  opts = opts.clone
  yield OptionsInterpreter.new(opts)

  unless opts.empty?
    raise ArgumentError, "Unrecognized options: #{opts.keys.join(", ")}"
  end

end