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
45
46
47
# File 'lib/oni/configurable.rb', line 39

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

  if default and !value
    value = default
  end

  return value.respond_to?(:call) ? value.call : value
end

#require_option!(option) ⇒ Object

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

Parameters:

  • option (Symbol|String)

Raises:

  • (ArgumentError)


55
56
57
58
59
# File 'lib/oni/configurable.rb', line 55

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