nestedtext 2.1.0 → 3.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 830cd05483344cec4a5d4132a7b6a373c81e6e48494dbf22dd29ff5ff5985599
4
- data.tar.gz: 8ff4109630f482c63ddc9aac99de1a34f3dc3977c12af5a94eae266ad8a8a7d0
3
+ metadata.gz: 8dde068cab1260c8f534bdce49b3efea6fe1db7d30a73b180eff12d931d5a8f4
4
+ data.tar.gz: 325515db0115690e4aa62892e43445cc550d96346840f6404356214667b04de0
5
5
  SHA512:
6
- metadata.gz: 2e10ec7ae7e7d48c5a708be79803e8048322e474301d3e499c4bdee33199e7e99c4b1d3c8de2fd9ad6055d12332745c93c108d178ff829bdf1ddee1fad243d2a
7
- data.tar.gz: 247bd8a03e0c4433f10811523b1e7da9b2ac747f98c1d735a1a7efdeef23b6b8b38be360a88c82acdedba66c90f177102a04ecd85777c77cac9f9a092a3f86e2
6
+ metadata.gz: 1eab50ba68842d4c00011f18c3ef84d89ac848e9d7a63a821153690ba750b3f6ae707c0ef71dc211dd5c4c76b6272d1e670c0a9698db3d31fe80066500916f6f
7
+ data.tar.gz: efaafb7e53f31a6d9db3178befd6a9471a3677ff23122fbf1975018338c3ec35a04f12d61f4af05b9b2fd805bedf0b23f6c8fb2fa3dc12a59a5d3391ecdb1e33
data/CHANGELOG.md CHANGED
@@ -6,7 +6,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
- ## [2.1.0] - date
9
+ ## [3.2.1] - 2022-01-27
10
+ ### Fixed
11
+ - Fix logo at rubydoc.info
12
+
13
+ ## [3.2.0] - 2022-01-27
14
+ ### Changed
15
+ - Switch from rdoc formatting syntax to Markdown with Redcarpet to be able to render README.md properly.
16
+
17
+ ## [3.1.0] - 2022-01-27
18
+ ### Changed
19
+ - Switch from rdoc to YARD to match rubydoc.info that is used automatically for Gems uploaded to rubygems.org.
20
+
21
+ ## [3.0.0] - 2022-01-27
22
+ ### Added
23
+ - API documentation generated with rdoc.
24
+
25
+ ### Fixed
26
+ - Removed leaked `NT_MIXIN` constant in core extensions.
27
+
28
+ ### Changed
29
+ - **Breaking change**: `#to_nt` on `String`, `Array` and `Hash` is no longer strict by default for consistency an unexpected surprises e.g. when having an array of CustomObjects and calling the method on the array.
30
+ - Internal clean-up and simplifications on helper classes and methods.
31
+
32
+ ## [2.1.0] - 2022-01-27
10
33
  ### Changed
11
34
  - Slim down Gem by using include instead of block list.
12
35
 
