Skip to content

dillonkearns/argumentative

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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}'

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages