Another bone-headed idea implemented in Ruby
Go to file
R. Tyler Croy 1d7c5b2fe6 Implement support for typedeaf'd class methods via #class_*
Fixes #6
2014-10-27 19:56:58 -07:00
benchmarks Add a benchmark and results for method calls with missing arguments 2014-10-26 17:22:56 -07:00
lib Implement support for typedeaf'd class methods via #class_* 2014-10-27 19:56:58 -07:00
spec Implement support for typedeaf'd class methods via #class_* 2014-10-27 19:56:58 -07:00
.gitignore More ignores 2014-10-23 18:22:25 -07:00
.rspec Initial cut of the "define" API with blocks 2014-10-24 01:00:14 -07:00
.travis.yml Enable Travis 2014-10-25 14:33:54 -07:00
Gemfile Start adding some basic benchmarks 2014-10-26 15:09:20 -07:00
LICENSE.txt
README.md Add support for blocks with some caveats listed in the README 2014-10-25 16:26:32 -07:00
Rakefile Initial cut of the "define" API with blocks 2014-10-24 01:00:14 -07:00
typedeaf.gemspec Implement support for declaring promise/future methods natively 2014-10-25 16:43:11 -07:00

README.md

Typedeaf

Build Status

Typedeaf is a gem to help add some type-checking to method declarations in Ruby

RDoc

Usage

Writing your Typedeaf'd code

require 'typedeaf'

class Logger
  include Typedeaf

  # Log an error
  #
  # @param [String] messaage
  define :error, message: String do
    # Just delegate to the #log method nad pass
    # the level of error
    self.log(message, :error)
  end

  # Log a simple message to STDOUT
  #
  # @param [String] message The log message to log 
  # @param [Symbol] level The level at which to log the
  #     message, defaults to :info
  define :log, message: String, level: default(:info, Symbol) do
    puts "[#{level}] #{message}"
  end
end

Calling Typedeaf'd code

[1] pry(main)> require './logger'
=> true
[2] pry(main)> l = Logger.new
=> #<Logger:0x00000803c616b8>
[3] pry(main)> l.log 'test 1, 2, 3'
[info] test 1, 2, 3
=> nil
[4] pry(main)> l.log 'this is SUPER SERIOUS', :critical
[critical] this is SUPER SERIOUS
=> nil
[5] pry(main)> l.error(5) # wrong type!
Typedeaf::InvalidTypeException: Expected `message` to be a kind of String but was Fixnum
from /usr/home/tyler/source/github/ruby/typedeaf/lib/typedeaf.rb:58:in `type_validation!'
[6] pry(main)> l.log("This doesn't use the right type either", 1)
Typedeaf::InvalidTypeException: Expected `level` to be a kind of [Symbol] but was Fixnum
from /usr/home/tyler/source/github/ruby/typedeaf/lib/typedeaf.rb:58:in `type_validation!'
[7] pry(main)> 

Passing blocks to Typedeaf code

require 'typedeaf'

class Logger
  include Typedeaf

  define :log, message: String, block: Proc do
    puts "Loggin: #{message}"
    block.call
  end
end

Logger.new.log('hello world') do
  puts 'called back!'
end

Caveats

Here's a list of caveats, or things that don't quite work, with Typedeaf:

  • Typedeaf methods cannot use return or break, since they're technically Ruby blocks and must follow the rules defined for block behaviors
  • yield will not properly work, if you want to pass blocks into your Typedeafed method, see the above usage example

Installation

Add this line to your application's Gemfile:

gem 'typedeaf'

And then execute:

$ bundle

Or install it yourself as:

$ gem install typedeaf