Coder Social home page Coder Social logo

eloquent's Introduction

Eloquent Travis-CI.org Build Status Coveralls.io Coverage Rating

Easily build up chaining structures

Example

var eloquent = require('eloquent');

var structure = {
	_constructor: function () { this.flag = false; },

	signal: {
		_getter: function () { this.flag = true; }
	},

	clear: {
		_getter: function () { this.flag = false; }
	},

	util: {
		signalIf: {
			_method: function (cond) {
				if (cond) {
					this.flag = true;
				}
			}
		}
	},

	hasFlag: {
		_returns: true, /* the method should not be wrapped */
		_method: function () {
			return !!this.flag;
		}
	},

	status: {
		_returns: true, /* the getter should not be wrapped */
		_getter: function () {
			return !!this.flag;
		}
	},

	emit: {
		_method: function () {
			if (this.flag) {
				console.log('Signal!');
			}
		}
	}
};

var Signaler = eloquent(structure);

Signaler().emit().signal.emit() // emits
	.clear.emit()
	.signal.clear.emit()
	.util.signalIf(5 > 1).emit(); // emits
// The above statement logs 'Signal!' twice.

console.log(Signaler().signal.hasFlag()); //-> true
console.log(Signaler().util.signalIf(true).status); //-> true

To-Do

Some things that still need to be done

  • DRYing out the implementation
  • Review/revise the underscore-prefix API (as suggested by Sindre Sorhus)
  • Add better ability to mix dynamic properties with non-dynamic properties
  • Performance increases (static prototype buildup at creation of Eloquent structure, etc.)

License

Licensed under the MIT License. You can find a copy of it in LICENSE.

eloquent's People

Contributors

qix- avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

eloquent's Issues

_alias: []

Allow for getters/methods to have aliases.

var structure = {
  util:
    _alias: ['utility'],  // or non-array, for single member
    /* . . . */
};

/*
     structure.util.xyz
     structure.utility.xyz
*/

_dynamic: true

It'd be nice if we could specify that a getter or method's return value is actually a new chain structure to use. That means we can query an object at call-time and make a completely new structure out of it, instead of having to know about it at the structure definition.

getLabels = -> ['foobar']

structure =
  lookup:
    _dynamic: yes
    _method: -> return "#{(getLabels())[0]}": _getter:-> console.log 'woo!'

Dynamic = eloquent structure

Dynamic().lookup().foobar #-> woo!

Allow both _getter and _method

With the implicit need to correct whatever the _getter does, it shouldn't be a restriction to have both _getter and _method properties.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.