Module: Clive::StateActions
- Included in:
- Base, Command, Option::Runner
- Defined in:
- lib/clive/option/runner.rb
Overview
Methods for modifying a state object. Requires that the instance variable @state exists and that it responds to #[]
, #store
and #key?
.
Instance Method Summary collapse
-
#get(key) ⇒ Object
Gets the value for the key, returns nil if the key does not exist.
-
#get!(key) ⇒ Object
Gets the value for the key, raises an error if the key does not exist.
-
#has?(key) ⇒ Boolean
State has
key
?. -
#set(key, value) ⇒ Object
Sets the key to the value given.
- #update(*args) ⇒ Object
Instance Method Details
#get(key) ⇒ Object
Gets the value for the key, returns nil if the key does not exist.
17 18 19 |
# File 'lib/clive/option/runner.rb', line 17 def get(key) @state[key] end |
#get!(key) ⇒ Object
Gets the value for the key, raises an error if the key does not exist.
25 26 27 28 29 30 31 |
# File 'lib/clive/option/runner.rb', line 25 def get!(key) if has?(key) get(key) else raise KeyError, "key not found: #{key}" end end |
#has?(key) ⇒ Boolean
Returns State has key
?.
93 94 95 |
# File 'lib/clive/option/runner.rb', line 93 def has?(key) @state.key? key end |
#set(key, value) ⇒ Object
Sets the key to the value given.
43 44 45 |
# File 'lib/clive/option/runner.rb', line 43 def set(key, value) @state.store key, value end |
#update(key, method, *args) ⇒ Object #update(key, &block) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/clive/option/runner.rb', line 69 def update(*args) if block_given? key = args.first set key, yield(get(key)) elsif args.size > 1 key, method = args.shift, args.shift set key, get(key).send(method, *args) else raise ArgumentError, "wrong number of arguments (#{args.size} for 2)" end end |