Module: HollaBack::OptionLoader

Included in:
Response
Defined in:
lib/holla_back/option_loader.rb

Overview

Module of methods for loading options

Class Method Summary collapse

Class Method Details

.load_option(option, options) ⇒ Class, Exception

Loads one option into an instance variable and raises if it’s missing the option

Examples:

load_option('foo', {:foo => 'bar'})
@foo #=> 'bar'

Parameters:

  • option (String)

    the name of the instance variable you want

  • options (Hash)

    the hash of options to fetch the value of the instance variable

Returns:

  • (Class)

    the class type of the fetchted value in the options hash

  • (Exception)

    an exception will be raised if the option isn’t found in the options hash



15
16
17
# File 'lib/holla_back/option_loader.rb', line 15

def load_option(option, options)
  instance_variable_set("@#{option}", options.fetch(option.to_sym) { raise "Missing required option: #{option}" } )
end

.load_options(options, *option_names) ⇒ Class, Exception

Loads multiple options in an array from an options hash

Examples:

load_options({:foo1 => 'bar1', :foo2 => 'bar2'}, :foo1, :foo2)
@foo1 #=> 'bar1'
@foo2 #=> 'bar2'

Parameters:

  • option_names (Array)

    the array of option names to fetch from the options hash

  • options (Hash)

    the hash of options which hold values for each option name

Returns:

  • (Class)

    the class type of the fetched value in the options hash

  • (Exception)

    an exception will be raised if the option isn’t found in the options hash

See Also:

  • #load_option


33
34
35
# File 'lib/holla_back/option_loader.rb', line 33

def load_options(options, *option_names)
  option_names.each{|o| load_option(o, options) }
end