0.0
No commit activity in last 3 years
No release in over 3 years
Flexible argument processing in a readable, declarative style!
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

Argumentative

Build Status Coverage Status Gem Version Dependency Status Code Climate

Flexible argument processing in a readable, declarative style!

Installation

Just add gem 'argumentative' to your Gemfile and require 'argumentative'.

Usage

require 'argumentative'

def flexible_args_method(*args)
  Argumentative.process(args) do |a|
    a.type(String) do |string|
      "I was called with a string (#{string})"
    end

    a.type(Numeric) do |number|
      "I was called with a number (#{number})"
    end

    a.type(String.*, Hash) do |*strings, options|
      "I got strings #{strings.inspect} and options #{options.inspect}"
    end
  end
end

flexible_args_method('string')                # => "I was called with a string (string)"
flexible_args_method(123.5)                   # => "I was called with a number (123.5)"
flexible_args_method('one', 'two', three: 3)  # => 'I got strings ["one", "two"] and options {:three=>3}'
require 'argumentative'

def flexible_args_method(*args)
  Argumentative::Processor.new(args).
    type(String) { |string| "I was called with a string (#{string})" }.
    type(Numeric) { |number| "I was called with a number (#{number})" }.
    type(String.*, Hash) { |*strings, options| "I got strings #{strings.inspect} and options #{options.inspect}" }.
    process
end

flexible_args_method('string')                # => "I was called with a string (string)"
flexible_args_method(123.5)                   # => "I was called with a number (123.5)"
flexible_args_method('one', 'two', three: 3)  # => 'I got strings ["one", "two"] and options {:three=>3}'