class EacRubyUtils::OptionsConsumer
def consume(key, default_value = nil, &block)
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
def consume_all(*keys)
-
(Hash)
- (Default) or [OpenStruct].
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
def consume_all_build_result(keys, ostruct)
def consume_all_build_result(keys, ostruct) if ostruct ::OpenStruct.new(keys.index_with { |key| consume(key) }) # rubocop:disable Style/OpenStructUse else keys.map { |key| consume(key) } end end
def consume_all_extract_options(keys)
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
def initialize(data)
def initialize(data) @data = data.with_indifferent_access end
def left_data
def left_data data.dup end
def validate
def validate return if data.empty? raise "Invalid keys: #{data.keys}" end