Skip to content

Instantly share code, notes, and snippets.

@leejarvis
Last active December 24, 2015 22:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leejarvis/6873452 to your computer and use it in GitHub Desktop.
Save leejarvis/6873452 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "with_args" # TBA
class User
with_args String,
def name=(name)
@name = name
end
with_args Hash,
def to_json(options = {})
@attributes.to_json(options)
end
with_args "Send an invite to this user", String,
def invite!(email)
UserMailer.invite(self, email).deliver
end
with_args "Check if this user has the same name as another", :name,
def eql?(user)
name == user.name
end
with_args "Do something with", String, "and optional", Hash,
def something(string, options = {})
end
end
u = User.new
u.name = 1 # WithArgs::TypeError -- Expected a String
u.eql?(Object.new) # WithArgs::TypeError -- Object does not respond to `name'
# Annocations for free
User.annotations #=>
{
"name=" => "name=(String)",
"to_json" => "to_json(Hash)",
"invite!" => "Send an invite to this user String",
"eql?" => "Check if this user has the same name as another",
"something" => "Do something with String and optional Hash"
}
@kotay
Copy link

kotay commented Oct 8, 2013

I'd very much enjoy seeing your implementation of WithArgs :) I started doing something similar to auto-document public-facing API methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment