Class: Bogo::Stack::Action::Arguments

Inherits:
Object
  • Object
show all
Defined in:
lib/bogo/stack.rb

Overview

Arguments to pass to action when called

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list: [], named: {}) ⇒ Arguments

Returns a new instance of Arguments.

Raises:

  • (TypeError)


323
324
325
326
327
328
329
330
331
332
# File 'lib/bogo/stack.rb', line 323

def initialize(list: [], named: {})
  list = [] if list.nil?
  named = {} if named.nil?
  raise TypeError, "Expected Array but received #{list.class.name}" if
    !list.is_a?(Array)
  raise TypeError, "Expecting Hash but received #{named.class.name}" if
    !named.is_a?(Hash)
  @list = list
  @named = Hash[named.map{ |k,v| [k.to_sym, v] }]
end

Instance Attribute Details

#listArray<Object> (readonly)

Returns list of arguments.

Returns:

  • (Array<Object>)

    list of arguments



319
320
321
# File 'lib/bogo/stack.rb', line 319

def list
  @list
end

#namedHash<Symbol,Object> (readonly)

Returns named arguments.

Returns:

  • (Hash<Symbol,Object>)

    named arguments



321
322
323
# File 'lib/bogo/stack.rb', line 321

def named
  @named
end

Class Method Details

.load(callable:, arguments:) ⇒ Arguments

Generate a new Arguments instance when given an argument list and the method they will be provided to

Parameters:

  • callable (Method, Proc)

    method to call with arguments

  • arguments (Array)

    arguments to call method

Returns:



340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/bogo/stack.rb', line 340

def self.load(callable:, arguments:)
  arguments = arguments.dup
  nargs = {}
  # check if we have any named parameters
  if callable.parameters.any?{ |p| [:key, :keyreq].include?(p.first) } && arguments.last.is_a?(Hash)
    p_keys = callable.parameters.map{ |p| p.last if [:key, :keyreq].include?(p.first) }
    e_keys = arguments.last.keys
    if (e_keys - p_keys).empty? || (e_keys.map(&:to_sym) - p_keys).empty?
      nargs = arguments.pop
    end
  end
  self.new(list: arguments, named: nargs)
end

Instance Method Details

#validate!(callable) ⇒ Object

Validate defined arguments can be properly applied to the given callable

Parameters:

  • callable (Proc, Object)

    Instance that responds to #call or Proc

Raises:



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/bogo/stack.rb', line 358

def validate!(callable)
  params = callable.is_a?(Proc) ? callable.parameters :
    callable.method(:call).parameters
  l = list.dup
  n = named.dup
  params.each do |param|
    type, name = param
    case type
    when :key
      n.delete(name)
    when :keyreq
      if !n.key?(name)
        raise Error::InvalidArgumentsError,
          "Missing named argument `#{name}' for action"
      end
      n.delete(name)
    when :keyrest
      n.clear
    when :rest
      l.clear
    when :req
      if l.size < 1
        raise Error::InvalidArgumentsError,
          "Missing required argument `#{name}' for action"
      end
      l.shift
    when :opt
      l.shift
    end
  end
  raise Error::InvalidArgumentsError,
    "Too many arguments provided to action" if !l.empty?
  if !n.empty?
    keys = n.keys.map { |k| "#{k}'"}.join(", `")
    raise Error::InvalidArgumentsError,
      "Unknown named arguments provided to action `#{keys}'"
  end
  nil
end