1 # Property Accessors Mixin [![Build Status](https://travis-ci.org/atom/property-accessors.svg?branch=master)](https://travis-ci.org/atom/property-accessors)
3 A mixin for defining dynamic properties.
7 To define a basic property accessor, use the `accessor` declaration. If you've
8 included the mixin into a class, you define a prototype property by calling
9 `@::accessor` on its prototype.
12 PropertyAccessors = require 'property-accessors'
15 PropertyAccessors.includeInto(this)
20 when 4 then 'sedan' # i know this isn't strictly accurate
24 when 'sedan' then @doorCount = 4
25 when 'coupe' then @doorCount = 2
32 You can define a class-level property by *extending* with the mixin rather than
33 including it (which extends the prototype).
37 PropertyAccessors.extend(this)
39 @accessor 'vehicleCount', get: -> @allVehicles.length
42 You can just pass a single function if you only want to define a getter:
46 PropertyAccessors.includeInto(this)
48 @::accessor 'type', -> # ...
55 Lazy accessors call a function the first time a property is accessed. You are
56 still free to overwrite this value by assigning the property explicitly.
60 PropertyAccessors.includeInto(this)
62 @::lazyAccessor 'crazyComputation', -> computeCrazyComputation()
67 Advised accessors allow you to call code before the reading or writing of a
68 property value. If a property is being assigned, your advice function is called
69 with the value being assigned and the old value.
73 @advisedAccessor 'online',
74 get: -> @ensureAllSystemsNominal()
75 set: -> @ensureUserIsSpy()
77 station = new SpyStation
78 station.online = true # ensures user is a spy, then assigns true
79 station.online # ensures all systems are nominal, then returns true