class EacRubyUtils::ArgumentsConsumer::Parser

Attributes

arguments_consumer[R]
data[R]

Public Class Methods

new(arguments_consumer, args) click to toggle source
# File lib/eac_ruby_utils/arguments_consumer.rb, line 28
def initialize(arguments_consumer, args)
  @arguments_consumer = arguments_consumer
  @data = ::ActiveSupport::HashWithIndifferentAccess.new
  @options_found = false
  arguments_consumer.positional.each { |key| data[key] = nil }
  data.merge!(arguments_consumer.default_options)
  args.each_with_index { |value, index| add_arg(value, index) }
  data.freeze
end

Private Instance Methods

add_arg(value, index) click to toggle source
# File lib/eac_ruby_utils/arguments_consumer.rb, line 40
def add_arg(value, index)
  arg = ::OpenStruct.new(value: value, index: index)
  if arg.value.is_a?(::Hash)
    add_hash_arg(arg)
  else
    add_positional_arg(arg)
  end
end
add_hash_arg(arg) click to toggle source
# File lib/eac_ruby_utils/arguments_consumer.rb, line 49
def add_hash_arg(arg)
  check_no_more_arguments(arg)
  data.merge!(arg.value)
  @options_found = true
end
add_positional_arg(arg) click to toggle source
# File lib/eac_ruby_utils/arguments_consumer.rb, line 55
def add_positional_arg(arg)
  check_no_more_arguments(arg)
  invalid_argument arg, 'No more valid positional' if
    arg.index >= arguments_consumer.positional.count
  data[arguments_consumer.positional[arg.index]] = arg.value
end
check_no_more_arguments(arg) click to toggle source
# File lib/eac_ruby_utils/arguments_consumer.rb, line 62
def check_no_more_arguments(arg)
  return unless @options_found

  invalid_argument arg, 'Hash already found - no more positional allowed'
end
invalid_argument(arg, message) click to toggle source
# File lib/eac_ruby_utils/arguments_consumer.rb, line 68
def invalid_argument(arg, message)
  raise InvalidArgumentError.new(self, arg, message)
end