module Confo::OptionsManager

Public Instance Methods

[](option)

Alias for get.

Alias for: get
[]=(arg, *args)

Alias for set.

Alias for: set
get(option) click to toggle source

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
Also aliased as: []
init(*args)

Alias for set_at_first.

Alias for: set_at_first
keys() click to toggle source

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(option, *args) click to toggle source

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

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
result_of(option, *args) click to toggle source

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
set(arg, *args) click to toggle source

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
Also aliased as: []=
set?(arg, *rest_args) click to toggle source

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
set_at_first(*args) click to toggle source

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
Also aliased as: init
unset(option) click to toggle source

Unsets option.

# File lib/confo/concerns/options_manager.rb, line 154
def unset(option)
  options_storage.delete(option)
  self
end
values() click to toggle source

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

options_storage() click to toggle source
# File lib/confo/concerns/options_manager.rb, line 182
def options_storage
  @options_storage ||= OptionsStorage.new
end
public_get(option) click to toggle source

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
public_set(option, value) click to toggle source

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
raw_get(option) click to toggle source

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
raw_set(option, value) click to toggle source

Internal method to set option.

# File lib/confo/concerns/options_manager.rb, line 106
def raw_set(option, value)
  options_storage[option] = value
end