Adding the ability to add default property values.
This commit is contained in:
parent
a6df4d544b
commit
ba000379d7
2 changed files with 8 additions and 3 deletions
|
@ -26,6 +26,10 @@ To declare a property, you can use the `property` function. For example, for an
|
||||||
property Object.name
|
property Object.name
|
||||||
```
|
```
|
||||||
**NOTE**: Bash doesn't have a typing system, so you cannot set property types.
|
**NOTE**: Bash doesn't have a typing system, so you cannot set property types.
|
||||||
|
You can also set default values by adding a property after the declaration, e.g:
|
||||||
|
```bash
|
||||||
|
property Object.name "Example"
|
||||||
|
```
|
||||||
|
|
||||||
Class functions are declared the same way you would in bash, except it uses a prefix with object type. For example:
|
Class functions are declared the same way you would in bash, except it uses a prefix with object type. For example:
|
||||||
```bash
|
```bash
|
||||||
|
|
7
oop.sh
7
oop.sh
|
@ -173,17 +173,18 @@ _accessProperty() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Declares a property.
|
# Declares a property with an optional default value.
|
||||||
# Signature: (<string propertyFullName>)
|
# Signature: (<string propertyFullName>, [string propertyValue])
|
||||||
property() {
|
property() {
|
||||||
propertyFullName=$1
|
propertyFullName=$1
|
||||||
|
propertyValue=$2
|
||||||
# Split the name by ".". First element is variable name,
|
# Split the name by ".". First element is variable name,
|
||||||
# second is property name.
|
# second is property name.
|
||||||
propertyNames=($(echo $propertyFullName | tr "." "\n"))
|
propertyNames=($(echo $propertyFullName | tr "." "\n"))
|
||||||
varName=${propertyNames[0]}
|
varName=${propertyNames[0]}
|
||||||
prop=${propertyNames[1]}
|
prop=${propertyNames[1]}
|
||||||
# Default value
|
# Default value
|
||||||
eval "_${varName}_properties[$prop]=''"
|
eval "_${varName}_properties[$prop]='$propertyValue'"
|
||||||
# Property alias
|
# Property alias
|
||||||
eval "$propertyFullName() { _accessProperty $varName $prop \$@; }"
|
eval "$propertyFullName() { _accessProperty $varName $prop \$@; }"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue