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 |
# File 'lib/oni/configurable.rb', line 39 def option(name, default = nil) value = self.class.[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.
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 |