-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathbase.coffee
75 lines (66 loc) · 2.53 KB
/
base.coffee
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
###
Base objects -----------------------------------------------------------------
###
# A base class which sets passed options as properties on itself.
class Brauhaus.OptionConstructor
# A mapping of parameter names to objects. When an option is encountered
# with a matching param name, it is instantiated as that object if it
# is not already an instance of the object. If it is an array, then each
# item in the array is instantiated or copied.
_paramMap: {}
constructor: (options) ->
# Convert JSON strings to objects
if typeof options is 'string'
options = JSON.parse options
# Set any properties passed in
keys = Object.keys(@_paramMap)
for own property of options
# Is this a property that requires a constructor?
if property in keys
# Don't construct null values
if options[property] is null then continue
# Is the property an arrary or a single object?
if options[property] instanceof Array
# Set the property to a mapped array, calling the
# constructor method to instantiate new objects
# if they are not already instances
@[property] = for item in options[property]
if item instanceof @_paramMap[property]
item
else
new @_paramMap[property](item)
else
# Set the property to an instance of the constructor
if options[property] instanceof @_paramMap[property]
@[property] = options[property]
else
@[property] = new @_paramMap[property](options[property])
else
@[property] = options[property]
###
Base class for new recipe ingredients. Each ingredient gets a name,
which defaults to 'New ' + the class name. For classes that inherit
Ingredient it will use their name, e.g:
###
class Brauhaus.Ingredient extends Brauhaus.OptionConstructor
constructor: (options) ->
# Set default name based on the class name
@name = 'New ' + @constructor.name
super(options)
# Check if a regex or list of regexes matches the name, returning
# either true/false or a value if the list has two items
nameRegex: (regex) ->
result = false
if typeof regex is 'string'
result = regex.exec(@name)
else
for item in regex
if Array.isArray(item) and item.length is 2
if item[0].exec(@name)
result = item[1]
break
else if typeof item is 'string'
result = item.exec(@name)
else
throw new Error('Invalid regex input!')
result