class EacRubyUtils::OptionsConsumer
Constants
- DEFAULT_OPTIONS
Attributes
data[R]
Public Class Methods
new(data)
click to toggle source
# File lib/eac_ruby_utils/options_consumer.rb, line 10 def initialize(data) @data = data.with_indifferent_access end
Public Instance Methods
consume(key, default_value = nil) { |value| ... }
click to toggle source
# File lib/eac_ruby_utils/options_consumer.rb, line 14 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)
click to toggle source
If last argument is a Hash
it is used a options. Options:
-
validate
: validate after consume. -
ostruct
: return a [OpenStruct] instead a [Hash].
@return [Hash] (Default) or [OpenStruct].
# File lib/eac_ruby_utils/options_consumer.rb, line 27 def consume_all(*keys) options = consume_all_extract_options(keys) result = consume_all_build_result(keys, options.fetch(:ostruct)) validate if options.fetch(:validate) result end
left_data()
click to toggle source
# File lib/eac_ruby_utils/options_consumer.rb, line 40 def left_data data.dup end
validate()
click to toggle source
# File lib/eac_ruby_utils/options_consumer.rb, line 34 def validate return if data.empty? raise "Invalid keys: #{data.keys}" end
Private Instance Methods
consume_all_build_result(keys, ostruct)
click to toggle source
# File lib/eac_ruby_utils/options_consumer.rb, line 54 def consume_all_build_result(keys, ostruct) if ostruct ::OpenStruct.new(keys.map { |key| [key, consume(key)] }.to_h) else keys.map { |key| consume(key) } end end
consume_all_extract_options(keys)
click to toggle source
# File lib/eac_ruby_utils/options_consumer.rb, line 48 def consume_all_extract_options(keys) options = DEFAULT_OPTIONS options = options.merge(keys.pop.with_indifferent_access) if keys.last.is_a?(Hash) options end