You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+44-17Lines changed: 44 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,64 +2,78 @@
2
2
3
3
Obj is an [Ash](https://github.com/ash-shell/ash) module that adds object support to Bash.
4
4
5
+
> If you want to first get excited about this before reading the entire README, jump to [this section](#class-example).
6
+
5
7
# Getting started
6
8
7
-
## Ash Users
9
+
Obj is part of the Ash core, so you can just start using it in your Ash modules.
8
10
9
-
Obj is part of the Ash core, so you can immediately start using it within your Ash modules.
11
+
For an example of an ash module that uses objects, [look here](https://github.com/BrandonRomano/ash-obj-examples).
10
12
11
-
Simply place your Classes in a directory named `classes` in the root of your modules directory, and start using them!
13
+
# Features + Usage
12
14
13
-
For an example of an ash module that uses objects, [look here](https://github.com/BrandonRomano/ash-obj-examples).
15
+
## Imports
14
16
15
-
## Non Ash Users
17
+
Before we can start using any objects, we must first import them.
16
18
17
-
Even if you're not an Ash user, this module can be used as a library.
19
+
If the objects you want to use are in your current module, they are already imported for you.
18
20
19
-
Just include the `obj.sh` library file in your script, and also point to where you're going to be keeping your classes.
21
+
If the objects you want to use are outside of your current module, you must import them. You can import an external module with `Obj__import`:
20
22
21
-
The start of your script will look something like this:
23
+
```bash
24
+
Obj__import "$package_to_import""$package_alias"
25
+
```
26
+
27
+
`$package_to_import` is the modules package (as specified in its `ash_config.yaml` file), while `$package_alias` is the alias in which we will refer to the newly imported package.
28
+
29
+
If I have `https://github.com/BrandonRomano/ash-obj-examples` installed and would like to import its classes in a different module, I would this following line to my module:
> External modules must first be installed before importing them. See [apm](https://github.com/ash-shell/apm).
29
36
30
37
## Creating Classes
31
38
32
39
Before getting into any details, as you would expect classes are the definition of what an object is.
33
40
34
-
Classes must be placed in the directory defined to hold classes (for Ash users, this is in a directory named `classes` in the root of your module). Classes also must be named as their classname with `.sh` as their extension. By convention, you should use [PascalCase](http://c2.com/cgi/wiki?PascalCase) for class naming as many modern programming languages use.
41
+
Classes must be placed in the directory named `classes` in the root of a module. Classes also must be named as their classname with `.sh` as their extension. By convention, you should use [PascalCase](http://c2.com/cgi/wiki?PascalCase) for class naming as many modern programming languages use.
35
42
36
43
> For example, if I were to create a class that would define what a Person object is, I would name the file `Person.sh`.
37
44
45
+
### Class Example
46
+
38
47
I'll explain the different components of what a class is below, but here is what a class looks like. This would be in a file named Person.sh in our classes diretory:
39
48
40
-
> For a fully commented version of this class, look [here](https://github.com/ash-shell/obj-examples/blob/master/classes/Person.sh)
49
+
> For a fully commented version of this class, look [here](https://github.com/BrandonRomano/ash-obj-examples/blob/master/classes/Person.sh)
41
50
42
51
```bash
43
52
#!/bin/bash
44
53
# This is a simple class that represents a Person.
@@ -101,7 +115,7 @@ A private method is a function denoted by the Class name followed by a single un
101
115
102
116
### Constructors
103
117
104
-
Constructors are magically named funcitons that get called when an object is initialized. Constructors can take an arbitrary amount of parameters, and have all of the same powers as a public method (as it technically is a public method).
118
+
Constructors are magically named funcitons that get called when an object is initialized. Constructors can take an arbitrary amount of parameters, and have all of the same powers as a public method (as it technically is just a public method).
105
119
106
120
A constructor is a function denoted by the class name followed by `__construct`.
Now I have a reference to `$brandon`, which is a pointer to a initialized object representing myself.
136
150
151
+
##### From an External Package
152
+
153
+
If I wanted to create an object from an external package that I've imported, we must specify the package alias in `Obj__alloc`, in `alias.ClassName` format:
brandon=$(Obj__alloc "objex.Person")# Note the `objex.` before the class name
161
+
Obj__init $brandon 1 "Brandon" 23
162
+
```
163
+
164
+
> When you're creating an object from the current context, you could actually say `this.ClassName` - Although, it isn't necessisary because if you don't specify a package, `Obj__alloc` by default assumes you mean `this`.
165
+
137
166
##### A quick Obj__init Gotcha
138
167
139
168
It's worth nothing that if you use `Obj__init` within a subshell, the object will only be initialized within the scope of that subshell. Your best bet is to not wrap this call in a subshell unless you really know what you're doing. However, we are allowed to pass objects into subshells after they have been initialized. This follows the same rules as variables in a subshell:
@@ -151,7 +180,6 @@ If I were to run `Obj__dump $brandon`, immediately after initializing the object
151
180
| age=23
152
181
| id=3
153
182
| name='Brandon Romano'
154
-
=====================================
155
183
```
156
184
157
185
As you can see, `Obj__dump` prints out all of the public variables in an object.
@@ -183,7 +211,6 @@ An `Obj__dump` on our `$brandon` object would now yield:
183
211
| age=23
184
212
| id=3
185
213
| name='Brandon Romano'
186
-
=====================================
187
214
```
188
215
189
216
#### Accessing Member Variables (Obj__get)
@@ -211,7 +238,7 @@ Brandon Romano
211
238
212
239
### Calling Public Methods
213
240
214
-
Inside of a class, methods are called with the full name of the function (e.g. `Person__make_older`).
241
+
Inside of a class, all methods are called with the full name of the function (e.g. `Person__make_older`).
215
242
216
243
Outside of the class, this is different, and public methods must be called via `Obj__call`.
0 commit comments