Module: Oni::Configurable
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
-
#option(name, default = nil) ⇒ Mixed
Returns the value of the given option.
-
#require_option!(option) ⇒ Object
Raises an error if the given option isn't set.
Class Method Details
.included(into) ⇒ Object
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.
39 40 41 42 43 44 45 46 47 |
# File 'lib/oni/configurable.rb', line 39 def option(name, default = nil) value = self.class.[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.
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 |