Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use AMD to define prereqs

  • Loading branch information...
commit bd55fa48f02427def85fbf0ba8303608d6ee26d3 1 parent 5895a5b
@autarch authored
Showing with 18,962 additions and 1,041 deletions.
  1. +34 −29 lib/Brocket.coffee
  2. +19 −17 lib/Brocket/Base.coffee
  3. +67 −61 lib/Brocket/Helpers.coffee
  4. +153 −151 lib/Brocket/Meta/Attribute.coffee
  5. +20 −19 lib/Brocket/Meta/Cache.coffee
  6. +167 −166 lib/Brocket/Meta/Class.coffee
  7. +46 −45 lib/Brocket/Meta/Method.coffee
  8. +45 −44 lib/Brocket/Meta/Mixin/AttributeCore.coffee
  9. +29 −28 lib/Brocket/Meta/Mixin/HasAttributes.coffee
  10. +32 −31 lib/Brocket/Meta/Mixin/HasMethods.coffee
  11. +29 −28 lib/Brocket/Meta/Mixin/HasRoles.coffee
  12. +135 −134 lib/Brocket/Meta/Role.coffee
  13. +15 −14 lib/Brocket/Meta/Role/Application.coffee
  14. +69 −68 lib/Brocket/Meta/Role/Application/RoleSummation.coffee
  15. +60 −59 lib/Brocket/Meta/Role/Application/ToClass.coffee
  16. +40 −39 lib/Brocket/Meta/Role/Application/ToRole.coffee
  17. +41 −40 lib/Brocket/Meta/Role/Attribute.coffee
  18. +26 −25 lib/Brocket/Meta/Role/Composite.coffee
  19. +12 −11 lib/Brocket/Meta/Role/ConflictingMethod.coffee
  20. +10 −9 lib/Brocket/Meta/Role/RequiredMethod.coffee
  21. +28 −23 lib/Brocket/Role.coffee
  22. +4 −0 lib/Brocket/Types.coffee
  23. +1 −0  node_modules/.bin/r.js
  24. +2 −0  node_modules/amdefine/.npmignore
  25. +58 −0 node_modules/amdefine/LICENSE
  26. +200 −0 node_modules/amdefine/amdefine.js
  27. +41 −0 node_modules/amdefine/package.json
  28. +9 −0 node_modules/requirejs/README.md
  29. +15,484 −0 node_modules/requirejs/bin/r.js
  30. +44 −0 node_modules/requirejs/package.json
  31. +2,041 −0 node_modules/requirejs/require.js
  32. +1 −0  package.json
