Struct 0.2.5

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



Details

Author
Michael Ficarra
Current version
0.2.5
GitHub
michaelficarra/MooTools-Struct
Downloads
14188
Category
Native
Tags
Report
GitHub Issues

Releases


Dependencies

  • _self_/_current_:
    • Core
    • Array
    • Hash
    • Class
    • Options

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)   // [Object:myCar]

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

console.log(myCar.setModel('Sebring'))          // [Object:myCar]
console.log(myCar.getModel())                   // 'Sebring'

Undefined values will be ignored

var Car = new Struct('make','model','year')     // [Class:Car]
var myCar = new Car('Chrysler','Cirrus')        // [Object:myCar]

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

console.log(myCar.setYear(2000))                // [Object:myCar]
console.log(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)   // [Object:myCar]

console.log(myCar.make())                       // 'Chrysler'
console.log(myCar.year())                       // 2000
console.log(myCar.changeYear(2010))             // [Object:myCar]
console.log(myCar.year())                       // 2010

An Array may also be used to specify Struct members

var Car = new Struct(['make','model','year'])
var Car2 = new Struct('make','model','year')

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)
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.


Discuss

A note on comments here: These comments are moderated. No comments will show up until they are approved. Comments that are not productive (i.e. inflammatory, rude, etc) will not be approved.

Found a bug in this plugin? Please report it this repository's Github Issues.

blog comments powered by Disqus