Skip to content

michaelficarra/MooTools-Struct

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Struct

Struct generates classes to be used similarly to C's struct construct. Inspired by Ruby's Struct class

How To Use

Generate a Struct class with the constructor

var Car = new Struct('make','model','year')		// <#Class:Car>
var myCar = new Car('Chrysler','Cirrus',2000)	// <#Car:myCar>

myCar.getMake()					// 'Chrysler'
myCar.getModel()				// 'Cirrus'
myCar.getYear()					// 2000

myCar.setModel('Sebring')		// <#Car:myCar>
myCar.getModel()				// 'Sebring'

Undefined values will be ignored

var Car = new Struct('make','model','year')		// <#Class:Car>
var myCar = new Car('Chrysler','Cirrus')		// <#Car:myCar>

myCar.getMake()					// 'Chrysler'
myCar.getYear()					// null

myCar.setYear(2000)				// <#Car:myCar>
myCar.getYear()					// 2000

Optionally, one may set the prefixes for the getter and setter methods when generating the class

var Car = new Struct('make','model','year',{
	getterPrefix: '',
	setterPrefix: 'change'
})												// <#Class:Car>
var myCar = new Car('Chrysler','Cirrus',2000)	// <#Car:myCar>

myCar.make()					// 'Chrysler'
myCar.year()					// 2000
myCar.changeYear(2010)			// <#Car:myCar>
myCar.year()					// 2010

An Array may also be used to specify Struct members

var Car = new Struct(['make','model','year'])	// <#Class:Car>
var Car2 = new Struct('make','model','year')	// <#Class:Car2>

Car.members()		// ['make','model','year']
Car2.members()		// ['make','model','year']

Instance Methods

In the following examples, assume the following Struct has been defined

var Car = new Struct('make','model','year')		// <#Class:Car>

toHash

Retrieve a hash representation of the Struct and its contents

(new Car('Chrysler','Cirrus',2000)).toHash()
// {make:'Chrysler',model:'Cirrus',year:2000}

(new Car('Chrysler','Cirrus')).toHash()
// {make:'Chrysler',model:'Cirrus',year:undefined}

members

Retrieve the Struct's properties

(new Car()).members()		// ['make','model','year']

each(func(val,key,hash), bind)

Passes the given function to the each function of the hash representation of the Struct

var cadillac = new Car('Cadillac','CTS',2010)	// <#Car:cadillac>
cadillac.each(function(val,key){
	console.log(key+' => '+val.toString())
})

// Console Output:
//  make => Cadillac
//  model => CTS
//  year => 2010

equals

Checks if two instances of a class generated by Struct have the same values for the available properties. Does not check that the constructors are the same reference, but does check that each object's constructor takes the same parameters. This ensures duck-typing equality rather than type equality.

var myCar = new Car('Chrysler','Cirrus',2000),
	cadillac = new Car('Cadillac','CTS',2010),
	anotherCaddy = new Car('Cadillac','CTS',2010)
myCar.equals(cadillac)				// false (unfortunately)
cadillac.equals(anotherCaddy)		// true

Array.toStruct(options)

Converts the Array instance to a Struct class, using the Array entries as Struct properties. Passes an optional Hash/Object to the Struct constructor as options

var Car = ['make','model','year'].toStruct({getterPrefix:''})	// <#Class:Car>

Class Methods

members

Retrieve the Struct's properties

Car.members()		// ['make','model','year']

Known Issues

There are currently no known issues.

Additional Info

I am always open for feature requests or any feedback. I can be reached at Github.

Thanks to the Ruby community for the original idea and implementation.

About

MooTools class to generate classes that are used similarly to C's struct construct

Resources

License

LGPL-3.0, GPL-3.0 licenses found

Licenses found

LGPL-3.0
COPYING.LESSER
GPL-3.0
COPYING

Stars

Watchers

Forks

Packages

No packages published