CreamerScript::Sweeteners

Sweeteners are the components that take CreamerScript code and transforms it into Coffeescript.

Overview

In the end, CreamerScript is just a syntactic sugar framework on top of Coffeescript. Sweeteners are the work horses of the framework; they are responsible for first parsing out any code that looks like something they want to transform and then subsequently transforming it.

Sweetener Interface

Sweeteners need to implement a specific interface in order to work with CreamerScript.

pattern Required

Must match the simplest form of the thing it cares about. Don't try to match recursive things like arrays inside of an array. Just match an array with no brackets inside it. CreamerScript will works its way out hitting all levels of nesting.

type Optional

Default: class name, lowercased and underscored.

A name for the Sweetener. This is used to generate tokens and then later to determine which Sweetener should handle transforming which chunks of code.

substitute Optional

Default: Replaces the entire matched pattern with a token.

You should override this if you need to do something special with the matched code like only replace a portion of it.

to_coffee Optional

Default: The original code that was replaced.

This is what turns CreamerScript into Coffeescript. When to_coffee is called, you have access to a source instance variable that contains the original code that was replaced. You should transform source into Coffeescript.

source may contain other tokens that have not been transformed yet. That's fine; treat them like you would a variable or any other entity.

Writing Sweeteners

Implement at least pattern. Usually you'll need to_coffee as well. To tell CreamerScript to use your Sweetener, just register it.

Creamerscript::Sweeteners.register(MyCustomSweetener)

Only one Sweetener will be used, per type. It's generally a good idea to use a somewhat broad type, like :method_definition or :string.

Default Sweeteners

require "creamerscript/sweeteners/default"

1. Smalltalk/Obj-C inspired method invocations (for methods defined in CreamerScript):

(this say:words to:person)
(this start:)

2. Smalltalk/Obj-C inspired method invocations (for methods defined in Coffeescript/JavaScript)

($ get: "/people", success, "json")

3. Expression based property invocations:

(person name)

4. Smalltalk inspired method definitions (with def thrown in for good measure)

def send_async:request on_queue:queue with_options:options
  ...