Skip to content

Commit

Permalink
feat: basic snake addon implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 20, 2018
1 parent 5ddd5bb commit 04965d2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion addon/atoms.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var atoms = {
var atoms = exports.atoms = {
d: 'display',

mar: 'margin',
Expand Down
75 changes: 75 additions & 0 deletions addon/snake.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

var atoms = require('./atoms').atoms;

var createSnake = function (renderer, rules) {
rules = rules || {};

var defaultRules = renderer.assign({}, atoms, {
bgWhite: function () {
defaultRules.bg.call(this, '#fff');
},

bgBlack: function () {
defaultRules.bg.call(this, '#000');
},
});

rules = renderer.assign({}, defaultRules, rules);

var snake = {
start: function () {
var instance = Object.create(snake);

instance.obj = {};
instance.toString = function () {
return renderer.cache(instance.obj);
};

return instance;
}
};

var onRule = function (name) {
var rule = rules[name];

if (typeof rule === 'function') {
if (!rule.length) {
Object.defineProperty(snake, name, {
get: function () {
rule.call(this.obj);
return this;
}
});
} else {
snake[name] = function () {
rule.apply(this.obj, arguments);
return this;
};
}
} else {
snake[name] = function (value) {
this.obj['' + rule] = value;
return this;
};
}
};

for (var name in rules) onRule(name);

return snake;
};

exports.addon = function (renderer) {
if (process.env.NODE_ENV !== 'production') {
require('./__dev__/warnOnMissingDependencies')('sheet', renderer, ['cache']);
}

var snake = createSnake(renderer);

Object.defineProperty(renderer, 's', {
get: function () {
return snake.start();
}
});
};

0 comments on commit 04965d2

Please sign in to comment.