Glorify allows you to quickly and easily define properties on your objects or within your constructor functions using getters, setters, and other descriptors.
Example in CoffeeScript and NodeJS:
Glorify = require 'glorify'
# Generic database for example purposes.
Database = require 'some-database'
class Foo
@properties
name:
get: -> Database.getProperty @, 'name'
set: ( value ) -> Database.setProperty @, 'name', value
age:
get: -> Database.getProperty @, 'age'
set: ( value ) -> Database.setProperty @, 'name', value
enumerable: yes
@readonly
type: 'Foobar'
constructor: ( @name, @age ) ->
# Example of a read-only property at
# construction time.
createdOn = Date.now()
# You can use the static Glorify method to chain
# 'properties' or 'readonly' calls in any order
Glorify( @ )
.readonly
createdOn: createdOn
.properties
upperCase:
get: -> @name.toUpperCase()
Glorify can be used either directly on a constructor function or as a static call on an arbitrary object.
Note: All examples below are in CoffeeScript.
Glorify = require 'glorify'
class Foo
@properties
name:
get: -> getNameFor @
set: ( value ) -> setNameFor @, value
age:
get: -> getAgeFor @
set: ( value ) -> setAgeFor @, value
guid:
get: -> getGuidFor @
enumerableProperty:
get: -> getEnumerablePropertyFor @
set: ( value ) -> setEnumerablePropertyFor @, value
enumerable: yes
@readonly
version: '0.0.1'
Glorify = require 'glorify'
foo = do ->
foo = {}
name = 'Blah'
createdOn = Date.now()
Glorify( @ )
.properties
name:
get: -> name
set: ( value ) -> name = value
uppercase:
get: -> name.toUpperCase()
.readonly
createdOn: createdOn
foo
Glorify = require 'glorify'
# For demonstration purposes, this is our database
DATABASE = {}
Glorify.add
database: ( property ) ->
entry = ( item ) -> DATABASE[item.guid]
enumerable: yes
get: -> entry( @ )[property]
set: ( value ) -> entry( @ )[property] = value
class Person
constructor: ( name, age ) ->
# Set up a fake GUID (just using a timestamp)
guid = Date.now()
DATABASE[guid] = {}
# Glorify our person
Glorify( @ )
.readonly
guid: guid
.database
name: name
age: age
bob = new Person 'Bob', 32
-
descriptors
: For each key indescriptors
, a function is added to the Glorify prototype, allowing it to be used in subsequentGlorify
calls. This function is passed one argument, the name of the property being accessed through aget
orset
call, and is expected to return a valid descriptor. Documentation on valid descriptors may be found at MDN.
The functions below have the same signature whether they are used on a constructor function or as a static call.
-
definitions
: An object whose keys become the property names and whose values are used as descriptors. Onlyget
,set
, andenumerable
descriptors are used.
-
definitions
: An object whose keys become the property names and whose values are used as the static values for read-only properties added to the instance.