My favorites | Sign in
Project Logo
                
Search
for
Updated Apr 12, 2008 by malte.ubl
BuildingARole  
Building a Role

Synopsis

Role("Comparable", {
    requires: "compare",
    does: Eq,
    
    methods: {
        equalTo:     function (other) { return this.compare(other) == 0 },
        greaterThan: function (other) { return this.compare(other) == 1 },
        lessThan:    function (other) { return this.compare(other) == -1 },
        
        greaterThanOrEqualTo: function (other) {
            return this.greaterThan(other) || this.equalTo(other)
        },
        
        lessThanOrEqualTo: function (other) {
            return this.lessThan(other) || this.equalTo(other)
        }
    }
})

// Using the role
Class("Currency", {
    does: Comparable,
    ... class declaration ...
})

Role(name, properties)

Roles are units of reusable behavior. You can also think of them as Java-interfaces on steroids. Classes and other roles declare that they implement a role using the does-keyword. When something does a role, it receives all the methods implemented by the class. If the implementing class already has a method of the same name, it will not be overidden.

For more info see CookbookRecipe6.

requires: methodName or Array of methodNames

Using the requires-keyword you declare that classes and roles that implement this role, need to provide methods named methodName. By passing an array of methodNames to the keyword you can require multiple method names.

requires: ["compare", "asNumber"]

methods

The declared methods will be exported to all classes implementing this role.

Caveats


Sign in to add a comment
Hosted by Google Code