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' }
51 52 53 |
# File 'lib/confo/concerns/options_manager.rb', line 51 def get(option) public_get(option) end |
#keys ⇒ Object
Returns option names as array of symbols.
160 161 162 163 164 |
# File 'lib/confo/concerns/options_manager.rb', line 160 def keys .each_with_object([]) do |(k, v), memo| memo << k.to_sym end end |
#option(option, *args) ⇒ Object
Option accessor in functional style:
obj.option(:option, 'value')
obj.option(:option) => 'value'
128 129 130 |
# File 'lib/confo/concerns/options_manager.rb', line 128 def option(option, *args) args.size > 0 ? set(option, args.first) : get(option) end |
#options ⇒ Object
Returns all options at once.
obj. => { option: 'value' }
175 176 177 178 179 |
# File 'lib/confo/concerns/options_manager.rb', line 175 def .each_with_object({}) do |(k, v), memo| memo[k.to_sym] = get(k) end 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
149 150 151 |
# File 'lib/confo/concerns/options_manager.rb', line 149 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 } })
82 83 84 85 86 87 88 89 |
# 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 |
#set?(arg, *rest_args) ⇒ Boolean
Checks if option is set. Works similar to set if value passed but sets only uninitialized options.
134 135 136 137 138 139 140 141 142 143 144 |
# 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 .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
117 118 119 120 |
# File 'lib/confo/concerns/options_manager.rb', line 117 def set_at_first(*args) set?(*args) self end |
#unset(option) ⇒ Object
Unsets option.
154 155 156 157 |
# File 'lib/confo/concerns/options_manager.rb', line 154 def unset(option) .delete(option) self end |
#values ⇒ Object
Returns option values as array.
167 168 169 170 171 |
# File 'lib/confo/concerns/options_manager.rb', line 167 def values .each_with_object([]) do |(k, v), memo| memo << get(k) end end |