Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

keyword dispatch/doesNotUnderstand in coffeescript

View gist:2887304
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#this is absolutely just for fun
 
keyWordDispatcher = (obj, msg) ->
selectorName = ""
args = []
for k, v of msg
selectorName += k + "_"
args.push v
 
if obj[selectorName]?
return obj[selectorName].apply obj, args
 
if obj.doesNotUnderstand?
return obj.doesNotUnderstand selectorName, args
 
throw new Exception "can not dispatch #{selectorName}"
 
keyWordDispatcher.make = (obj) -> (msg) -> keyWordDispatcher obj, msg
class Person
@new: -> keyWordDispatcher.make new @
say_to_: (aPhrase, aPerson) ->
console.log "called for #{aPhrase} and #{aPerson}"
doesNotUnderstand: (selector, args) ->
console.log "don't grok #{selector}.. do magic things. args:", args
 
from_to_do_: (start, end, block) ->
block i for i in [start..end]
guy = Person.new()
 
console.log guy
guy
say: 'hello'
to: 'walter'
guy
doSomething: 'crazy'
with: 'stuff you do not comprehend'
guy
from: 1
to: 10
do: (i) ->
console.log i * 2
 
num = keyWordDispatcher.make
str = keyWordDispatcher.make
Number::to_do_ = (limit, block) -> block i for i in [@..limit]
String::charAt_ = (i) -> @charAt i
(num 1)
to: 6
do: (i) -> console.log " ha ha #{i}"
 
(str "cat") charAt: 2
‚Äč
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.