View
63 lib/Brocket.coffee
@@ -1,42 +1,47 @@
-Base = require "./Brocket/Base"
-Class = require "./Brocket/Meta/Class"
-Helpers = require "./Brocket/Helpers"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-_has = (meta, name, attr) ->
- clone = name: name
- for own key, val of attr
- clone[key] = val
+define (require) ->
+ Base = require "./Brocket/Base"
+ Class = require "./Brocket/Meta/Class"
+ Helpers = require "./Brocket/Helpers"
+ util = require "util"
- meta.addAttribute clone
+ _has = (meta, name, attr) ->
+ clone = name: name
+ for own key, val of attr
+ clone[key] = val
-_method = (meta, name, body) ->
- meta.addMethod name: name, body: body, source: meta
+ meta.addAttribute clone
-_subclasses = (meta, supers) ->
- meta.setSuperclasses supers
+ _method = (meta, name, body) ->
+ meta.addMethod name: name, body: body, source: meta
-_with = (meta, roles) ->
- Helpers.applyRoles meta, roles
+ _subclasses = (meta, supers) ->
+ meta.setSuperclasses supers
-_consumes = (meta, name, options) ->
+ _with = (meta, roles) ->
+ Helpers.applyRoles meta, roles
-module.exports.makeClass = (name, definition) ->
- metaclass = new Class name: name
+ _consumes = (meta, name, options) ->
- metaclass.setSuperclasses(Base)
+ makeClass = (name, definition) ->
+ metaclass = new Class name: name
- klass = metaclass.class()
+ metaclass.setSuperclasses(Base)
- B = {}
- B.has = (name, attr) -> _has metaclass, name, attr
- B.method = (name, body) -> _method metaclass, name, body
- B.subclasses = (supers) -> _subclasses metaclass, supers
- B.with = (roles...) -> _with metaclass, roles
- B.consumes = (role, options) -> _consumes metaclass, role, options
+ klass = metaclass.class()
- definition ?= -> return
+ B = {}
+ B.has = (name, attr) -> _has metaclass, name, attr
+ B.method = (name, body) -> _method metaclass, name, body
+ B.subclasses = (supers) -> _subclasses metaclass, supers
+ B.with = (roles...) -> _with metaclass, roles
+ B.consumes = (role, options) -> _consumes metaclass, role, options
- definition.call @, B
+ definition ?= -> return
- return klass
+ definition.call @, B
+
+ return klass
+
+ return { makeClass: makeClass };
View
36 lib/Brocket/Base.coffee
@@ -1,24 +1,26 @@
-Class = require "./Meta/Class"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-class Base
- constructor: ->
- throw new Error "Cannot construct a Brocket/Base object"
+define (require) ->
+ Class = require "./Meta/Class"
+ util = require "util"
- BUILDARGS: (params) ->
- return params ? {}
+ class Base
+ constructor: ->
+ throw new Error "Cannot construct a Brocket/Base object"
- BUILDALL: (params) ->
- for meta in @meta().selfAndParents().reverse()
- build = meta.methodNamed "BUILD"
- build.body().call @, params if build?
+ BUILDARGS: (params) ->
+ return params ? {}
- return
+ BUILDALL: (params) ->
+ for meta in @meta().selfAndParents().reverse()
+ build = meta.methodNamed "BUILD"
+ build.body().call @, params if build?
- DOES: (role) ->
- return @meta().doesRole role
+ return
- _meta = new Class { name: "Brocket.Base", _class: @ }
- @meta: -> _meta
+ DOES: (role) ->
+ return @meta().doesRole role
+
+ _meta = new Class { name: "Brocket.Base", _class: @ }
+ @meta: -> _meta
-module.exports = Base
View
128 lib/Brocket/Helpers.coffee
@@ -1,84 +1,90 @@
-_ = require "underscore"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-Class = null
-Role = null
+define (require) ->
+ _ = require "underscore"
+ util = require "util"
+
+ Class = null
+ Role = null
+
+ e = {};
+ e.arrayToObject = (array) ->
+ if typeof array == "string"
+ obj = {}
+ obj[array] = true
+ return obj
-module.exports.arrayToObject = (array) ->
- if typeof array == "string"
obj = {}
- obj[array] = true
- return obj
+ for elt in array
+ obj[elt] = true
- obj = {}
- for elt in array
- obj[elt] = true
+ return obj
- return obj
+ e.className = (klass) ->
+ if matches = klass.toString().match( /function\s*(\w+)/ )
+ return matches[1]
+ else
+ return null
-module.exports.className = (klass) ->
- if matches = klass.toString().match( /function\s*(\w+)/ )
- return matches[1]
- else
- return null
+ e.applyRoles = (applyTo, roles...) ->
+ Role ?= require "./Meta/Role"
-module.exports.applyRoles = (applyTo, roles...) ->
- Role ?= require "./Meta/Role"
+ if roles[0] instanceof Array
+ roles = roles[0]
- if roles[0] instanceof Array
- roles = roles[0]
+ rolesWithArgs = e.optList roles, { lhs: Role }
- rolesWithArgs = module.exports.optList roles, { lhs: Role }
+ if rolesWithArgs.length == 2
+ role = rolesWithArgs[0]
+ args = rolesWithArgs[1]
- if rolesWithArgs.length == 2
- role = rolesWithArgs[0]
- args = rolesWithArgs[1]
+ role.apply applyTo, args
+ else
+ (Role.Combine rolesWithArgs).apply applyTo
- role.apply applyTo, args
- else
- (Role.Combine rolesWithArgs).apply applyTo
+ return
- return
+ e.optList = (list, args) ->
+ args ?= {}
-module.exports.optList = (list, args) ->
- args ?= {}
+ lhsTest =
+ if args.lhs?
+ (item) -> item instanceof args.lhs
+ else
+ (item) -> typeof item == "string"
- lhsTest =
- if args.lhs?
- (item) -> item instanceof args.lhs
- else
- (item) -> typeof item == "string"
+ pairs = []
+ for item in list
+ if lhsTest item
+ pairs.push [item]
+ else if item instanceof Object
+ pairs[ pairs.length - 1 ].push item
- pairs = []
- for item in list
- if lhsTest item
- pairs.push [item]
- else if item instanceof Object
- pairs[ pairs.length - 1 ].push item
+ retVal = []
+ for pair in pairs
+ pair[1] ?= {}
+ retVal = retVal.concat pair
- retVal = []
- for pair in pairs
- pair[1] ?= {}
- retVal = retVal.concat pair
+ return retVal
- return retVal
+ e.findMeta = (thing, classClass) ->
+ Class ?= require "./Meta/Class"
+ Role ?= require "./Meta/Role"
-module.exports.findMeta = (thing, classClass) ->
- Class ?= require "./Meta/Class"
- Role ?= require "./Meta/Role"
+ return thing if thing instanceof Class
+ return thing if thing instanceof Role
+ return thing.meta() if thing.meta?
- return thing if thing instanceof Class
- return thing if thing instanceof Role
- return thing.meta() if thing.meta?
+ unless typeof thing == "function"
+ throw new Error "Cannot find a metaclass for a #{thing}"
- unless typeof thing == "function"
- throw new Error "Cannot find a metaclass for a #{thing}"
+ # Allows callers to pass an alternate metaclass
+ classClass ?= Class
- # Allows callers to pass an alternate metaclass
- classClass ?= Class
+ unless classClass.newFromClass?
+ name = e.className classClass
+ throw new Error "The #{name} class does not have a newFromClass method"
- unless classClass.newFromClass?
- name = module.exports.className classClass
- throw new Error "The #{name} class does not have a newFromClass method"
+ return classClass.newFromClass thing
- return classClass.newFromClass thing
+ return e
View
304 lib/Brocket/Meta/Attribute.coffee
@@ -1,160 +1,162 @@
-_ = require "underscore"
-AttributeCore = require "./Mixin/AttributeCore"
-Method = require "./Method"
-util = require "util"
-
-class Attribute
- for own key of AttributeCore.prototype
- Attribute.prototype[key] = AttributeCore.prototype[key]
-
- constructor: (args) ->
- @_buildAttributeCore args
-
- @_setAccessorMethodProperties args
-
- @_setDefaultProperties args
-
- @_slotName = " __#{ @_name }__ "
-
- @_methodClass = args.methodClass ? Method
-
- @_methods = {}
-
- @_buildMethods()
-
- return
-
- _setAccessorMethodProperties: (args) ->
- if args.accessor
- @_accessor = args.accessor
- else if @_access == "rw" && ! args.reader? && ! args.writer
- @_accessor = @_name
-
- if !@_accessor?
- if args.reader?
- @_reader = args.reader
- else if @_access == "ro"
- @_reader = @_name
-
- @_writer = args.writer if args.writer?
-
- @_predicate = args.predicate ? null
- @_clearer = args.clearer ? null
-
- return
-
- _setDefaultProperties: (args) ->
- if Object.prototype.hasOwnProperty.call args, "default"
- def = args.default
- @_default = def
- @__defaultFunc = -> def
- else if Object.prototype.hasOwnProperty.call args, "builder"
- builder = args.builder
- @_builder = builder
- # XXX - need some sort of error handling
- @__defaultFunc = (instance) -> @[builder].call instance
-
- if @_lazy && !@__defaultFunc?
- throw new Error "You must provide a default or builder for a lazy attribute"
-
- return
-
- _buildMethods: () ->
- name = @name()
- slotName = @slotName()
- def = @_defaultFunc()
-
- methods = @_methodsObj()
- mclass = @methodClass()
-
- if @hasAccessor()
- if @accessor() instanceof Method
- methods[ @accessor().name() ] = @accessor()
- else
- body =
- if @isLazy()
- ->
- if arguments.length > 0
- this[slotName] = arguments[0]
-
- if ! Object.prototype.hasOwnProperty.call this, name
- this[slotName] = def.call this
- return this[slotName]
- else
- ->
- if arguments.length > 0
- this[slotName] = arguments[0]
-
- return this[slotName]
- methods[ @accessor() ] = new mclass name: @accessor(), body: body
-
- if @hasReader()
- if @reader() instanceof Method
- methods[ @reader().name() ] = @reader()
- else
- body =
- if @isLazy()
- ->
- if ! Object.prototype.hasOwnProperty.call this, name
- this[slotName] = def.call this
- return this[slotName]
- else
- ->
- return this[slotName]
- methods[ @reader() ] = new mclass name: @reader(), body: body
-
- if @hasWriter()
- if @writer() instanceof Method
- methods[ @writer().name() ] = @writer()
- else
- body = (value) ->
- this[slotName] = value
- methods[ @writer() ] = new mclass name: @writer(), body: body
-
- if @hasClearer()
- body = -> delete this[slotName]
- methods[ @clearer() ] = new mclass name: @clearer(), body: body
-
- if @hasPredicate()
- body = -> Object.prototype.hasOwnProperty.call this, slotName
- methods[ @predicate() ] = new mclass name: @predicate(), body: body
-
- return
-
- attachToClass: (metaclass) ->
- @_associatedClass = metaclass
- return;
-
- associatedClass: ->
- return @_associatedClass
-
- detachFromClass: (metaclass) ->
- delete @_associatedClass
- return;
-
- initializeInstanceSlot: (instance, params) ->
- name = @name();
-
- if params? && typeof params == "object" && Object.prototype.hasOwnProperty.call params, name
- instance[ @slotName() ] = params[name]
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
+
+define (require) ->
+ _ = require "underscore"
+ AttributeCore = require "./Mixin/AttributeCore"
+ Method = require "./Method"
+ util = require "util"
+
+ class Attribute
+ for own key of AttributeCore.prototype
+ Attribute.prototype[key] = AttributeCore.prototype[key]
+
+ constructor: (args) ->
+ @_buildAttributeCore args
+
+ @_setAccessorMethodProperties args
+
+ @_setDefaultProperties args
+
+ @_slotName = " __#{ @_name }__ "
+
+ @_methodClass = args.methodClass ? Method
+
+ @_methods = {}
+
+ @_buildMethods()
+
return
- return if @isLazy() || ! @_defaultFunc()
+ _setAccessorMethodProperties: (args) ->
+ if args.accessor
+ @_accessor = args.accessor
+ else if @_access == "rw" && ! args.reader? && ! args.writer
+ @_accessor = @_name
+
+ if !@_accessor?
+ if args.reader?
+ @_reader = args.reader
+ else if @_access == "ro"
+ @_reader = @_name
+
+ @_writer = args.writer if args.writer?
+
+ @_predicate = args.predicate ? null
+ @_clearer = args.clearer ? null
+
+ return
- instance[ @slotName() ] = @_defaultFunc().call instance
+ _setDefaultProperties: (args) ->
+ if Object.prototype.hasOwnProperty.call args, "default"
+ def = args.default
+ @_default = def
+ @__defaultFunc = -> def
+ else if Object.prototype.hasOwnProperty.call args, "builder"
+ builder = args.builder
+ @_builder = builder
+ # XXX - need some sort of error handling
+ @__defaultFunc = (instance) -> @[builder].call instance
- return
+ if @_lazy && !@__defaultFunc?
+ throw new Error "You must provide a default or builder for a lazy attribute"
+
+ return
+
+ _buildMethods: () ->
+ name = @name()
+ slotName = @slotName()
+ def = @_defaultFunc()
+
+ methods = @_methodsObj()
+ mclass = @methodClass()
+
+ if @hasAccessor()
+ if @accessor() instanceof Method
+ methods[ @accessor().name() ] = @accessor()
+ else
+ body =
+ if @isLazy()
+ ->
+ if arguments.length > 0
+ this[slotName] = arguments[0]
+
+ if ! Object.prototype.hasOwnProperty.call this, name
+ this[slotName] = def.call this
+ return this[slotName]
+ else
+ ->
+ if arguments.length > 0
+ this[slotName] = arguments[0]
+
+ return this[slotName]
+ methods[ @accessor() ] = new mclass name: @accessor(), body: body
+
+ if @hasReader()
+ if @reader() instanceof Method
+ methods[ @reader().name() ] = @reader()
+ else
+ body =
+ if @isLazy()
+ ->
+ if ! Object.prototype.hasOwnProperty.call this, name
+ this[slotName] = def.call this
+ return this[slotName]
+ else
+ ->
+ return this[slotName]
+ methods[ @reader() ] = new mclass name: @reader(), body: body
+
+ if @hasWriter()
+ if @writer() instanceof Method
+ methods[ @writer().name() ] = @writer()
+ else
+ body = (value) ->
+ this[slotName] = value
+ methods[ @writer() ] = new mclass name: @writer(), body: body
+
+ if @hasClearer()
+ body = -> delete this[slotName]
+ methods[ @clearer() ] = new mclass name: @clearer(), body: body
+
+ if @hasPredicate()
+ body = -> Object.prototype.hasOwnProperty.call this, slotName
+ methods[ @predicate() ] = new mclass name: @predicate(), body: body
+
+ return
+
+ attachToClass: (metaclass) ->
+ @_associatedClass = metaclass
+ return;
+
+ associatedClass: ->
+ return @_associatedClass
+
+ detachFromClass: (metaclass) ->
+ delete @_associatedClass
+ return;
+
+ initializeInstanceSlot: (instance, params) ->
+ name = @name();
+
+ if params? && typeof params == "object" && Object.prototype.hasOwnProperty.call params, name
+ instance[ @slotName() ] = params[name]
+ return
+
+ return if @isLazy() || ! @_defaultFunc()
+
+ instance[ @slotName() ] = @_defaultFunc().call instance
+
+ return
- slotName: ->
- @_slotName
+ slotName: ->
+ @_slotName
- methodClass: ->
- @_methodClass
+ methodClass: ->
+ @_methodClass
- _methodsObj: ->
- @_methods
+ _methodsObj: ->
+ @_methods
- methods: ->
- _.values @_methodsObj()
+ methods: ->
+ _.values @_methodsObj()
-module.exports = Attribute
View
39 lib/Brocket/Meta/Cache.coffee
@@ -1,27 +1,28 @@
-_ = require "underscore"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-class Cache
- _metaobjects = {}
+define (require) ->
+ _ = require "underscore"
+ util = require "util"
- @storeMetaObject = (meta) ->
- _metaobjects[ meta.name() ] = meta
- return
+ class Cache
+ _metaobjects = {}
- @getMetaObject = (name) ->
- return _metaobjects[name]
+ @storeMetaObject = (meta) ->
+ _metaobjects[ meta.name() ] = meta
+ return
- @metaObjectExists = (name) ->
- return _metaobjects[name]?
+ @getMetaObject = (name) ->
+ return _metaobjects[name]
- @removeMetaObject = (name) ->
- return _metaobjects[name]?
+ @metaObjectExists = (name) ->
+ return _metaobjects[name]?
- @allMetaObjects = ->
- return _.values _metaobjects
+ @removeMetaObject = (name) ->
+ return _metaobjects[name]?
- @_clearMetaObjects = ->
- _metaobjects = {}
- return
+ @allMetaObjects = ->
+ return _.values _metaobjects
-module.exports = Cache
+ @_clearMetaObjects = ->
+ _metaobjects = {}
+ return
View
333 lib/Brocket/Meta/Class.coffee
@@ -1,229 +1,230 @@
-_ = require "underscore"
-Attribute = require "./Attribute"
-Cache = require "./Cache"
-HasAttributes = require "./Mixin/HasAttributes"
-HasMethods = require "./Mixin/HasMethods"
-HasRoles = require "./Mixin/HasRoles"
-Helpers = require "../Helpers"
-Role = require "./Role"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-class Class
- for own key of HasAttributes.prototype
- Class.prototype[key] = HasAttributes.prototype[key]
+define (require) ->
+ _ = require "underscore"
+ Attribute = require "./Attribute"
+ Cache = require "./Cache"
+ HasAttributes = require "./Mixin/HasAttributes"
+ HasMethods = require "./Mixin/HasMethods"
+ HasRoles = require "./Mixin/HasRoles"
+ Helpers = require "../Helpers"
+ Role = require "./Role"
+ util = require "util"
- for own key of HasMethods.prototype
- Class.prototype[key] = HasMethods.prototype[key]
+ class Class
+ for own key of HasAttributes.prototype
+ Class.prototype[key] = HasAttributes.prototype[key]
- for own key of HasRoles.prototype
- Class.prototype[key] = HasRoles.prototype[key]
+ for own key of HasMethods.prototype
+ Class.prototype[key] = HasMethods.prototype[key]
- constructor: (args) ->
- @_name = args.name
- throw new Error "You must provide a name when constructing a class" unless @_name
+ for own key of HasRoles.prototype
+ Class.prototype[key] = HasRoles.prototype[key]
- args.cache = true unless args.cache? && ! args.cache
+ constructor: (args) ->
+ @_name = args.name
+ throw new Error "You must provide a name when constructing a class" unless @_name
- if args.cache && Cache.metaObjectExists args.name
- meta = Cache.getMetaObject args.name
- unless meta instanceof Class
- error = "Found an existing meta object named #{ args.name } which is not a Class object."
- if meta instanceof Role
- error += " You cannot create a Class and a Role with the same name."
- throw new Error error
+ args.cache = true unless args.cache? && ! args.cache
- return meta
+ if args.cache && Cache.metaObjectExists args.name
+ meta = Cache.getMetaObject args.name
+ unless meta instanceof Class
+ error = "Found an existing meta object named #{ args.name } which is not a Class object."
+ if meta instanceof Role
+ error += " You cannot create a Class and a Role with the same name."
+ throw new Error error
- @_buildMethodProperties args
- @_buildAttributeProperties args
- @_buildRoleProperties args
+ return meta
- @_superclasses = []
+ @_buildMethodProperties args
+ @_buildAttributeProperties args
+ @_buildRoleProperties args
- @_class = @_makeClass args._class
+ @_superclasses = []
- Cache.storeMetaObject @ if args.cache
+ @_class = @_makeClass args._class
- return
+ Cache.storeMetaObject @ if args.cache
- _makeClass: (klass) ->
- meta = @
+ return
- if !klass
- klass = ->
- args = [@].concat Array.prototype.slice.call arguments
- meta.constructInstance.apply meta, args
+ _makeClass: (klass) ->
+ meta = @
- klass.meta = => meta
- klass.prototype.meta = => meta
+ if !klass
+ klass = ->
+ args = [@].concat Array.prototype.slice.call arguments
+ meta.constructInstance.apply meta, args
- klass.prototype._super = ->
- error = new Error
+ klass.meta = => meta
+ klass.prototype.meta = => meta
- caller = meta._callerFromError error, "_super"
+ klass.prototype._super = ->
+ error = new Error
- ancestors = meta.linearizedInheritance()
- for supermeta in ancestors
- superclass = supermeta.class()
- if Object.prototype.hasOwnProperty.call superclass.prototype, caller
- return superclass.prototype[caller].apply @, Array.prototype.slice.call arguments
+ caller = meta._callerFromError error, "_super"
- name = (s) -> s.name()
- supernames = (name s for s in ancestors)
+ ancestors = meta.linearizedInheritance()
+ for supermeta in ancestors
+ superclass = supermeta.class()
+ if Object.prototype.hasOwnProperty.call superclass.prototype, caller
+ return superclass.prototype[caller].apply @, Array.prototype.slice.call arguments
- throw new Error "No #{caller} method found in any superclasses of #{ meta.name() } - superclasses are #{ supernames.join(', ') }"
+ name = (s) -> s.name()
+ supernames = (name s for s in ancestors)
- return klass
+ throw new Error "No #{caller} method found in any superclasses of #{ meta.name() } - superclasses are #{ supernames.join(', ') }"
- @newFromClass = (klass) ->
- cache = true
- name = Helpers.className klass
+ return klass
- unless name?
- name = "__Anon__"
- cache = false
+ @newFromClass = (klass) ->
+ cache = true
+ name = Helpers.className klass
- return new @ { name: name, _class: klass, cache: cache }
+ unless name?
+ name = "__Anon__"
+ cache = false
- setSuperclasses: (supers) ->
- supers = [supers] unless supers instanceof Array
+ return new @ { name: name, _class: klass, cache: cache }
- constructor = @constructor
- metaFor = (s) ->
- meta = Helpers.findMeta s, constructor
- unless meta instanceof Class
- throw new Error "Cannot have a superclass which is a #{ Helpers.className meta }"
- return meta
+ setSuperclasses: (supers) ->
+ supers = [supers] unless supers instanceof Array
- @_superclasses = (metaFor s for s in supers)
+ constructor = @constructor
+ metaFor = (s) ->
+ meta = Helpers.findMeta s, constructor
+ unless meta instanceof Class
+ throw new Error "Cannot have a superclass which is a #{ Helpers.className meta }"
+ return meta
- @_checkMetaclassCompatibility()
+ @_superclasses = (metaFor s for s in supers)
- for meta in @_superclasses
- for own name, method of meta._methodMap()
- continue if @hasMethod name
- continue unless method.isInheritable()
- @addMethod method.clone()
+ @_checkMetaclassCompatibility()
- return
+ for meta in @_superclasses
+ for own name, method of meta._methodMap()
+ continue if @hasMethod name
+ continue unless method.isInheritable()
+ @addMethod method.clone()
- _checkMetaclassCompatibility: (klass) ->
- return
+ return
- constructInstance: (instance, params) ->
- params =
- if instance.BUILDARGS?
- instance.BUILDARGS params
- else
- params
+ _checkMetaclassCompatibility: (klass) ->
+ return
- for own name, attr of @attributes()
- attr.initializeInstanceSlot instance, params
+ constructInstance: (instance, params) ->
+ params =
+ if instance.BUILDARGS?
+ instance.BUILDARGS params
+ else
+ params
- instance.BUILDALL params if instance.BUILDALL?
+ for own name, attr of @attributes()
+ attr.initializeInstanceSlot instance, params
- return instance
+ instance.BUILDALL params if instance.BUILDALL?
- # XXX - this needs to be redone to use the C3 algorithm (or we can just not
- # support multiple inheritance, which is ok too).
- linearizedInheritance: ->
- metas = [];
+ return instance
- for supermeta in @superclasses()
- metas.push supermeta;
+ # XXX - this needs to be redone to use the C3 algorithm (or we can just not
+ # support multiple inheritance, which is ok too).
+ linearizedInheritance: ->
+ metas = [];
- for meta in supermeta.linearizedInheritance()
- metas.push meta
+ for supermeta in @superclasses()
+ metas.push supermeta;
- return metas
+ for meta in supermeta.linearizedInheritance()
+ metas.push meta
- selfAndParents: ->
- metas = @linearizedInheritance()
- metas.unshift @
+ return metas
- return metas
+ selfAndParents: ->
+ metas = @linearizedInheritance()
+ metas.unshift @
- _attachMethod: (method) ->
- method.attachToMeta @
- @class().prototype[ method.name() ] = method.body()
- return
+ return metas
- _detachMethod: (method) ->
- method.detachFromMeta @
- delete @class().prototype[ method.name() ]
- return
+ _attachMethod: (method) ->
+ method.attachToMeta @
+ @class().prototype[ method.name() ] = method.body()
+ return
- methodNamed: (name) ->
- methods = @_methodMap()
- return methods[name] if methods[name]?
+ _detachMethod: (method) ->
+ method.detachFromMeta @
+ delete @class().prototype[ method.name() ]
+ return
- if @class().prototype[name]? && typeof @class().prototype[name] == "function"
- @addMethod name: name, body: @class().prototype[name]
+ methodNamed: (name) ->
+ methods = @_methodMap()
+ return methods[name] if methods[name]?
- return methods[name]
+ if @class().prototype[name]? && typeof @class().prototype[name] == "function"
+ @addMethod name: name, body: @class().prototype[name]
- # XXX - once there's an immutabilization hook this method should just cache
- # the methods
- _methodMap: ->
- methods = @_methodsObj()
+ return methods[name]
- for own name, body of @class().prototype
- continue if methods[name]?
- # XXX - this is kind of gross - maybe have some sort of way of marking a
- # method as hidden or something?
- continue if name == "_super"
+ # XXX - once there's an immutabilization hook this method should just cache
+ # the methods
+ _methodMap: ->
+ methods = @_methodsObj()
- @addMethod name: name, body: @class().prototype[name], source: @
+ for own name, body of @class().prototype
+ continue if methods[name]?
+ # XXX - this is kind of gross - maybe have some sort of way of marking a
+ # method as hidden or something?
+ continue if name == "_super"
- return @_methodsObj()
+ @addMethod name: name, body: @class().prototype[name], source: @
- _attachAttribute: (attribute) ->
- attribute.attachToClass @
- @addMethod method for method in attribute.methods()
- return
+ return @_methodsObj()
- _detachAttribute: (attribute) ->
- attribute.detachFromClass @
- @removeMethod method for method in attribute.methods()
- return
+ _attachAttribute: (attribute) ->
+ attribute.attachToClass @
+ @addMethod method for method in attribute.methods()
+ return
- roles: ->
- classes = @linearizedInheritance()
- classes.unshift @
+ _detachAttribute: (attribute) ->
+ attribute.detachFromClass @
+ @removeMethod method for method in attribute.methods()
+ return
- roles = []
+ roles: ->
+ classes = @linearizedInheritance()
+ classes.unshift @
- for klass in classes
- for role in klass.localRoles()
- roles = roles.concat role.roles()
+ roles = []
- return _.uniq roles
+ for klass in classes
+ for role in klass.localRoles()
+ roles = roles.concat role.roles()
- _callerFromError: (error, ignoreBefore) ->
- re = new RegExp "\\.#{ignoreBefore} \\("
- for line in error.stack.split /\n+/
- if re.test(line)
- next = true
- continue
- else
- continue unless next
- if m = line.match( /\[as (\w+)\]/ )
- return m[1]
- else
- return line.match( /\.(\w+) [\(\[]/ )[1]
+ return _.uniq roles
- return
+ _callerFromError: (error, ignoreBefore) ->
+ re = new RegExp "\\.#{ignoreBefore} \\("
+ for line in error.stack.split /\n+/
+ if re.test(line)
+ next = true
+ continue
+ else
+ continue unless next
+ if m = line.match( /\[as (\w+)\]/ )
+ return m[1]
+ else
+ return line.match( /\.(\w+) [\(\[]/ )[1]
- _defaultAttributeClass: ->
- Attribute
+ return
- name: ->
- @_name
+ _defaultAttributeClass: ->
+ Attribute
- superclasses: ->
- @_superclasses
+ name: ->
+ @_name
- class: ->
- @_class
+ superclasses: ->
+ @_superclasses
-module.exports = Class
+ class: ->
+ @_class
View
91 lib/Brocket/Meta/Method.coffee
@@ -1,63 +1,64 @@
-_ = require "underscore"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-class Method
- constructor: (args) ->
- @_name = args.name
- @_body = args.body
- @_source = args.source
- @_associatedMeta = args.associatedMeta
+define (require) ->
+ _ = require "underscore"
+ util = require "util"
- return
+ class Method
+ constructor: (args) ->
+ @_name = args.name
+ @_body = args.body
+ @_source = args.source
+ @_associatedMeta = args.associatedMeta
- clone: (args) ->
- args ?= {}
+ return
- for prop in [ "name", "body" ]
- args[prop] ?= @[prop]()
+ clone: (args) ->
+ args ?= {}
- args.source = @source()
+ for prop in [ "name", "body" ]
+ args[prop] ?= @[prop]()
- constructor = @constructor
+ args.source = @source()
- return new constructor args
+ constructor = @constructor
- attachToMeta: (meta) ->
- @_setAssociatedMeta meta
- return
+ return new constructor args
- detachFromMeta: (meta) ->
- @_clearAssociatedMeta()
- return
+ attachToMeta: (meta) ->
+ @_setAssociatedMeta meta
+ return
- name: ->
- return @_name
+ detachFromMeta: (meta) ->
+ @_clearAssociatedMeta()
+ return
- body: ->
- return @_body
+ name: ->
+ return @_name
- source: ->
- return @_source
+ body: ->
+ return @_body
- associatedMeta: ->
- return @_associatedMeta
+ source: ->
+ return @_source
- _setAssociatedMeta: (meta) ->
- @_associatedMeta = meta
- return
+ associatedMeta: ->
+ return @_associatedMeta
- _clearAssociatedMeta: ->
- delete @_associatedMeta
- return
+ _setAssociatedMeta: (meta) ->
+ @_associatedMeta = meta
+ return
- # XXX - this is a horrible, horrible, horrible hack - it's necessary because
- # of the wonky way inheritance is currently being handled
- isInheritable: ->
- name = @name()
- if name == "BUILD" || name == "BUILDARGS"
- meta = @associatedMeta()
- return false unless meta? && meta.name() == "Brocket.Base"
+ _clearAssociatedMeta: ->
+ delete @_associatedMeta
+ return
- return true
+ # XXX - this is a horrible, horrible, horrible hack - it's necessary because
+ # of the wonky way inheritance is currently being handled
+ isInheritable: ->
+ name = @name()
+ if name == "BUILD" || name == "BUILDARGS"
+ meta = @associatedMeta()
+ return false unless meta? && meta.name() == "Brocket.Base"
-module.exports = Method
+ return true
View
89 lib/Brocket/Meta/Mixin/AttributeCore.coffee
@@ -1,65 +1,66 @@
-_ = require "underscore"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-class AttributeCore
- _buildAttributeCore: (args) ->
- @_name = args.name
+define (require) ->
+ _ = require "underscore"
+ util = require "util"
- @_access = args.access ? "ro"
- @_validateAccess @_access
+ class AttributeCore
+ _buildAttributeCore: (args) ->
+ @_name = args.name
- @_required = args.required ? false
- @_lazy = args.lazy ? false
+ @_access = args.access ? "ro"
+ @_validateAccess @_access
- return
+ @_required = args.required ? false
+ @_lazy = args.lazy ? false
- _validateAccess: (access) ->
- return if access in [ "bare", "ro", "rw" ]
- throw new Error "The access value for an attribute must be \"bare, \"ro\" or \"rw\", not \"#{access}\""
+ return
- name: ->
- @_name
+ _validateAccess: (access) ->
+ return if access in [ "bare", "ro", "rw" ]
+ throw new Error "The access value for an attribute must be \"bare, \"ro\" or \"rw\", not \"#{access}\""
- access: ->
- @_access
+ name: ->
+ @_name
- required: ->
- @_required
+ access: ->
+ @_access
- isLazy: ->
- @_lazy
+ required: ->
+ @_required
- reader: ->
- @_reader
+ isLazy: ->
+ @_lazy
- hasReader: ->
- @reader()?
+ reader: ->
+ @_reader
- writer: ->
- @_writer
+ hasReader: ->
+ @reader()?
- hasWriter: ->
- return @writer()?
+ writer: ->
+ @_writer
- accessor: ->
- @_accessor
+ hasWriter: ->
+ return @writer()?
- hasAccessor: ->
- return @accessor()?
+ accessor: ->
+ @_accessor
- predicate: ->
- @_predicate
+ hasAccessor: ->
+ return @accessor()?
- hasPredicate: ->
- @predicate()?
+ predicate: ->
+ @_predicate
- clearer: ->
- @_clearer
+ hasPredicate: ->
+ @predicate()?
- hasClearer: ->
- @clearer()?
+ clearer: ->
+ @_clearer
- _defaultFunc: ->
- @__defaultFunc
+ hasClearer: ->
+ @clearer()?
-module.exports = AttributeCore
+ _defaultFunc: ->
+ @__defaultFunc
View
57 lib/Brocket/Meta/Mixin/HasAttributes.coffee
@@ -1,39 +1,40 @@
-_ = require "underscore"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-class HasAttributes
- _buildAttributeProperties: (args) ->
- @__attributeMap = {}
- @_attributeClass = args.attributeClass ? @_defaultAttributeClass()
+define (require) ->
+ _ = require "underscore"
+ util = require "util"
- addAttribute: (attribute) ->
- if attribute not instanceof @_defaultAttributeClass()
- aclass = @attributeClass()
- attribute = new aclass attribute
+ class HasAttributes
+ _buildAttributeProperties: (args) ->
+ @__attributeMap = {}
+ @_attributeClass = args.attributeClass ? @_defaultAttributeClass()
- @_attachAttribute attribute
- @_attributeMap()[ attribute.name() ] = attribute
+ addAttribute: (attribute) ->
+ if attribute not instanceof @_defaultAttributeClass()
+ aclass = @attributeClass()
+ attribute = new aclass attribute
- return attribute
+ @_attachAttribute attribute
+ @_attributeMap()[ attribute.name() ] = attribute
- removeAttribute: (attribute) ->
- @_detachAttribute attribute
- delete @_attributeMap()[ attribute.name() ]
- return
+ return attribute
- hasAttribute: (name) ->
- return @_attributeMap()[name]?
+ removeAttribute: (attribute) ->
+ @_detachAttribute attribute
+ delete @_attributeMap()[ attribute.name() ]
+ return
- attributeNamed: (name) ->
- return @_attributeMap()[name]
+ hasAttribute: (name) ->
+ return @_attributeMap()[name]?
- attributes: ->
- return _.values @_attributeMap()
+ attributeNamed: (name) ->
+ return @_attributeMap()[name]
- _attributeMap: ->
- return @__attributeMap
+ attributes: ->
+ return _.values @_attributeMap()
- attributeClass: ->
- return @_attributeClass
+ _attributeMap: ->
+ return @__attributeMap
-module.exports = HasAttributes
+ attributeClass: ->
+ return @_attributeClass
View
63 lib/Brocket/Meta/Mixin/HasMethods.coffee
@@ -1,44 +1,45 @@
-_ = require "underscore"
-Method = require "../Method"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-class HasMethods
- _buildMethodProperties: (args) ->
- @__methodsObj = {}
- @_methodClass = args.methodClass ? Method
+define (require) ->
+ _ = require "underscore"
+ Method = require "../Method"
+ util = require "util"
- addMethod: (method) ->
- if method not instanceof Method
- mclass = @methodClass()
- method.source ?= @
- method = new mclass method
+ class HasMethods
+ _buildMethodProperties: (args) ->
+ @__methodsObj = {}
+ @_methodClass = args.methodClass ? Method
- @_methodsObj()[ method.name() ] = method
- @_attachMethod method
+ addMethod: (method) ->
+ if method not instanceof Method
+ mclass = @methodClass()
+ method.source ?= @
+ method = new mclass method
- return
+ @_methodsObj()[ method.name() ] = method
+ @_attachMethod method
- removeMethod: (method) ->
- method = @methodNamed method unless method instanceof Method
+ return
- delete @_methodsObj()[ method.name() ]
- @_detachMethod method
+ removeMethod: (method) ->
+ method = @methodNamed method unless method instanceof Method
- return
+ delete @_methodsObj()[ method.name() ]
+ @_detachMethod method
- hasMethod: (name) ->
- return @_methodMap()[name]?
+ return
- methodNamed: (name) ->
- return @_methodMap()[name]
+ hasMethod: (name) ->
+ return @_methodMap()[name]?
- methods: ->
- _.values @_methodMap()
+ methodNamed: (name) ->
+ return @_methodMap()[name]
- _methodsObj: ->
- @__methodsObj
+ methods: ->
+ _.values @_methodMap()
- methodClass: ->
- @_methodClass
+ _methodsObj: ->
+ @__methodsObj
-module.exports = HasMethods
+ methodClass: ->
+ @_methodClass
View
57 lib/Brocket/Meta/Mixin/HasRoles.coffee
@@ -1,39 +1,40 @@
-_ = require "underscore"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-Role = null
+define (require) ->
+ _ = require "underscore"
+ util = require "util"
-class HasRoles
- _buildRoleProperties: ->
- Role ?= require "../Role"
+ Role = null
- @_localRoles = []
- @_roleApplications = []
+ class HasRoles
+ _buildRoleProperties: ->
+ Role ?= require "../Role"
- addRole: (role) ->
- @localRoles().push role
- return
+ @_localRoles = []
+ @_roleApplications = []
- doesRole: (role) ->
- name =
- if role instanceof Role
- role.name()
- else
- role
+ addRole: (role) ->
+ @localRoles().push role
+ return
- for role in @roles()
- return true if role.name() == name
+ doesRole: (role) ->
+ name =
+ if role instanceof Role
+ role.name()
+ else
+ role
- return false
+ for role in @roles()
+ return true if role.name() == name
- addRoleApplication: (application) ->
- @roleApplications().push application
- return
+ return false
- localRoles: ->
- @_localRoles
+ addRoleApplication: (application) ->
+ @roleApplications().push application
+ return
- roleApplications: ->
- @_roleApplications
+ localRoles: ->
+ @_localRoles
-module.exports = HasRoles
+ roleApplications: ->
+ @_roleApplications
View
269 lib/Brocket/Meta/Role.coffee
@@ -1,185 +1,186 @@
-_ = require "underscore"
-Attribute = require "./Attribute"
-Cache = require "./Cache"
-ConflictingMethod = require "./Role/ConflictingMethod"
-HasAttributes = require "./Mixin/HasAttributes"
-HasMethods = require "./Mixin/HasMethods"
-HasRoles = require "./Mixin/HasRoles"
-RequiredMethod = require "./Role/RequiredMethod"
-RoleAttribute = require "./Role/Attribute"
-ToClass = require "./Role/Application/ToClass"
-ToInstance = null #require "./Role/Application/ToInstance"
-ToRole = require "./Role/Application/ToRole"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-Class = null
-Composite = null
+define (require) ->
+ _ = require "underscore"
+ Attribute = require "./Attribute"
+ Cache = require "./Cache"
+ ConflictingMethod = require "./Role/ConflictingMethod"
+ HasAttributes = require "./Mixin/HasAttributes"
+ HasMethods = require "./Mixin/HasMethods"
+ HasRoles = require "./Mixin/HasRoles"
+ RequiredMethod = require "./Role/RequiredMethod"
+ RoleAttribute = require "./Role/Attribute"
+ ToClass = require "./Role/Application/ToClass"
+ ToInstance = null #require "./Role/Application/ToInstance"
+ ToRole = require "./Role/Application/ToRole"
+ util = require "util"
-class Role
- for own key of HasAttributes.prototype
- Role.prototype[key] = HasAttributes.prototype[key]
+ Class = null
+ Composite = null
- for own key of HasMethods.prototype
- Role.prototype[key] = HasMethods.prototype[key]
+ class Role
+ for own key of HasAttributes.prototype
+ Role.prototype[key] = HasAttributes.prototype[key]
- for own key of HasRoles.prototype
- Role.prototype[key] = HasRoles.prototype[key]
+ for own key of HasMethods.prototype
+ Role.prototype[key] = HasMethods.prototype[key]
- constructor: (args) ->
- @_name = args.name
- throw new Error "You must provide a name when constructing a role" unless @_name
+ for own key of HasRoles.prototype
+ Role.prototype[key] = HasRoles.prototype[key]
- args.cache = true unless args.cache? && ! args.cache
+ constructor: (args) ->
+ @_name = args.name
+ throw new Error "You must provide a name when constructing a role" unless @_name
- # This is necessary to avoid a circular dependency issue between Class &
- # Role. One of them has to be loaded later.
- Class ?= require "./Class"
+ args.cache = true unless args.cache? && ! args.cache
- if args.cache && Cache.metaObjectExists args.name
- meta = Cache.getMetaObject args.name
- unless meta instanceof Role
- message = "Found an existing meta object named #{ args.name } which is not a Role object."
- if meta instanceof Class
- message += " You cannot create a Class and a Role with the same name."
- throw new Error message
+ # This is necessary to avoid a circular dependency issue between Class &
+ # Role. One of them has to be loaded later.
+ Class ?= require "./Class"
- return meta
+ if args.cache && Cache.metaObjectExists args.name
+ meta = Cache.getMetaObject args.name
+ unless meta instanceof Role
+ message = "Found an existing meta object named #{ args.name } which is not a Role object."
+ if meta instanceof Class
+ message += " You cannot create a Class and a Role with the same name."
+ throw new Error message
- @_buildMethodProperties args
- @_buildAttributeProperties args
- @_buildRoleProperties args
+ return meta
- @_requiredMethods = []
+ @_buildMethodProperties args
+ @_buildAttributeProperties args
+ @_buildRoleProperties args
- @_requiredMethodClass = args.requiredMethodClass ? RequiredMethod
- @_conflictingMethodClass = args.conflictingMethodClass ? ConflictingMethod
+ @_requiredMethods = []
- @_applicationToClassClass = args.applicationToClassClass ? ToClass
- @_applicationToRoleClass = args.applicationToRoleClass ? ToRole
- @_applicationToInstanceClass = args.applicationToInstanceClass ? ToRole
+ @_requiredMethodClass = args.requiredMethodClass ? RequiredMethod
+ @_conflictingMethodClass = args.conflictingMethodClass ? ConflictingMethod
- @_appliedAttributeClass = args.appliedAttributeClass ? Attribute
+ @_applicationToClassClass = args.applicationToClassClass ? ToClass
+ @_applicationToRoleClass = args.applicationToRoleClass ? ToRole
+ @_applicationToInstanceClass = args.applicationToInstanceClass ? ToRole
- @_localRoles = []
+ @_appliedAttributeClass = args.appliedAttributeClass ? Attribute
- Cache.storeMetaObject @ if args.cache
+ @_localRoles = []
- return
+ Cache.storeMetaObject @ if args.cache
- @Combine = (rolesWithArgs) ->
- Composite ?= require "./Role/Composite"
+ return
- roles = []
- args = {}
+ @Combine = (rolesWithArgs) ->
+ Composite ?= require "./Role/Composite"
- for i in [ 0 .. rolesWithArgs.length - 1 ] by 2
- role = rolesWithArgs[i]
- roles.push role
- args[ role.name() ] = rolesWithArgs[ i + 1 ]
+ roles = []
+ args = {}
- # XXX - need to allow each role to provide traits to be applied to the
- # Composite class
- composite = new Composite roles: roles
- return composite.applyCompositionArgs roleParams: args
+ for i in [ 0 .. rolesWithArgs.length - 1 ] by 2
+ role = rolesWithArgs[i]
+ roles.push role
+ args[ role.name() ] = rolesWithArgs[ i + 1 ]
- _defaultAttributeClass: ->
- RoleAttribute
+ # XXX - need to allow each role to provide traits to be applied to the
+ # Composite class
+ composite = new Composite roles: roles
+ return composite.applyCompositionArgs roleParams: args
- _attachAttribute: (attr) ->
- attr.attachToRole @
- return
+ _defaultAttributeClass: ->
+ RoleAttribute
- _detachAttribute: (attr) ->
- attr.detachFromRole @
- return
+ _attachAttribute: (attr) ->
+ attr.attachToRole @
+ return
- _attachMethod: (method) ->
- method.attachToMeta @
- return
+ _detachAttribute: (attr) ->
+ attr.detachFromRole @
+ return
- _detachMethod: (method) ->
- method.detachFromMeta @
- return
+ _attachMethod: (method) ->
+ method.attachToMeta @
+ return
- # Unlike a class, methods can only be added to a role explicitly, so we
- # don't need to check an associated prototype for implicit methods.
- _methodMap: ->
- return @_methodsObj()
+ _detachMethod: (method) ->
+ method.detachFromMeta @
+ return
- addRequiredMethod: (method) ->
- rmclass = @requiredMethodClass()
- unless method instanceof rmclass
- method = new rmclass name: method
+ # Unlike a class, methods can only be added to a role explicitly, so we
+ # don't need to check an associated prototype for implicit methods.
+ _methodMap: ->
+ return @_methodsObj()
- @requiredMethods().push method
+ addRequiredMethod: (method) ->
+ rmclass = @requiredMethodClass()
+ unless method instanceof rmclass
+ method = new rmclass name: method
- return;
+ @requiredMethods().push method
- addConflictingMethod: (method) ->
- rmclass = @conflictingMethodClass()
- unless method instanceof rmclass
- method = new rmclass method
+ return;
- @requiredMethods().push method
+ addConflictingMethod: (method) ->
+ rmclass = @conflictingMethodClass()
+ unless method instanceof rmclass
+ method = new rmclass method
- return;
+ @requiredMethods().push method
- apply: (other, args) ->
- args ?= {}
+ return;
- if other instanceof Class
- appClass = @applicationToClassClass()
- else if other instanceof Role
- appClass = @applicationToRoleClass()
- else if other instanceof Object
- appClass = @applicationToInstanceClass()
- else
- throw new Error "Cannot apply a role to a #{ other.toString() }"
+ apply: (other, args) ->
+ args ?= {}
- (new appClass args).apply @, other
+ if other instanceof Class
+ appClass = @applicationToClassClass()
+ else if other instanceof Role
+ appClass = @applicationToRoleClass()
+ else if other instanceof Object
+ appClass = @applicationToInstanceClass()
+ else
+ throw new Error "Cannot apply a role to a #{ other.toString() }"
- return
+ (new appClass args).apply @, other
- roles: ->
- roles = [@].concat @localRoles()
+ return
- seen = {}
- for role in roles
- continue if seen[ role.name() ]
- seen[ role.name() ] = role
- roles.push role.localRoles()
+ roles: ->
+ roles = [@].concat @localRoles()
- return _.values seen
+ seen = {}
+ for role in roles
+ continue if seen[ role.name() ]
+ seen[ role.name() ] = role
+ roles.push role.localRoles()
- _roleForCombination: ->
- return @
+ return _.values seen
- name: ->
- return @_name
+ _roleForCombination: ->
+ return @
- requiredMethods: ->
- return @_requiredMethods
+ name: ->
+ return @_name
- requiredMethodClass: ->
- return @_requiredMethodClass
+ requiredMethods: ->
+ return @_requiredMethods
- conflictingMethods: ->
- cmclass = @conflictingMethodClass()
- return ( m for m in @requiredMethods() when m instanceof cmclass )
+ requiredMethodClass: ->
+ return @_requiredMethodClass
- conflictingMethodClass: ->
- return @_conflictingMethodClass
+ conflictingMethods: ->
+ cmclass = @conflictingMethodClass()
+ return ( m for m in @requiredMethods() when m instanceof cmclass )
- applicationToClassClass: ->
- return @_applicationToClassClass
+ conflictingMethodClass: ->
+ return @_conflictingMethodClass
- applicationToRoleClass: ->
- return @_applicationToRoleClass
+ applicationToClassClass: ->
+ return @_applicationToClassClass
- applicationToInstanceClass: ->
- return @_applicationToInstanceClass
+ applicationToRoleClass: ->
+ return @_applicationToRoleClass
- appliedAttributeClass: ->
- return @_appliedAttributeClass
+ applicationToInstanceClass: ->
+ return @_applicationToInstanceClass
-module.exports = Role
+ appliedAttributeClass: ->
+ return @_appliedAttributeClass
View
29 lib/Brocket/Meta/Role/Application.coffee
@@ -1,19 +1,20 @@
-_ = require "underscore"
-Helpers = require "../../Helpers"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-class Application
- apply: ->
- @_checkRequiredMethods()
+define (require) ->
+ _ = require "underscore"
+ Helpers = require "../../Helpers"
+ util = require "util"
- @_applyAttributes()
- @_applyMethods()
+ class Application
+ apply: ->
+ @_checkRequiredMethods()
-# @_applyOverrideMethodModifiers()
-# @_applyBeforeMethodModifiers()
-# @_applyAroundMethodModifiers()
-# @_applyAfterMethodModifiers()
+ @_applyAttributes()
+ @_applyMethods()
- return
+ # @_applyOverrideMethodModifiers()
+ # @_applyBeforeMethodModifiers()
+ # @_applyAroundMethodModifiers()
+ # @_applyAfterMethodModifiers()
-module.exports = Application
+ return
View
137 lib/Brocket/Meta/Role/Application/RoleSummation.coffee
@@ -1,92 +1,93 @@
-_ = require "underscore"
-Application = require "../Application"
-Helpers = require "../../../Helpers"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-class RoleSummation extends Application
- constructor: (args) ->
- @_roleParams = args.roleParams
- return
+define (require) ->
+ _ = require "underscore"
+ Application = require "../Application"
+ Helpers = require "../../../Helpers"
+ util = require "util"
- apply: (compositeRole) ->
- @_compositeRole = compositeRole
- super
+ class RoleSummation extends Application
+ constructor: (args) ->
+ @_roleParams = args.roleParams
+ return
- _checkRequiredMethods: ->
- roles = @compositeRole().roles()
+ apply: (compositeRole) ->
+ @_compositeRole = compositeRole
+ super
- allRequired = []
- for role in roles
- for m in role.requiredMethods()
- continue if _.any( roles, (r) -> r.hasMethod m.name() )
- allRequired.push m
+ _checkRequiredMethods: ->
+ roles = @compositeRole().roles()
- for m in allRequired
- @compositeRole().addRequiredMethod m
+ allRequired = []
+ for role in roles
+ for m in role.requiredMethods()
+ continue if _.any( roles, (r) -> r.hasMethod m.name() )
+ allRequired.push m
- return
+ for m in allRequired
+ @compositeRole().addRequiredMethod m
- _applyAttributes: ->
- allAttributes = []
- for role in @compositeRole().roles()
- allAttributes = allAttributes.concat role.attributes()
+ return
- seen = {}
- for attr in allAttributes
- name = attr.name()
+ _applyAttributes: ->
+ allAttributes = []
+ for role in @compositeRole().roles()
+ allAttributes = allAttributes.concat role.attributes()
- if seen[name]
- role1 = attr.associatedRole().name()
- role2 = seen[name].name()
+ seen = {}
+ for attr in allAttributes
+ name = attr.name()
- message = "We have encountered an attribute conflict with '#{name}'" +
- " during role composition." +
- " This attribute is defined in both #{role1} and #{role2}. " +
- " This is a fatal error and cannot be disambiguated."
- throw new Error message
+ if seen[name]
+ role1 = attr.associatedRole().name()
+ role2 = seen[name].name()
- seen[ attr.name() ] = attr
+ message = "We have encountered an attribute conflict with '#{name}'" +
+ " during role composition." +
+ " This attribute is defined in both #{role1} and #{role2}. " +
+ " This is a fatal error and cannot be disambiguated."
+ throw new Error message
- for attr in allAttributes
- @compositeRole().addAttribute attr.clone()
+ seen[ attr.name() ] = attr
- return
+ for attr in allAttributes
+ @compositeRole().addAttribute attr.clone()
- _applyMethods: ->
- roles = @compositeRole().roles()
- allMethods = []
+ return
- for role in roles
- for method in role.methods()
- allMethods.push { role: role, name: method.name(), method: method }
+ _applyMethods: ->
+ roles = @compositeRole().roles()
+ allMethods = []
- seen = {}
- conflicts = {}
- methodMap = {}
+ for role in roles
+ for method in role.methods()
+ allMethods.push { role: role, name: method.name(), method: method }
- for method in allMethods
- continue if conflicts[ method.name ]
+ seen = {}
+ conflicts = {}
+ methodMap = {}
- saw = seen[ method.name ]
- if saw?
- if saw.method.body() != method.method.body()
- @compositeRole().addConflictingMethod name: method.name, roles: [ method.role, saw.role ]
- delete methodMap[ method.name ]
- conflicts[ method.name ] = true
- continue
+ for method in allMethods
+ continue if conflicts[ method.name ]
- seen[ method.name ] = method
- methodMap[ method.name ] = method.method
+ saw = seen[ method.name ]
+ if saw?
+ if saw.method.body() != method.method.body()
+ @compositeRole().addConflictingMethod name: method.name, roles: [ method.role, saw.role ]
+ delete methodMap[ method.name ]
+ conflicts[ method.name ] = true
+ continue
- for name, method of methodMap
- @compositeRole().addMethod method.clone name: name
+ seen[ method.name ] = method
+ methodMap[ method.name ] = method.method
- return
+ for name, method of methodMap
+ @compositeRole().addMethod method.clone name: name
- roleParams: ->
- @_roleParams
+ return
- compositeRole: ->
- @_compositeRole
+ roleParams: ->
+ @_roleParams
-module.exports = RoleSummation
+ compositeRole: ->
+ @_compositeRole
View
119 lib/Brocket/Meta/Role/Application/ToClass.coffee
@@ -1,85 +1,86 @@
-_ = require "underscore"
-Application = require "../Application"
-Conflicting = require "../ConflictingMethod"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-class ToClass extends Application
- apply: (role, metaclass) ->
- @_role = role
- @_class = metaclass
+define (require) ->
+ _ = require "underscore"
+ Application = require "../Application"
+ Conflicting = require "../ConflictingMethod"
+ util = require "util"
- super
+ class ToClass extends Application
+ apply: (role, metaclass) ->
+ @_role = role
+ @_class = metaclass
- metaclass.addRole role
- metaclass.addRoleApplication @
+ super
- return
+ metaclass.addRole role
+ metaclass.addRoleApplication @
- # XXX - also need to handle conflicting methods
- _checkRequiredMethods: ->
- missing = []
- conflicting = []
+ return
- for method in @role().requiredMethods()
- continue if @class().hasMethod method.name()
+ # XXX - also need to handle conflicting methods
+ _checkRequiredMethods: ->
+ missing = []
+ conflicting = []
- if method instanceof Conflicting
- conflicting.push method
- else
- missing.push method
+ for method in @role().requiredMethods()
+ continue if @class().hasMethod method.name()
- messages = []
+ if method instanceof Conflicting
+ conflicting.push method
+ else
+ missing.push method
- if conflicting.length
- message = "The following method conflicts were detected:"
- for conflict in conflicting
- roles = (_.map conflict.roles(), (r) -> r.name() ).join " and "
- message += "\n'#{ conflict.name() }' conflicts in #{roles}"
+ messages = []
- messages.push message
+ if conflicting.length
+ message = "The following method conflicts were detected:"
+ for conflict in conflicting
+ roles = (_.map conflict.roles(), (r) -> r.name() ).join " and "
+ message += "\n'#{ conflict.name() }' conflicts in #{roles}"
- if missing.length
- noun = if missing.length > 1 then "method" else "methods"
- list = _.map missing.sort().join(", "), (name) ->
- "'#{name}'"
+ messages.push message
- message = "The #{ @role().name() } role requires the #{noun} #{list}
- to be implemented by #{ @class().name() }"
+ if missing.length
+ noun = if missing.length > 1 then "method" else "methods"
+ list = _.map missing.sort().join(", "), (name) ->
+ "'#{name}'"
- messages.push message
+ message = "The #{ @role().name() } role requires the #{noun} #{list}
+ to be implemented by #{ @class().name() }"
- if messages.length
- throw new Error messages.join "\n"
+ messages.push message
- return
+ if messages.length
+ throw new Error messages.join "\n"
- _applyAttributes: ->
- metaclass = @class()
+ return
- for attr in @role().attributes()
- continue if metaclass.hasAttribute attr.name()
- metaclass.addAttribute attr.attributeForClass()
+ _applyAttributes: ->
+ metaclass = @class()
- return
+ for attr in @role().attributes()
+ continue if metaclass.hasAttribute attr.name()
+ metaclass.addAttribute attr.attributeForClass()
- _applyMethods: ->
- metaclass = @class()
+ return
- for method in @role().methods()
- @_applyMethod method
+ _applyMethods: ->
+ metaclass = @class()
- return
+ for method in @role().methods()
+ @_applyMethod method
- _applyMethod: (method) ->
- return if @class().hasMethod method.name()
- @class().addMethod method.clone()
+ return
- return
+ _applyMethod: (method) ->
+ return if @class().hasMethod method.name()
+ @class().addMethod method.clone()
- role: ->
- return @_role
+ return
- class: ->
- return @_class
+ role: ->
+ return @_role
-module.exports = ToClass
+ class: ->
+ return @_class
View
79 lib/Brocket/Meta/Role/Application/ToRole.coffee
@@ -1,55 +1,56 @@
-_ = require "underscore"
-Application = require "../Application"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-class ToRole extends Application
- apply: (appliedRole, receivingRole) ->
- @_appliedRole = appliedRole
- @_receivingRole = receivingRole
+define (require) ->
+ _ = require "underscore"
+ Application = require "../Application"
+ util = require "util"
- super
+ class ToRole extends Application
+ apply: (appliedRole, receivingRole) ->
+ @_appliedRole = appliedRole
+ @_receivingRole = receivingRole
- @receivingRole().addRole @appliedRole()
- @receivingRole().addRoleApplication @
+ super
- return
+ @receivingRole().addRole @appliedRole()
+ @receivingRole().addRoleApplication @
- _checkRequiredMethods: ->
- for method in @appliedRole().requiredMethods()
- continue if @receivingRole().hasMethod method.name()
- @receivingRole().addRequiredMethod method
+ return
- return
+ _checkRequiredMethods: ->
+ for method in @appliedRole().requiredMethods()
+ continue if @receivingRole().hasMethod method.name()
+ @receivingRole().addRequiredMethod method
- _applyAttributes: ->
- for attr in @appliedRole().attributes()
- if @receivingRole().hasAttribute attr.name() && @receivingRole().attributeNamed attr.name() != attr
- unless @receivingrole().attributeNamed attribute.name() == attribute
- message = "There was an attribute conflict while composing" +
- "#{ @appliedRole().name() } into #{ @receivingRole().name() }." +
- "This is a fatal error and cannot be disambiguated." +
- "The conflict attribute is named '#{ attr.name() }'"
- throw new Error message
+ return
- @receivingRole().addAttribute attr.clone()
+ _applyAttributes: ->
+ for attr in @appliedRole().attributes()
+ if @receivingRole().hasAttribute attr.name() && @receivingRole().attributeNamed attr.name() != attr
+ unless @receivingrole().attributeNamed attribute.name() == attribute
+ message = "There was an attribute conflict while composing" +
+ "#{ @appliedRole().name() } into #{ @receivingRole().name() }." +
+ "This is a fatal error and cannot be disambiguated." +
+ "The conflict attribute is named '#{ attr.name() }'"
+ throw new Error message
- return
+ @receivingRole().addAttribute attr.clone()
- _applyMethods: ->
- for method in @appliedRole().methods()
- @_applyMethod method
+ return
- return
+ _applyMethods: ->
+ for method in @appliedRole().methods()
+ @_applyMethod method
- _applyMethod: (method) ->
- return if @receivingRole().hasMethod method.name()
+ return
- @receivingRole().addMethod method.clone()
+ _applyMethod: (method) ->
+ return if @receivingRole().hasMethod method.name()
- appliedRole: ->
- return @_appliedRole
+ @receivingRole().addMethod method.clone()
- receivingRole: ->
- return @_receivingRole
+ appliedRole: ->
+ return @_appliedRole
-module.exports = ToRole
+ receivingRole: ->
+ return @_receivingRole
View
81 lib/Brocket/Meta/Role/Attribute.coffee
@@ -1,56 +1,57 @@
-_ = require "underscore"
-AttributeCore = require "../Mixin/AttributeCore"
-util = require "util"
+`if (typeof define !== 'function') { var define = require('amdefine')(module) }`
-class Attribute
- for own key of AttributeCore.prototype
- Attribute.prototype[key] = AttributeCore.prototype[key]
+define (require) ->
+ _ = require "underscore"
+ AttributeCore = require "../Mixin/AttributeCore"
+ util = require "util"
- constructor: (args) ->
- @_buildAttributeCore args
+ class Attribute
+ for own key of AttributeCore.prototype
+ Attribute.prototype[key] = AttributeCore.prototype[key]
- if args._originalRole?
- @__originalRole = args._originalRole
- delete args._originalRole
+ constructor: (args) ->
+ @_buildAttributeCore args
- @__originalArgs = args
+ if args._originalRole?
+ @__originalRole = args._originalRole
+ delete args._originalRole
- return
+ @__originalArgs = args
- attributeForClass: ->
- aclass = @originalRole().appliedAttributeClass()
- return new aclass @_originalArgs()
+ return
- attachToRole: (role) ->
- @_associatedRole = role
- return;
+ attributeForClass: ->
+ aclass = @originalRole().appliedAttributeClass()
+ return new aclass @_originalArgs()