Class: EacRubyUtils::OptionsConsumer
- Defined in:
- lib/eac_ruby_utils/options_consumer.rb
Instance Method Summary collapse
- #consume(key, default_value = nil, &block) ⇒ Object
- #consume_all(*keys) ⇒ Object
-
#initialize(data) ⇒ OptionsConsumer
constructor
A new instance of OptionsConsumer.
- #left_data ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize(data) ⇒ OptionsConsumer
Returns a new instance of OptionsConsumer.
7 8 9 |
# File 'lib/eac_ruby_utils/options_consumer.rb', line 7 def initialize(data) @data = data.with_indifferent_access end |
Instance Method Details
#consume(key, default_value = nil, &block) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/eac_ruby_utils/options_consumer.rb', line 11 def consume(key, default_value = nil, &block) return default_value unless data.key?(key) value = data.delete(key) value = yield(value) if block value end |
#consume_all(*keys) ⇒ Object
19 20 21 |
# File 'lib/eac_ruby_utils/options_consumer.rb', line 19 def consume_all(*keys) keys.map { |key| consume(key) } end |
#left_data ⇒ Object
29 30 31 |
# File 'lib/eac_ruby_utils/options_consumer.rb', line 29 def left_data data.dup end |
#validate ⇒ Object
23 24 25 26 27 |
# File 'lib/eac_ruby_utils/options_consumer.rb', line 23 def validate return if data.empty? raise "Invalid keys: #{data.keys}" end |