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
-
#get(option) ⇒ Object
(also: #[])
Returns option value: obj.get(:option) => ‘value’ obj.get(‘option’) => ‘value’.
-
#keys ⇒ Object
Returns option names as array of symbols.
-
#option(option, *args) ⇒ Object
Option accessor in functional style: obj.option(:option, ‘value’) obj.option(:option) => ‘value’.
-
#options ⇒ Object
Returns all options at once.
-
#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.
-
#set(arg, *args) ⇒ Object
(also: #[]=)
Sets option: obj.set(:option, ‘value’) obj.set(‘option’, ‘value’) obj.set({ foo: ‘1’, bar: ‘2’, baz: -> { 3 } }).
-
#set?(arg, *rest_args) ⇒ Boolean
Checks if option is set.
-
#set_at_first(*args) ⇒ Object
(also: #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.
-
#unset(option) ⇒ Object
Unsets option.
-
#values ⇒ Object
Returns option values as array.
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 |
#keys ⇒ Object
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 |
#options ⇒ Object
Returns all options at once.
obj. => { option: 'value' }
164 165 166 167 168 |
# File 'lib/confo/concerns/options_manager.rb', line 164 def = storage.dup .each { |k, v| [k] = get(k) } 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.
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 |
#values ⇒ Object
Returns option values as array.
158 159 160 |
# File 'lib/confo/concerns/options_manager.rb', line 158 def values storage.values end |