Class: Bogo::Stack::Action::Arguments
- Inherits:
-
Object
- Object
- Bogo::Stack::Action::Arguments
- Defined in:
- lib/bogo/stack.rb
Overview
Arguments to pass to action when called
Instance Attribute Summary collapse
-
#list ⇒ Array<Object>
readonly
List of arguments.
-
#named ⇒ Hash<Symbol,Object>
readonly
Named arguments.
Class Method Summary collapse
-
.load(callable:, arguments:) ⇒ Arguments
Generate a new Arguments instance when given an argument list and the method they will be provided to.
Instance Method Summary collapse
-
#initialize(list: [], named: {}) ⇒ Arguments
constructor
A new instance of Arguments.
-
#validate!(callable) ⇒ Object
Validate defined arguments can be properly applied to the given callable.
Constructor Details
#initialize(list: [], named: {}) ⇒ Arguments
Returns a new instance of Arguments.
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
#list ⇒ Array<Object> (readonly)
Returns list of arguments.
319 320 321 |
# File 'lib/bogo/stack.rb', line 319 def list @list end |
#named ⇒ Hash<Symbol,Object> (readonly)
Returns 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
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
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 |