Skip to content

Commit

Permalink
update to version 2.0.0 -- simplified the Generator.generate method a…
Browse files Browse the repository at this point in the history
…nd added Generator.toGenerator method (turns a constructor into a generator).
  • Loading branch information
Michaelangelo Jong committed Jan 13, 2015
1 parent 1f0f541 commit 3850552
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 138 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Michaelangelo Jong
Copyright (c) 2014-2015 Michaelangelo Jong

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
125 changes: 84 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
## Table of Contents

* [ Generator ](#generator)
* [ Generator.generate(create, init) ](#generate)
* [ Generator.generate(create) ](#generate)
* [ Generator.isGenerator(test) ](#is-generator)
* [ Generator.toGenerator(constructor) ](#to-generator)
* [ Class: Generation ](#class-generation)
* [ Generation.name ](#generation-name)
* [ Generation.definePrototype([descriptor], properties) ](#generation-define-prototype)
* [ Generation.definePrototype([descriptor,] properties) ](#generation-define-prototype)
* [ Generation.create() ](#generation-create)
* [ Generation.generate(create, init) ](#generation-generate)
* [ Generation.generate(create) ](#generation-generate)
* [ Generation.isCreation(test) ](#generation-is-creation)
* [ Generation.isGeneration(test) ](#generation-is-generation)
* [ Class: Creation ](#class-creation)
* [ Creation.defineProperties([descriptor], properties) ](#creation-define-properties)
* [ Creation.defineProperties([descriptor,] properties) ](#creation-define-properties)

<a name="generator"></a>
Generator
Expand All @@ -25,10 +26,9 @@ $ npm install generate-js
```

<a name="generate"></a>
## Generator.generate(create, init)
## Generator.generate(create)

* *create* `Function` Create method that gets called when creating a new object that inherits from *this* generator.
* *init* `Function` Init method that gets called to initialize any data stores needed by prototypal methods.
* *return*: `Generator` A new generator that inherits from *this* generator.

Returns a new generator that inherits from Generator.
Expand All @@ -39,16 +39,10 @@ var Generator = require('generate-js');

var Person = Generator.generate(
/* create method */
function (name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
},
/* init method */
function () {
this.name = 'bob';
this.age = 0;
this.sex = 'male';
function Person(name, age, sex) {
this.name = name || 'no-name';
this.age = age || 0;
this.sex = sex || 'unknown';
}
);
```
Expand All @@ -61,10 +55,38 @@ var Person = Generator.generate(

Returns `true` if *test* object or its generator was generated by Generator, `false` otherwise.

<a name="to-generator"></a>
## Generator.toGenerator(constructor)

* *constructor* `Function` An constructor to be generatorized.
* *return*: `Generator` A new generator that who's create method is equivalent to calling `new constructor();`.

Returns a new generator that is equivalent to *constructor*.

*NOTE*: Most native constructors can *NOT* be generatorized.

Example:
```javascript
// generatorize NodeJS' EventEmitter
var Generator = require('generate-js'),
events = require('events');

var EventEmitter = Generator.toGenerator(events.EventEmitter);

// EventEmitter.create() same as new events.EventEmitter();

// new generators can inherit all the abilities of EventEmitter like so.
var MyNewGenerator = EventEmitter.generate(
/* create method */
function MyNewGenerator() {}
);

```

<a name="class-generation"></a>
## Class: Generation

A new generator that inherits from the generator that generated it using the [ Generation.generate(create, init) ](#generation-generate) method.
A new generator that inherits from the generator that generated it using the [ Generation.generate(create) ](#generation-generate) method.

<a name="generation-name"></a>
## Generation.name
Expand All @@ -73,7 +95,7 @@ A new generator that inherits from the generator that generated it using the [ G
Name property for *this* generator.

<a name="generation-define-prototype"></a>
## Generation.definePrototype([descriptor], properties)
## Generation.definePrototype([descriptor,] properties)

* *descriptor* `Object` Optional object descriptor that will be applied to all attaching properties.
* *configurable* `Boolean` States weather or not properties will be *configurable*, defaults to `false`.
Expand All @@ -87,7 +109,7 @@ Defines shared properties for all objects created by *this* generator.
Example:
```javascript
/*
* Defining prototype properties that can be overwritten by generations.
* Defining prototype properties that can be overwritten by creations using the `=` sign.
*/
Person.definePrototype(
{
Expand All @@ -101,7 +123,7 @@ Person.definePrototype(
);

/*
* Defining prototype properties that can NOT be overwritten by generations.
* Defining prototype properties that can NOT be overwritten by creations using the `=` sign.
*/
Person.definePrototype(
{
Expand All @@ -116,21 +138,19 @@ Person.definePrototype(

/*
* Defining prototype properties that use getters and setters.
* NOTE: getter/setter prototype properties can NOT be overwritten by generations.
* NOTE: getter/setter prototype properties can NOT be overwritten by creations using the `=` sign.
*/
(function(){
var money = 0;
var something = 'something';

Person.definePrototype({
money: {
something: {
get: function() {
return money;
return something;
},
set: function (newMoney) {
if (typeof newMoney === 'number') {
money = newMoney;
}
return money;
set: function (newSomething) {
something = newSomething;
return something;
}
}
});
Expand Down Expand Up @@ -159,10 +179,9 @@ jim.sayBye(); // prints out: 'Goodbye.'
```

<a name="generation-generate"></a>
## Generation.generate(create, init)
## Generation.generate(create)

* *create* `Function` Create method that gets called when creating a new object that inherits from *this* generator.
* *init* `Function` Init method that gets called to initialize any data stores needed by prototypal methods.
* *return*: `Generator` A new generator that inherits from *this* generator.

Returns a new generator that inherits from *this* generator.
Expand All @@ -171,15 +190,11 @@ Example:
```javascript
var Student = Person.generate(
/* create method */
function (name, age, sex, studentId) {
this.name = name;
this.age = age;
this.sex = sex;
this.studentId = studentId;
},
/* init method */
function () {
this.studentId = 'A0000000000';
function Student(name, age, sex, studentId) {
// 'supercreate' method is only available in this create method scope.
// NOTE: if the 'supercreate' method is not called implicitly it will be called with no arguments.
this.supercreate(name, age, sex);
this.studentId = studentId || 'A0000000000';
}
);

Expand Down Expand Up @@ -228,7 +243,7 @@ Returns `true` if *test* was created by *this* generator or its generations, `fa
A new object that inherits from the generator that created it using the [ Generation.create() ](#generation-create) method.

<a name="creation-define-properties"></a>
## Creation.defineProperties([descriptor], properties)
## Creation.defineProperties([descriptor,] properties)

* *descriptor* `Object` Optional object descriptor that will be applied to all attaching properties.
* *configurable* `Boolean` States weather or not properties will be *configurable*, defaults to `false`.
Expand Down Expand Up @@ -266,3 +281,31 @@ sarah.defineProperties(
);

```

## Author:
Michaelangelo Jong

## License:
The MIT License (MIT)

Copyright (c) 2014-2015 Michaelangelo Jong

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


Loading

0 comments on commit 3850552

Please sign in to comment.