Module: Unobtainium::World

Extended by:
ClassMethods
Includes:
Support::Identifiers
Defined in:
lib/unobtainium/world.rb

Overview

The World module combines other modules, defining simpler entry points into the gem’s functionality.

Defined Under Namespace

Modules: ClassMethods

Constant Summary

Constants included from ClassMethods

ClassMethods::DEFAULT_CONFIG_OPTIONS

Instance Method Summary collapse

Methods included from ClassMethods

config_file, config_file=, extended, included, set_config_defaults

Methods included from Support::Identifiers

#identifier

Instance Method Details

#driver(label = nil, options = nil) ⇒ Object

Returns a driver instance with the given options. If no options are provided, options from the global configuration are used.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/unobtainium/world.rb', line 85

def driver(label = nil, options = nil)
  # Resolve unique options
  label, options = resolve_options(label, options)

  # Create a key for the label and options. This should always
  # return the same key for the same label and options.
  key = options['unobtainium_instance_id']
  if key.nil?
    key = identifier('driver', label, options)
  end

  # Only create a driver with this exact configuration once. Unfortunately
  # We'll have to bind the destructor to whatever configuration exists at
  # this point in time, so we have to create a proc here - whether the Driver
  # gets created or not.
  at_end = config.fetch("at_end", "quit")
  dtor = proc do |the_driver|
    # :nocov:
    if the_driver.nil?
      return
    end

    # We'll rescue Exception here because we really want all destructors
    # to run.
    # rubocop:disable Lint/RescueException
    begin
      meth = at_end.to_sym
      the_driver.send(meth)
    rescue Exception => err
      puts "Exception in destructor: [#{err.class}] #{err}"
    end
    # rubocop:enable Lint/RescueException
    # :nocov:
  end
  return ::Unobtainium::Runtime.instance.store_with_if(key, dtor) do
    ::Unobtainium::Driver.create(label, options)
  end
end