Module: Oni::Configurable

Included in:
Daemon, Mapper, Worker
Defined in:
lib/oni/configurable.rb

Overview

Configurable is a basic configuration mixin that can be used to set options on class level and easily access them on instance level, optionally only evaluating the setting when it's accessed.

Basic usage:

class SomeClass
  include Oni::Configurable

  set :threads, 5
  set :logger, proc { Logger.new(STDOUT) }

  def some_method
    option(:threads).times do
      # ...
    end
  end
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(into) ⇒ Object

Parameters:

  • into (Class|Module)


26
27
28
# File 'lib/oni/configurable.rb', line 26

def self.included(into)
  into.extend(ClassMethods)
end

Instance Method Details

#option(name, default = nil) ⇒ Mixed

Returns the value of the given option. If the value responds to #call the method is invoked and the return value of this call is returned.

Parameters:

  • name (Symbol|String)
  • default (Mixed) (defaults to: nil)

    The default value to return if no custom one was found.

Returns:

  • (Mixed)


39
40
41
42
43
44
# File 'lib/oni/configurable.rb', line 39

def option(name, default = nil)
  value = self.class.options[name.to_sym]
  value = default if default and !value

  if value.respond_to? :call then value.call else value end
end

#require_option!(option) ⇒ Object

Raises an error if the given option isn't set.

Parameters:

  • option (Symbol|String)

Raises:

  • (ArgumentError)


52
53
54
55
56
# File 'lib/oni/configurable.rb', line 52

def require_option!(option)
  unless option(option)
    raise ArgumentError, "The option #{option} is required but isn't set"
  end
end