data/README.md CHANGED
@@ -3,19 +3,20 @@
3
3
  [![Gem Downloads](https://ruby-gem-downloads-badge.herokuapp.com/nestedtext?color=brightgreen&type=total&label=gem%20downloads)](https://rubygems.org/gems/nestedtext)
4
4
  [![Documentation](https://img.shields.io/badge/docs-API-informational?logo=readthedocs&logoColor=violet)](https://www.rubydoc.info/gems/nestedtext/)
5
5
  [![Data Format Version Supported](https://img.shields.io/badge/%F0%9F%84%BD%F0%9F%85%83%20Version%20Supported-3.2.1-blueviolet)](https://nestedtext.org/en/v3.2/)
6
- [![Official Tests](https://img.shields.io/badge/%F0%9F%8F%81%20Official%20Tests-Passing-success)](https://github.com/KenKundert/nestedtext_tests/tree/585e95a73d94ac1f48e71a154e2db0ab67cf30fa)
6
+ [![Official Tests](https://img.shields.io/badge/Official%20Tests-Passing-success?logo=cachet)](https://github.com/KenKundert/nestedtext_tests/tree/585e95a73d94ac1f48e71a154e2db0ab67cf30fa)
7
7
  [![GitHub Actions: Continuous Integration](https://github.com/erikw/nestedtext-ruby/actions/workflows/ci.yml/badge.svg)](https://github.com/erikw/nestedtext-ruby/actions/workflows/ci.yml)
8
8
  [![GitHub Actions: Continuous Deployment](https://github.com/erikw/nestedtext-ruby/actions/workflows/cd.yml/badge.svg)](https://github.com/erikw/nestedtext-ruby/actions/workflows/cd.yml)
9
9
  [![GitHub Actions: CodeQL Analysis](https://github.com/erikw/nestedtext-ruby/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/erikw/nestedtext-ruby/actions/workflows/codeql-analysis.yml)
10
10
  [![Code Climate Maintainability](https://api.codeclimate.com/v1/badges/8409b6cdc3dc62a33f6f/maintainability)](https://codeclimate.com/github/erikw/nestedtext-ruby/maintainability)
11
11
  [![Code Climate Test Coverage](https://api.codeclimate.com/v1/badges/8409b6cdc3dc62a33f6f/test_coverage)](https://codeclimate.com/github/erikw/nestedtext-ruby/test_coverage)
12
- [![SLOC](https://img.shields.io/tokei/lines/github/erikw/nestedtext-ruby)](#)
12
+ [![SLOC](https://img.shields.io/tokei/lines/github/erikw/nestedtext-ruby?logo=codefactor&logoColor=lightgrey)](#)
13
13
  [![License](https://img.shields.io/github/license/erikw/nestedtext-ruby?color=informational)](LICENSE.txt)
14
14
  [![OSS Lifecycle](https://img.shields.io/osslifecycle/erikw/nestedtext-ruby)](https://github.com/Netflix/osstracker)
15
15
 
16
16
 
17
17
  A ruby library for the human friendly data format [NestedText](https://nestedtext.org/).
18
18
 
19
+ <!-- Use URL to hosetd image, so that it shows up at rubydocs.info as well. Using relative image and yardoc option "--asset img:img" did not work. -->
19
20
  <a href="#" ><img src="https://raw.githubusercontent.com/erikw/nestedtext-ruby/main/img/logo.webp" align="right" width="420px" alt="Project logo" /></a>
20
21
 
21
22
  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 see in the badge above. This implementation pass all the [official tests](https://github.com/KenKundert/nestedtext_tests).
@@ -56,7 +57,7 @@ vice president:
56
57
  See the [language introduction](https://nestedtext.org/en/latest/basic_syntax.html) for more details.
57
58
 
58
59
  # Usage
59
- The full documentation can be found at [TODO](TODO). A minimal & fully working example of a project using this library can be found at [erikw/nestedtext-ruby-test](https://github.com/erikw/nestedtext-ruby-test).
60
+ The full API documentation can be found at [**rubydocs.info**](https://www.rubydoc.info/gems/nestedtext/). A minimal & fully working example of a project using this library can be found at [erikw/nestedtext-ruby-test](https://github.com/erikw/nestedtext-ruby-test).
60
61
 
61
62
  ## Decoding (reading NT)
62
63
  This is how you can decode NestedText from a string or directly from a file (`*.nt`) to Ruby object instances:
@@ -78,17 +79,6 @@ key1: value1
78
79
  key2: value2
79
80
  ```
80
81
 
81
- The NestedText types maps like this to Ruby:
82
-
83
- [NestedText](https://nestedtext.org/en/latest/basic_syntax.html) | Ruby | Comment
84
- ---|---|---
85
- `String` | `String` |
86
- `List` | `Array` |
87
- `Dictionary`| `Hash` |
88
- `String` | `Symbol` | when `strict: true`, otherwise Ruby Symbols are encoded as Custom Class (see below).
89
- *empty* | `nil` | when `strict: true`, otherwise as Custom Class. How empty strings and nil are handled depends on where it is used. This library follows how the official implementation does it.
90
-
91
-
92
82
  Thus you must know what you're parsing, or test what you decoded.
93
83
 
94
84
  ### Explicit Top Level Type
@@ -119,11 +109,10 @@ ntstr = NestedText::dump(data)
119
109
  NestedText::dump_file(data, "path/to/data.nt")
120
110
  ```
121
111
 
122
-
123
112
  ### `#to_nt` Convenience
124
113
  To make it more convenient, the Ruby Core is extended with a `#to_nt` method on the supported types that will dump a String of the data structure. Here's an IRB session showing how it works:
125
114
 
126
- ```irb
115
+ ```ruby
127
116
  irb> require 'nestedtext'
128
117
  irb> puts "a\nstring".to_nt
129
118
  > a
@@ -132,7 +121,10 @@ irb> puts ["i1", "i2", "i3"].to_nt
132
121
  - i1
133
122
  - i2
134
123
  - i3
135
- irb> puts({"k1" => "v1", "multiline\nkey" => "v2", "k3" => ["a", "list"]}.to_nt)
124
+ irb> hash = {"k1" => "v1",
125
+ "multiline\nkey" => "v2",
126
+ "k3" => ["a", "list"]}
127
+ irb> puts hash.to_nt
136
128
  k1: v1
137
129
  : multiline
138
130
  : key
@@ -142,9 +134,46 @@ k3:
142
134
  - list
143
135
  ```
144
136
 
137
+ ## Types
138
+ Ruby classes maps like this to NestedText types:
139
+ Ruby | [NestedText](https://nestedtext.org/en/latest/basic_syntax.html)
140
+ ---|---
141
+ `String` |`String`
142
+ `Array` |`List`
143
+ `Hash` |`Dictionary`
144
+
145
+
146
+ ### Strict Mode
147
+ 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 turned **off**.
148
+
149
+ With `strict: true`
150
+ Ruby | NestedText | Comment
151
+ ---|---|---
152
+ `nil` |*empty* | (1.)
153
+ `Symbol` |`String` | Raises `NestedText::Error`
154
+ Other Class | -- | Raises `NestedText::Error`
155
+
156
+
157
+ With `strict: false`
158
+ Ruby | NestedText | Comment
159
+ ---|---|---
160
+ `nil` | *Custom Class Encoding* | (1.)
161
+ `Symbol` | `String` |
162
+ Custom Class | *Custom Class Encoding* | If the [Custom Class](#custom-classes-serialization) implements `#encode_nt_with` (2.)
163
+ Other Class | String | `#to_s` will be called if there is no `#encode_nt_with`
164
+
165
+
166
+ * (1.) How empty strings and nil are handled depends on where it is used. This library follows how the official implementation does it.
167
+
168
+
169
+
170
+
171
+
145
172
  ## Custom Classes Serialization
146
173
  This library has support for serialization/deserialization of custom classes as well. This is done by letting the objects tell NestedText what data should be used to represent the object instance with the `#encode_nt_with` method (inspired by `YAML`'s `#encode_with` method). All objects being recursively referenced from a root object being serialized must either implement this method or be one of the core supported NestedText data types from the table above.
147
174
 
175
+ A class implementing `#encode_nt_with` is refered to as `Custom Class` in this document.
176
+
148
177
  ```ruby
149
178
  class Apple
150
179
  def initialize(type, weight)
@@ -191,7 +220,6 @@ data:
191
220
  - granny smith
192
221
  - 12
193
222
  ```
194
- Note that the special key to denote the class name is subject to change in future versions and you **must not** rely on it.
195
223
 
196
224
  If you want to add some more super powers to your custom class, you can add the `#to_nt` shortcut by including the `NTEncodeMixin`:
197
225
  ```ruby
@@ -204,13 +232,16 @@ Apple.new("granny smith", 12).to_nt
204
232
  ```
205
233
 
206
234
 
207
- **NOTE** that when deserializing a custom class, this custom class must be available when calling the `#dump*` methods e.g.
208
- ```ruby
209
- require 'nestedtext'
210
- require_relative 'apple' # This is needed if Apple is defined in apple.rb and not in this scope already.
235
+ **Important notes**:
236
+ * The special key to denote the class name is subject to change in future versions and you **must not** rely on it.
237
+ * Custom Classes **can not be a key** in a Hash. Trying to do this will raise an Error.
238
+ * When deserializing a custom class, this custom class must be available when calling the `#dump*` methods e.g.
239
+ ```ruby
240
+ require 'nestedtext'
241
+ require_relative 'apple' # This is needed if Apple is defined in apple.rb and not in this scope already.
211
242
 
212
- NestedText::load_file('path/to/apple_dump.nt')
213
- ```
243
+ NestedText::load_file('path/to/apple_dump.nt')
244
+ ```
214
245
 
215
246
  See [encode_custom_classes_test.rb](test/nestedtext/encode_custom_classes_test.rb) for more real working examples.
216
247
 
@@ -1,19 +1,28 @@
1
1
  require "nestedtext/encode_helpers"
2
2
 
3
+ # Extension of Ruby core types with the NestedText::NTEncodeMixin.
4
+ #
3
5
  # TODO: add encoding of more Ruby native classes like Integer, Float etc plus commons like Set,....? Not covered in NestedText language.
4
6
  # Or leave this to a schema validator 3rd party plugin maybe? And replace my custom class decoding (and also encoding?)?
5
7
  # Or both: add encoding/decoding of more native classes, and allow decoding + applying a schema with 3rd party.
6
8
  # Or encourage using Marshal from core?
7
9
 
8
- NT_MIXIN = NestedText.const_get(:NTEncodeStrictMixin)
9
- class String include NT_MIXIN end
10
- class Array include NT_MIXIN end
11
- class Hash include NT_MIXIN end
10
+ # Extended with the `#to_nt` method.
11
+ class String include NestedText::NTEncodeMixin; end
12
12
 
13
+ # Extended with the `#to_nt` method.
14
+ class Array include NestedText::NTEncodeMixin; end
15
+
16
+ # Extended with the `#to_nt` method.
17
+ class Hash include NestedText::NTEncodeMixin; end
18
+
19
+ # Extended with NestedText support.
13
20
  class NilClass
14
- include NT_MIXIN
21
+ include NestedText::NTEncodeMixin
15
22
 
23
+ # Adds support for encoding and decoding nil.
16
24
  def self.nt_create(_data) = nil
17
25
 
26
+ # Adds support for encoding and decoding nil.
18
27
  def encode_nt_with() = ""
19
28
  end
@@ -1,18 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "nestedtext/parser"
4
- require "nestedtext/errors"
4
+ require "nestedtext/errors_internal"
5
5
 
6
6
  require "logger"
7
7
  require "stringio"
8
8
 
9
9
  module NestedText
10
+ # Decode a NestedText string to Ruby objects.
11
+ #
12
+ # @param ntstring [String] The string containing NestedText to be decoded.
13
+ # @param top_class [String] Force the top level returned object to be of this type. Supported values are `Object`, `Array`, `Hash` and `String`.
14
+ # @param strict [Boolean] If strict mode should be used.
15
+ #
16
+ # @return [Object, nil] The parsed object.
17
+ #
18
+ # @raise [NestedText::Error] if anything went wrong.
10
19
  def self.load(ntstring, top_class: Object, strict: false)
11
20
  raise Errors::WrongInputTypeError.new([String], ntstring) unless ntstring.nil? || ntstring.is_a?(String)
12
21
 
13
22
  Parser.new(StringIO.new(ntstring), top_class, strict: strict).parse
14
23
  end
15
24
 
25
+ # Decode a NestedText stored in a given file.
26
+ #
27
+ # @param filename [String] The file path to read NestedText to decode from.
28
+ # @param top_class [String] Force the top level returned object to be of this type. Supported values are `Object`, `Array`, `Hash` and `String`.
29
+ # @param strict [Boolean] If strict mode should be used.
30
+ #
31
+ # @return [Object, nil] The parsed object.
32
+ #
33
+ # @raise [NestedText::Error] if anything went wrong.
34
+ # @raise [IOError] on issue opening `filename` for reading in text mode.
16
35
  def self.load_file(filename, top_class: Object, strict: false)
17
36
  raise Errors::WrongInputTypeError.new([String], filename) unless !filename.nil? && filename.is_a?(String)
18
37
 
@@ -92,7 +92,6 @@ module NestedText
92
92
  end
93
93
  end
94
94
 
95
- # TODO: document that @strict==false allows to_s on key object
96
95
  def dump_hash(obj, depth: 0, **kwargs)
97
96
  rep = if obj.empty?
98
97
  "{}"
@@ -1,16 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "nestedtext/errors"
4
3
  require "nestedtext/dumper"
5
-
6
- # Model after JSON
7
- # NestedText.dump(obj, io=nil) => dumps to string, or to IO if given
8
- # NestedText.dump_file(obj, filename)
4
+ require "nestedtext/errors_internal"
9
5
 
10
6
  module NestedText
7
+ # Encode a Ruby object to a NestedText string.
8
+ #
9
+ # @param obj [Object] The object to encode to NestedText.
10
+ # @param io [IO] Additionally write the output to this IO object. The caller is responsible for that the IO is closed after the call to this method.
11
+ # @param indentation [Integer] The indentation of nested levels to use.
12
+ # @param strict [Boolean] If strict mode should be used.
13
+ #
14
+ # @return A String containing NestedText data.
15
+ # @raise [NestedText::Error] if anything went wrong.
16
+ # @raise Whatever the `io` can raise, if supplied.
11
17
  def self.dump(obj, io: nil, indentation: 4, strict: false)
12
- # io - additionaly write the out result to IO and still return result.
13
-
14
18
  raise Errors::DumpBadIOError, io unless io.nil? || io.respond_to?(:write) && io.respond_to?(:fsync)
15
19
 
16
20
  dumper = Dumper.new(indentation, strict)
@@ -22,6 +26,18 @@ module NestedText
22
26
  dumper.dump obj
23
27
  end
24
28
 
29
+ # Encode a Ruby object to a NestedText file.
30
+
31
+ # Apart from `filename`, this method behaves exactly like dump.
32
+ #
33
+ # @param (see dump)
34
+ # @param filename [String] The file path to write the NestedText result to. The conventional file extension is `.nt`.
35
+ #
36
+ #
37
+ # @return (see dump)
38
+ # @raise (see dump)
39
+ # @raise [IOError] on issues opening the `filename` for writing in text mode.
40
+ #
25
41
  def self.dump_file(obj, filename, **kwargs)
26
42
  raise Errors::DumpFileBadPathError, filename unless filename.is_a? String
27
43
 
@@ -1,16 +1,14 @@
1
1
  require "nestedtext/dumper"
2
2
 
3
3
  module NestedText
4
- module NTEncodeStrictMixin
5
- def to_nt(**kwargs)
6
- NestedText.dump(self, strict: true, **kwargs)
7
- end
8
- end
9
- private_constant :NTEncodeStrictMixin
10
-
4
+ # A mixin for Custom Classes to get the to_nt shortcut.
5
+ # TODO rename to: ToNTMixin
11
6
  module NTEncodeMixin
7
+ # Encode this object to a NestedText string.
8
+ #
9
+ # This method takes the same named arguments as {NestedText.dump}.
12
10
  def to_nt(**kwargs)
13
- NestedText.dump(self, **kwargs)
11
+ NestedText.dump(self, strict: false, **kwargs)
14
12
  end
15
13
  end
16
14
  end
@@ -0,0 +1,8 @@
1
+ module NestedText
2
+ # Top level error to rescue on.
3
+ #
4
+ # Error is a subclass of StandardError and behaves as expected e.g. #message.
5
+ class Error < StandardError
6
+ private_class_method :new
7
+ end
8
+ end
@@ -4,13 +4,9 @@ require "word_wrap"
4
4
  require "word_wrap/core_ext"
5
5
 
6
6
  require "nestedtext/constants"
7
+ require "nestedtext/error"
7
8
 
8
9
  module NestedText
9
- # Top level error for users to rescue on.
10
- class Error < StandardError
11
- private_class_method :new
12
- end
13
-
14
10
  module Errors
15
11
  class InternalError < Error
16
12
  public_class_method :new # Prevent users from instansiating.
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "stringio"
4
4
 
5
- require "nestedtext/errors"
5
+ require "nestedtext/errors_internal"
6
6
  require "nestedtext/scanners"
7
7
  require "nestedtext/constants"
8
8
 
@@ -15,7 +15,6 @@ module NestedText
15
15
  end
16
16
  end
17
17
 
18
- # TODO: document that caller is responsible for closing IO after done with Parser.
19
18
  def initialize(io, top_class, strict: false)
20
19
  assert_valid_input_type io
21
20
  Parser.assert_valid_top_level_type(top_class)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "nestedtext/errors"
3
+ require "nestedtext/errors_internal"
4
4
 
5
5
  module NestedText
6
6
  class LineScanner
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NestedText
4
- VERSION = "2.1.0"
4
+ # The version of this library.
5
+ VERSION = "3.2.1"
5
6
  end
data/lib/nestedtext.rb CHANGED
@@ -1,9 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "nestedtext/encode"
4
- require_relative "nestedtext/decode"
5
3
  require_relative "nestedtext/core_ext"
4
+ require_relative "nestedtext/decode"
5
+ require_relative "nestedtext/encode"
6
+ require_relative "nestedtext/encode_helpers"
7
+ require_relative "nestedtext/error"
6
8
  require_relative "nestedtext/version"
7
9
 
10
+ ##
11
+ # # NestedText
12
+ # The main module in this library to use.
13
+ #
14
+ # See {file:README.md} for documentation on Types, Strict Mode and Custom Classes.
8
15
  module NestedText
9
16
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nestedtext
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 3.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Westrup
@@ -62,7 +62,8 @@ files:
62
62
  - lib/nestedtext/dumper.rb
63
63
  - lib/nestedtext/encode.rb
64
64
  - lib/nestedtext/encode_helpers.rb
65
- - lib/nestedtext/errors.rb
65
+ - lib/nestedtext/error.rb
66
+ - lib/nestedtext/errors_internal.rb
66
67
  - lib/nestedtext/parser.rb
67
68
  - lib/nestedtext/scanners.rb
68
69
  - lib/nestedtext/version.rb