]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/node_modules/event-kit/node_modules/grim/node_modules/emissary/node_modules/property-accessors/README.md
6ef1e047cfe3ce1eb50f39e21c83d8d8b5aa40bf
[rbdr/dotfiles] / atom / packages / ex-mode / node_modules / event-kit / node_modules / grim / node_modules / emissary / node_modules / property-accessors / README.md
1 # Property Accessors Mixin [![Build Status](https://travis-ci.org/atom/property-accessors.svg?branch=master)](https://travis-ci.org/atom/property-accessors)
2
3 A mixin for defining dynamic properties.
4
5 ## Basic Usage
6
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.
10
11 ```coffee
12 PropertyAccessors = require 'property-accessors'
13
14 class Vehicle
15 PropertyAccessors.includeInto(this)
16
17 @::accessor 'type',
18 get: ->
19 switch @doorCount
20 when 4 then 'sedan' # i know this isn't strictly accurate
21 when 2 then 'coupe'
22 set: (type) ->
23 switch type
24 when 'sedan' then @doorCount = 4
25 when 'coupe' then @doorCount = 2
26
27 car = new Vehicle
28 car.doorCount = 2
29 car.type # => 'coupe'
30 ```
31
32 You can define a class-level property by *extending* with the mixin rather than
33 including it (which extends the prototype).
34
35 ```coffee
36 class Vehicle
37 PropertyAccessors.extend(this)
38
39 @accessor 'vehicleCount', get: -> @allVehicles.length
40 ```
41
42 You can just pass a single function if you only want to define a getter:
43
44 ```coffee
45 class Vehicle
46 PropertyAccessors.includeInto(this)
47
48 @::accessor 'type', -> # ...
49 ```
50
51 ## Fancy Usage
52
53 ### Lazy Accessors
54
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.
57
58 ```coffee
59 class ScienceLab
60 PropertyAccessors.includeInto(this)
61
62 @::lazyAccessor 'crazyComputation', -> computeCrazyComputation()
63 ```
64
65 ### Advised Accessors
66
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.
70
71 ```coffee
72 class SpyStation
73 @advisedAccessor 'online',
74 get: -> @ensureAllSystemsNominal()
75 set: -> @ensureUserIsSpy()
76
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
80 ```