aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/robertkrimen/otto/registry/registry.go
blob: 966638ac4c168403725c7b703aecf4974be277fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Package registry is an expirmental package to facillitate altering the otto runtime via import.

This interface can change at any time.
*/
package registry

var registry []*Entry = make([]*Entry, 0)

type Entry struct {
    active bool
    source func() string
}

func newEntry(source func() string) *Entry {
    return &Entry{
        active: true,
        source: source,
    }
}

func (self *Entry) Enable() {
    self.active = true
}

func (self *Entry) Disable() {
    self.active = false
}

func (self Entry) Source() string {
    return self.source()
}

func Apply(callback func(Entry)) {
    for _, entry := range registry {
        if !entry.active {
            continue
        }
        callback(*entry)
    }
}

func Register(source func() string) *Entry {
    entry := newEntry(source)
    registry = append(registry, entry)
    return entry
}