Module: Confo::OptionsManager

Extended by:
ActiveSupport::Concern
Included in:
Config
Defined in:
lib/confo/concerns/options_manager.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#get(option) ⇒ Object Also known as: []

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' }


43
44
45
# File 'lib/confo/concerns/options_manager.rb', line 43

def get(option)
  public_get(option)
end

#keysObject

Returns option names as array of symbols.



150
151
152
153
154
155
# File 'lib/confo/concerns/options_manager.rb', line 150

def keys
  storage.reduce([]) do |memo, k, v|
    memo << k.to_sym
    memo
  end
end

#option(option, *args) ⇒ Object

Option accessor in functional style:

obj.option(:option, 'value')
obj.option(:option)   => 'value'


118
119
120
# File 'lib/confo/concerns/options_manager.rb', line 118

def option(option, *args)
  args.size > 0 ? set(option, args.first) : get(option)
end

#optionsObject

Returns all options at once.

obj.options => { option: 'value' }


164
165
166
167
168
# File 'lib/confo/concerns/options_manager.rb', line 164

def options
  options = storage.dup
  options.each { |k, v| options[k] = get(k) }
  options
end

#result_of(option, *args) ⇒ Object

If you expect computed value you can pass arguments to it:

obj.set :calculator, -> (num) { num * 2 }
obj.result_of :calculator, 2    => 4


139
140
141
# File 'lib/confo/concerns/options_manager.rb', line 139

def result_of(option, *args)
  Confo.result_of(get(option), *args)
end

#set(arg, *args) ⇒ Object Also known as: []=

Sets option:

obj.set(:option, 'value')
obj.set('option', 'value')
obj.set({ foo: '1', bar: '2', baz: -> { 3 } })


73
74
75
76
77
78
79
80
# File 'lib/confo/concerns/options_manager.rb', line 73

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

#set?(arg, *rest_args) ⇒ Boolean

Checks if option is set. Works similar to set if value passed but sets only uninitialized options.

Returns:

  • (Boolean)


124
125
126
127
128
129
130
131
132
133
134
# File 'lib/confo/concerns/options_manager.rb', line 124

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
    storage.has_key?(arg)
  end
end

#set_at_first(*args) ⇒ Object Also known as: init

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


107
108
109
110
# File 'lib/confo/concerns/options_manager.rb', line 107

def set_at_first(*args)
  set?(*args)
  self
end

#unset(option) ⇒ Object

Unsets option.



144
145
146
147
# File 'lib/confo/concerns/options_manager.rb', line 144

def unset(option)
  storage.delete(option)
  self
end

#valuesObject

Returns option values as array.



158
159
160
# File 'lib/confo/concerns/options_manager.rb', line 158

def values
  storage.values
end