Flulidify is a way to easily make fluent interfaces in coffeescript where the methods can be called in any order.
Via npm: place
flulidify in the
dependencies your package.json or run:
npm install flulidify
dist/flulidify.js. There are no dependencies. Supports both browser (
flulidify global) or node
flulidify = require('flulidify')).
Say you wanted a fluent interface that looks like this:
You also don't care which order the methods are called:
You can make this work in coffeescript:
=# Your code here=::actualFunctionpredicatestreamfunc::actualFunctionpredicatestreamfunc
However, this gets tedious, especially with more methods. With Flulidify it's easy:
modify = flulidify 'inStream''using'# Your code here
More in depth, made up example:
Would be created using:
foo = flulidify 'bar''baz'onetwothreebar: ayybeeceebaz: exwhyzee -># Your code here
Flulidify takes a number of method names and a callback. When all the methods have been called, the callback is executed with the initial arguments being the arguments of the first call, followed by a hash mapping method names to arguments they were called with. We use coffeescript's nifty destructuring syntax to achieve this.
You can share partially applied fluent apis safely. Lets use this (contrived) builder example:
:employeeBuilder = flulidify 'name''email''age'name: nameage: agesalary: salary ->return nameagesalary
We can now create a template employee with a fixed salary that we can assign names and ages to:
tenXDev = employeeBuildersalary200000dave = tenXDevname'dave'age27john = tenXDevage29name'john'
Look inside the
Gulpfile for the various tasks. To get started with autocompilation-and-tests-on-save run:
npm install --save-dev ./gulp watch
Joe Lea for the original coffeescript fluent interface concept and bouncing off ideas for automating the pattern.