Last active
December 24, 2015 22:29
-
-
Save leejarvis/6873452 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd very much enjoy seeing your implementation of WithArgs :) I started doing something similar to auto-document public-facing API methods.