Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
Gemfile
Guardfile
LICENSE
README.md
Rakefile
argumentative.gemspec

README.md

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}'
Something went wrong with that request. Please try again.