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
+27-27Lines changed: 27 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,25 +18,25 @@
18
18
<a href="https://github.com/codespaces/new?hide_repo_select=true&ref=main&repo=428442325" title="Open in GitHub Codespaces" ><img alt="Open in GitHub Codespaces" src="https://github.com/codespaces/badge.svg"></a>
19
19
</p>
20
20
21
-
A ruby library for the humanfriendly data format [NestedText](https://nestedtext.org/).
21
+
A Ruby library for the human-friendly data format [NestedText](https://nestedtext.org/).
22
22
23
-
<!-- Use URL to hosted image, so that it shows up at rubydocs.info as well. Using relative image and yardoc option "--asset img:img" did not work. -->
23
+
<!-- Use URL to host image, so that it shows up at rubydocs.info as well. Using the relative image and yardoc option "--asset img:img" did not work. -->
Provided is support for decoding a NestedText file or string to Ruby data structures, as well as encoding Ruby objects to a NestedText file or string. Furthermore there is support for serialization and deserialization of custom classes. The supported language version of the data format can be seen in the badge above. This implementation pass all the [official tests](https://github.com/KenKundert/nestedtext_tests).
26
+
Provided is support for decoding a NestedText file or string to Ruby data structures, as well as encoding Ruby objects to a NestedText file or string. Furthermore, there is support for serialization and deserialization of custom classes. The supported language version of the data format can be seen in the badge above. This implementation passes all the [official tests](https://github.com/KenKundert/nestedtext_tests).
27
27
28
-
This library is inspired by Ruby's stdlib modules `JSON` and `YAML` as well as the Python [reference implementation](https://github.com/KenKundert/nestedtext) of NestedText. Parsing is done with a LL(1) recursive descent parser and dumping with a recursive DFS traversal of the object references.
28
+
This library is inspired by Ruby's stdlib modules `JSON` and `YAML` as well as the Python [reference implementation](https://github.com/KenKundert/nestedtext) of NestedText. Parsing is done with an LL(1) recursive descent parser, and dumping is with a recursive DFS traversal of the object references.
29
29
30
30
> [!TIP]
31
31
> To make this library practically useful, you should pair it with a [schema validator](#schema).
32
32
33
33
# What is NestedText?
34
34
Citing from the official [introduction](https://nestedtext.org/en/latest/index.html) page:
35
-
> NestedText is a file format for holding structured data to be entered, edited, or viewed by people. It organizes the data into a nested collection of dictionaries, lists, and strings without the need for quoting or escaping. A unique feature of this file format is that it only supports one scalar type: strings. While the decision to eschew integer, real, date, etc. types may seem counter intuitive, it leads to simpler data files and applications that are more robust.
35
+
> NestedText is a file format for holding structured data to be entered, edited, or viewed by people. It organizes the data into a nested collection of dictionaries, lists, and strings without the need for quoting or escaping. A unique feature of this file format is that it only supports one scalar type: strings. While the decision to eschew integer, real, date, etc. types may seem counterintuitive, it leads to simpler data files and applications that are more robust.
36
36
>
37
37
> NestedText is convenient for configuration files, address books, account information, and the like. Because there is no need for quoting or escaping, it is particularly nice for holding code fragments.
38
38
39
-
*"Why do we need another data format?"* is the right question to ask. The answer is that the current popular formats (JSON, YAML, TOML, INI etc.) all have shortcomings which NestedText [addresses](https://nestedtext.org/en/latest/alternatives.html).
39
+
*"Why do we need another data format?"* is the right question to ask. The answer is that the current popular formats (JSON, YAML, TOML, INI, etc.) all have shortcomings which NestedText [addresses](https://nestedtext.org/en/latest/alternatives.html).
40
40
41
41
## Example
42
42
Here's a full-fledged example of an address book (from the official docs):
The type of the returned object depends on the toplevel type in the NestedText data and will be of corresponding native Ruby type. In the example above, `obj1` will be an `Array` and `obj2` will be `Hash` if `data.nt` looks like e.g.
82
+
The type of the returned object depends on the top-level type in the NestedText data and will be of the corresponding native Ruby type. In the example above, `obj1` will be an `Array` and `obj2` will be a`Hash` if `data.nt` looks like e.g.
83
83
84
84
```
85
85
key1: value1
86
86
key2: value2
87
87
```
88
88
89
-
Thus you must know what you're parsing, or test what you decoded after.
89
+
Thus, you must know what you're parsing, or test what you decoded after.
90
90
91
-
### Explicit TopLevel Type
91
+
### Explicit Top-Level Type
92
92
If you already know what you expect to have, you can guarantee that this is what you will get by telling either function what the expected top type is. If not, an error will be raised.
The strict mode determines how classes other than the basic types `String`, `Array` and `Hash` are handled during encoding and decoding. By **default** strict mode is **false**.
154
+
The strict mode determines how classes other than the basic types `String`, `Array`, and `Hash` are handled during encoding and decoding. By **default** strict mode is **false**.
155
155
156
156
With `strict: true`
157
157
Ruby | NestedText | Comment
@@ -194,10 +194,10 @@ class Apple
194
194
end
195
195
```
196
196
197
-
When an apple instance will be serialized e.g. by `apple.to_nt`, NestedText will call `Apple.encode_nt_with` if it exist and let the returned data be encoded to represent the instance.
197
+
When an Apple instance will be serialized, e.g., by `apple.to_nt`, NestedText will call `Apple.encode_nt_with` if it exists and let the returned data be encoded to represent the instance.
198
198
199
199
200
-
To be able to get this instance back when deserializing the NestedText there must be a class method `Class.nt_create(data)`. When deserializing NestedText and the class `Apple` is detected, and the method `#nt_create`exist on the class, it will be called with the decoded data belonging to it. This method should create and return a new instance of the class. In the most simple case it's just translating this to a call to `#new`.
200
+
To be able to get this instance back when deserializing the NestedText, there must be a class method `Class.nt_create(data)`. When deserializing NestedText and the class `Apple` is detected, and the method `#nt_create`exists on the class, it will be called with the decoded data belonging to it. This method should create and return a new instance of the class. In the simplest case, it's just translating this to a call to `#new`.
201
201
202
202
In full, the `Apple` class should look like:
203
203
@@ -228,7 +228,7 @@ data:
228
228
-12
229
229
```
230
230
231
-
If you want to add some more super powers to your custom class, you can add the `#to_nt` shortcut by including the `ToNTMixin`:
231
+
If you want to add some more superpowers to your custom class, you can add the `#to_nt` shortcut by including the `ToNTMixin`:
> See [encode_custom_classes_test.rb](test/nestedtext/encode_custom_classes_test.rb) for more real working examples.
255
255
256
256
# Schema
257
-
The point of NestedText is to not get in to business of supporting ambiguous types. That's why all values are simple strings. Having only simple strings is not useful in practice though. This is why NestedText is intended to be paired with a [Schema Validator](https://nestedtext.org/en/latest/schemas.html)!
257
+
The point of NestedText is not to get into to business of supporting ambiguous types. That's why all values are simple strings. Having only simple strings is not useful in practice, though. This is why NestedText is intended to be paired with a [Schema Validator](https://nestedtext.org/en/latest/schemas.html)!
258
258
259
259
A schema validator can:
260
260
* assert that the parsed values are of the expected types
261
261
* automatically convert them to Ruby class instances like Integer, Float, etc.
262
262
263
-
The reference implementation in Python [lists](https://nestedtext.org/en/latest/examples.html) a few examples of Python validators. Here below is an example of how this Ruby implementation of NestedText can be paired it with [RSchema](https://github.com/tomdalling/rschema).
263
+
The reference implementation in Python [lists](https://nestedtext.org/en/latest/examples.html)provides a few examples of Python validators. Below is an example of how this Ruby implementation of NestedText can be paired with [RSchema](https://github.com/tomdalling/rschema).
264
264
265
265
## Example with RSchema
266
266
The full and working example can be found at [erikw/nestedtext-ruby-test](https://github.com/erikw/nestedtext-ruby-test/blob/main/parse_validate.rb).
@@ -279,7 +279,7 @@ Let's say that you have a program that should connect to a few servers. The list
279
279
stable: false
280
280
```
281
281
282
-
After parsing this file with this NestedText library, the values for all keys will be string. But to make practical use of this, we would of course like the values for the `port` keys to be `Integer`, and `stable` should have a value of either `true` or `false`. RSchema can do this conversion for us!
282
+
After parsing this file with the NestedText library, the values for all keys will be strings. But to make practical use of this, we would of course like the values for the `port` keys to be `Integer`, and `stable` should have a value of either `true` or `false`. RSchema can do this conversion for us!
git clone https://github.com/erikw/nestedtext-ruby.git && cd $(basename "$_" .git)
340
340
```
341
-
1. Install a supported ruby version (see .gemspec) with a ruby version manager e.g. [rbenv](https://github.com/rbenv/rbenv), [asdf](http://asdf-vm.com/) or [RVM](https://rvm.io/rvm/install)
341
+
1. Install a supported Ruby version (see .gemspec) with a Ruby version manager e.g., [rbenv](https://github.com/rbenv/rbenv), [asdf](http://asdf-vm.com/) or [RVM](https://rvm.io/rvm/install)
342
342
1. run `$ scripts/setup` or `$ bundle install` to install dependencies
343
343
1. run `$ scripts/test` or `bundle exec rake test` to run the tests
344
344
1. You can also run `$ scripts/console` for an interactive prompt that will allow you to experiment.
345
-
1. For local testing, install the gem on local machine with: `$ bundle exec rake install`.
345
+
1. For local testing, install the gem on the local machine with: `$ bundle exec rake install`.
346
346
* or manually with `$ gem build *.gemscpec && gem install *.gem`
347
-
1. Watch changes on file system and execute tests with `$ bundle exec guard`.
347
+
1. Watch changes on the file system and execute tests with `$ bundle exec guard`.
348
348
349
349
350
350
Extra:
@@ -357,7 +357,7 @@ Extra:
357
357
* To see undocumented methods with [YARD](https://www.rubydoc.info/gems/yard/file/docs/GettingStarted.md): `$ yard stats --list-undoc`
358
358
359
359
# Releasing
360
-
Instructions for releasing on rubygems.org below. Optionally make a GitHub [release](https://github.com/erikw/nestedtext-ruby/releases) after this for the pushed git tag.
360
+
Instructions for releasing on rubygems.org are below. Optionally make a GitHub [release](https://github.com/erikw/nestedtext-ruby/releases) after this for the pushed git tag.
361
361
362
362
## (manually) Using bundler/gem_tasks rake tasks
363
363
Following instructions from [bundler.io](https://bundler.io/guides/creating_gem.html#releasing-the-gem):
@@ -410,5 +410,5 @@ Bug reports and pull requests are welcome on GitHub at [erikw/nestedtext-ruby](h
410
410
The gem is available as open source with the [License](./LICENSE.txt).
411
411
412
412
# Acknowledgments
413
-
* Thanks to the data format authors making it easier making new implementations by providing an [official test suite](https://github.com/KenKundert/nestedtext_tests).
413
+
* Thanks to the data format authors for making it easier to make new implementations by providing an [official test suite](https://github.com/KenKundert/nestedtext_tests).
414
414
* Thanks to [pixteller](https://pixteller.com/) & [mp4.to](https://mp4.to/webp/) for offering the tools needed for creating an animated logo.
0 commit comments