module Confo::OptionsManager
Public Instance Methods
Returns option value:
obj.get(:option) => 'value' obj.get('option') => 'value' obj.set :option, -> { 'value' } obj.get(:option) => 'value' obj.set :option, -> (arg) { 'value' } obj.get(:option) => -> (arg) { 'value' }
# File lib/confo/concerns/options_manager.rb, line 51 def get(option) public_get(option) end
Returns option names as array of symbols.
# File lib/confo/concerns/options_manager.rb, line 160 def keys options_storage.each_with_object([]) do |(k, v), memo| memo << k.to_sym end end
Option accessor in functional style:
obj.option(:option, 'value') obj.option(:option) => 'value'
# File lib/confo/concerns/options_manager.rb, line 128 def option(option, *args) args.size > 0 ? set(option, args.first) : get(option) end
Returns all options at once.
obj.options => { option: 'value' }
# File lib/confo/concerns/options_manager.rb, line 175 def options options_storage.each_with_object({}) do |(k, v), memo| memo[k.to_sym] = get(k) end end
If you expect computed value you can pass arguments to it:
obj.set :calculator, -> (num) { num * 2 } obj.result_of :calculator, 2 => 4
# File lib/confo/concerns/options_manager.rb, line 149 def result_of(option, *args) Confo.result_of(get(option), *args) end
Sets option:
obj.set(:option, 'value') obj.set('option', 'value') obj.set({ foo: '1', bar: '2', baz: -> { 3 } })
# File lib/confo/concerns/options_manager.rb, line 82 def set(arg, *args) if arg.kind_of?(Hash) arg.each { |k, v| public_set(k, v) } elsif args.size > 0 public_set(arg, args.first) end self end
Checks if option is set. Works similar to set if value passed but sets only uninitialized options.
# File lib/confo/concerns/options_manager.rb, line 134 def set?(arg, *rest_args) if arg.kind_of?(Hash) arg.each { |k, v| set(k, v) unless set?(k) } nil elsif rest_args.size > 0 set(arg, rest_args.first) unless set?(arg) true else options_storage.has_key?(arg) end end
Sets option only if it is not set yet:
obj.set_at_first(:option, 1) obj.get(:option) => 1 obj.set_at_first(:option, 2) obj.get(:option) => 1
# File lib/confo/concerns/options_manager.rb, line 117 def set_at_first(*args) set?(*args) self end
Unsets option.
# File lib/confo/concerns/options_manager.rb, line 154 def unset(option) options_storage.delete(option) self end
Returns option values as array.
# File lib/confo/concerns/options_manager.rb, line 167 def values options_storage.each_with_object([]) do |(k, v), memo| memo << get(k) end end
Protected Instance Methods
# File lib/confo/concerns/options_manager.rb, line 182 def options_storage @options_storage ||= OptionsStorage.new end
Method to get an option. If there is an option accessor defined then it will be used. In other cases raw_get
will be used.
# File lib/confo/concerns/options_manager.rb, line 63 def public_get(option) # TODO Prevent subconfigs here respond_to?(option) ? send(option) : raw_get(option) end
Method to set an option. If there is an option accessor defined then it will be used. In other cases raw_set
will be used.
# File lib/confo/concerns/options_manager.rb, line 99 def public_set(option, value) # TODO Prevent subconfigs here method = "#{option}=" respond_to?(method) ? send(method, value) : raw_set(option, value) end
Internal method to get an option. If value is callable without arguments it will be called and result will be returned.
# File lib/confo/concerns/options_manager.rb, line 71 def raw_get(option) value = options_storage[option] Confo.callable_without_arguments?(value) ? value.call : value end
Internal method to set option.
# File lib/confo/concerns/options_manager.rb, line 106 def raw_set(option, value) options_storage[option] = value end