module Oni::Configurable

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

Public Class Methods

included(into) click to toggle source

@param [Class|Module] into

# File lib/oni/configurable.rb, line 26
def self.included(into)
  into.extend(ClassMethods)
end

Public Instance Methods

option(name, default = nil) click to toggle source

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.

@param [Symbol|String] name @param [Mixed] default The default value to return if no custom one was

found.

@return [Mixed]

# 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) click to toggle source

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

@param [Symbol|String] option @raise [ArgumentError]

# 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