activeobject

0.3.3 • Public • Published

ActiveObject

Inspired by ActiveModel for Rails, ActiveObject provides functionality to easily validate and merge JSON documents.

Installation

$ npm install activeobject

Features

  • Attribute Accessible
  • Validations
  • hasOne
  • hasMany
  • Normalization

Using ActiveObject

ActiveObject takes an object defining the properties of the class to create, and returns a constructor for that class.

var ActiveObject = require("active-object");
var Foo = ActiveObject({});

Defining Attributes

var Rectangle = ActiveObject({
  attributeAccessible: ["height", "width"]
});

var square = new Rectangle();
square.height = 5;
square.width = 5;

Once an object is created, it will only accept assignment of properties defined in attributeAccessible

square.radius = 10;
// => Error!

Properties can also be set when creating the object:

var square = new Rectangle({ height: 5, width: 5 });

Or they can be set together:

var square = new Rectangle();
square.attributes = { height: 5, width: 5 };

Again, setting a property not defined in the attributeAccessible will result in an error:

var square = new Rectangle({ radius: 10 });
// => Error

var square = new Rectangle();
square.attributes = { radius: 10 };
// => Error

Defining Child Objects

ActiveObject provides 2 ways to define child objects, hasOne and hasMany

hasOne

hasOne relationships can be defined with an object using the key as the property name, and the constructor as the value.

var Star = ActiveObject({
  attributeAccessible: ["diameter"]
});

var SolarSystem = ActiveObjct({
  hasOne: { sun: Star }
});

The object can then have its child set the same as a regular attribute:

var solarSystem = new SolarSystem();
solarSystem.sun = new Star({diameter: "1,392,000 Km"});

solarSystem.sun.diamaeter;
// => 1,392,000 Km

hasMany

hasMany relationships are defined the same as hasOne relationships.

var Star = ActiveObject({
  attributeAccessible: ["diameter"]
});

var SolarSystem = ActiveObjct({
  hasOne: { sun: Star },
  hasMany: { planets: Planet }
});

var Planet = ActiveObject({
  attributeAccessible: ["name"]
});

A SolarSystem can now have 1 star and many planets.

var solarSystem = new SolarSystem();
solarSystem.sun = new Star({diameter: "1,392,000 Km"});

solarSystem.planets.build({ name: "Mercury"});
solarSystem.planets.build({ name:"Venus"});
solarSystem.planets.build({ name:"Earth"})
solarSystem.planets.build({ name:"Mars"});
solarSystem.planets.build({ name:"Jupiter"});
solarSystem.planets.build({ name:"Saturn"});
solarSystem.planets.build({ name:"Uranus"});

forEach(solarSystem.planets, function(planet) {
  console.log(planet.name);
});

// => "Mercury"
// => "Venus"
// => "Earth"
// => "Mars"
// => "Jupiter"
// => "Saturn"
// => "Uranus"

hasMany Collection Methods

ActiveObject adds the following methods for retrieval and query to collections:

collection.findById(id)

Finds an associated object responding to the id and that meets the condition that it has to be associated with this object.

collection.build(attributes = {})

Returns a new object of the collection type that has been instantiated with attributes and added to this objects collection.

License

(The MIT License)

Copyright (c) 2009-2011 Mike Hemesath <mike.hemesath@gmail.com>

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.

Readme

Keywords

none

Package Sidebar

Install

npm i activeobject

Weekly Downloads

1

Version

0.3.3

License

none

Last publish

Collaborators

  • mike.hemesath