Kaffeine uses a plugin style approach, with a core/standard stack (order is important)
This is an example taken from production code. It show most of Kaffeine's features:
Edge::add = (nick, name, complete) { @client.select 15 user = User.find! {id: nick} puzzle = Puzzle.find! {name: name} err, data = client.set! "u:#{user}:p:#{puzzle}" complete() }
Allows function calls with callbacks to be unwrapped via a ! postfix. E.g:
fish = $.get!('/fish') $("stomach").append(fish)
Provide the @ alias for 'this'. It is also linked to the parent 'this' in the case of a function defined via 'async!'. E.g:
@legs = legs @color = color
provides support for omitting the var keyword: the variables will be automagically defined in the closest relevant closure. E.g.
x = 1 y = 2
Provide optional brackets for function calls. E.g.
remove eggs.shell mix eggs, milk
Allow multiline strings:
html = " <body> <h1>SOY SAUCE</h1> </body> "
This would maintain the new lines --- but they can be suppressed with the \ character
Allows an 'of' operator for looping through arrays:
for(x of [7,3,4]) sum += x // sum == 14
Allows allows an optional second parameter to refer to the key or value:
for(x, i of [7,3,4]) sum += i // sum == 3
provides ruby style string interpolation via #{}
letter = "Dear #{name}, I am writing to you to inform you of #{purpose} Kind Regards #{sender} "
getName = { @name }
provides the # shortcut for referring to the first argument in a function. Additionally, #n refers to the nth argument (n >= 0). Useful for terse function definitions, e.g:
square = { #*# }
the function keyword can be optionally omitted, along with empty argument lists.
ok = (timeout) { sendNote() setTimeout { run() }, timeout return true }
Note {} could be ambiguously a function or an object, so it's defined to be an function, to express an empty function, use one of the following :
{;} {null} function() { }
Provides an alternative calling method than can be used for easily chaining. Useful for ruby style enumeration chaining without using prototypes
| = require "pipe_utils" // we can specify the module to use with pipe People | map { #.name } | detect { #.length > 3 }