Skip to content

Instantly share code, notes, and snippets.

@michaelstephendavies
Created June 3, 2014 09:38
Show Gist options
  • Save michaelstephendavies/983790a7ecd49a29981f to your computer and use it in GitHub Desktop.
Save michaelstephendavies/983790a7ecd49a29981f to your computer and use it in GitHub Desktop.
meteor.sjs
// USAGE:
// define a method. Can optionally add a Match pattern to each argument.
method add(Number arg1, Number arg2, uncheckedArg) {
return arg1 + arg2;
}
/*
Meteor.methods({
add: function (arg1, arg2, uncheckedArg) {
check(arg1, Number);
check(arg2, Number);
return arg1 + arg2;
}
});
*/
// define a publication. Can optionally add a Match pattern to each argument.
publication things(String str, unchecked) {
return Things.find({owner: this.userId, something: str});
}
/*
Meteor.publish('things', function (str, unchecked) {
check(str, String);
return Things.find({owner: this.userId, something: str});
});
*/
// define template helpers and events
template thing {
rendered() {
console.log("was rendered");
}
addIncrement(x) {
return x + this.increment;
}
blahOfSomething() {
return blah(this.something);
}
on "click .thing" {
console.log("clicked the thing");
}
on "keydown input" (evt) {
Session.set("blah", $(evt.currentTarget).val());
}
}
/*
Template.thing.rendered = function () {
console.log('was rendered');
};
Template.thing.addIncrement = function (x) {
return x + this.increment;
};
Template.thing.blahTheSomething = function () {
return blah(this.something);
};
Template.thing.events({
'click .thing': function () {
console.log('clicked the thing');
}
});
Template.thing.events({
'keydown input': function (evt) {
Session.set('blah', $(evt.currentTarget).val());
}
});
*/
// THE MACROS
// used by method and publication
macroclass argType {
pattern {$type:expr $name:ident}
pattern {$name:ident} where ($type = #{}) // why does this work?
}
macro method {
rule {$name:ident ($args:argType (,) ...) {$code ...}} => {
Meteor.methods({
$name: function($args$name (,) ...) {
$(check($args$name, $args$type);) ...
$code ...
}
});
}
}
// used by publication
macro __stringifyIdent {
case {_ $x:ident} => {
var pattern = #{$x};
var tokenString = pattern[0].token.value.toString();
var stringValue = makeValue(tokenString, #{$here});
return withSyntax($val = [stringValue]) {
return #{$val};
}
}
}
macro publication {
rule {$name:ident ($args:argType (,) ...) {$code ...}} => {
Meteor.publish(__stringifyIdent $name, function ($args$name (,) ...) {
$(check($args$name, $args$type);) ...
$code ...
});
}
}
macro template {
rule {$name {}} => {}
rule {
$name {
$helperName($args:ident (,) ...) {$code ...}
$rest ...
}
} => {
Template.$name.$helperName = function ($args (,) ...) {
$code ...
};
template $name {$rest ...}
}
rule {
$name {
on $evt:lit ($args:ident (,) ...) {$code ...}
$rest ...
}
} => {
Template.$name.events({
$evt: function ($args (,) ...) {$code ...}
});
template $name {$rest ...}
}
rule {
$name {
on $evt:lit {$code ...}
$rest ...
}
} => {
Template.$name.events({
$evt: function () {$code ...}
});
template $name {$rest ...}
}
}
@joaomcarlos
Copy link

What atmosphere package are you using to compile those macros ?

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