nestedtext 3.2.1 → 4.2.0

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: 8dde068cab1260c8f534bdce49b3efea6fe1db7d30a73b180eff12d931d5a8f4
4
- data.tar.gz: 325515db0115690e4aa62892e43445cc550d96346840f6404356214667b04de0
3
+ metadata.gz: 9e3fcbca130327597aa3a6f2205d5328ddf407a916c5a071906686b5013fca52
4
+ data.tar.gz: f143d0efe77a919566360830209e8ab36b8a0205feb0466c8c00b55a598da904
5
5
  SHA512:
6
- metadata.gz: 1eab50ba68842d4c00011f18c3ef84d89ac848e9d7a63a821153690ba750b3f6ae707c0ef71dc211dd5c4c76b6272d1e670c0a9698db3d31fe80066500916f6f
7
- data.tar.gz: efaafb7e53f31a6d9db3178befd6a9471a3677ff23122fbf1975018338c3ec35a04f12d61f4af05b9b2fd805bedf0b23f6c8fb2fa3dc12a59a5d3391ecdb1e33
6
+ metadata.gz: 1835a85442d3080bb1084aeed972d787f12325b88f99c50528858596d4918eef73101eb9861fe7a78102a17f9ed27551034a8a237f55143f26d2e74deac3b9e7
7
+ data.tar.gz: 9998dd2a1d1a1afff99b5cc587a35e6cc240fe38a74c2b19fcb6dd38f3d907c414e1a644a8891832049cfa770eb2a047054e447f48bebbd4481e027eb0ad079f
data/CHANGELOG.md CHANGED
@@ -6,6 +6,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [4.2.0] - 2022-02-08
10
+ ### Fixed
11
+ - Proper Unicode character name lookup.
12
+
13
+ ## [4.1.1] - 2022-01-28
14
+ ### Fixed
15
+ - Don't trigger CI when CD will run all tests anyways.
16
+
17
+ ## [4.1.0] - 2022-01-28
18
+ ### Changed
19
+ - cd.yml now runs full tests before releasing new version, by using reusable workflows.
20
+
21
+ ## [4.0.0] - 2022-01-28
22
+ ### Changed
23
+ - **Breaking change**: Renamed `NTEncodeMixin` to `ToNTMixin`.
24
+ - All code linted with RuboCop
25
+
9
26
  ## [3.2.1] - 2022-01-27
10
27
  ### Fixed
11
28
  - Fix logo at rubydoc.info
@@ -26,7 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
26
43
  - Removed leaked `NT_MIXIN` constant in core extensions.
27
44
 
28
45
  ### 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.
46
+ - **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 Custom Objects and calling the method on the array.
30
47
  - Internal clean-up and simplifications on helper classes and methods.
31
48
 
32
49
  ## [2.1.0] - 2022-01-27
@@ -49,7 +66,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
49
66
 
50
67
  ## [1.1.1] - 2022-01-25
51
68
  ### Fixed
52
- - Renamed `NTEncodeMixing` to `NTEncodeMixin` .
69
+ - Renamed `ToNTMixing` to `ToNTMixin` .
53
70
 
54
71
  ## [1.1.0] - 2022-01-25
55
72
  ### Added
@@ -70,13 +87,13 @@ The library is now useful for users!
70
87
  - Publish Gem to GitHub Packages
71
88
 
72
89
  ## [0.4.0] - 2022-01-24
73
- - Iteration on CD GitHub Actins workflow.
90
+ - Iteration on CD GitHub Actions workflow.
74
91
 
75
92
  ## [0.3.0] - 2022-01-24
76
- - Iteration on CD GitHub Actins workflow.
93
+ - Iteration on CD GitHub Actions workflow.
77
94
 
78
95
  ## [0.2.0] - 2022-01-24
79
- - Iteration on CD GitHub Actins workflow.
96
+ - Iteration on CD GitHub Actions workflow.
80
97
 
81
98
  ## [0.1.0] - 2022-01-24
82
99
  ### Added
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # NestedText Ruby Library [![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=NestedText,%20the%20human%20friendly%20data%20format,%20has%20a%20now%20a%20ruby%20library%20for%20easy%20encoding%20and%20decoding&url=https://github.com/erikw/nestedtext-ruby&via=erik_westrup&hashtags=nestedtext,ruby,gem)
2
2
  [![Gem Version](https://badge.fury.io/rb/nestedtext.svg)](https://badge.fury.io/rb/nestedtext)
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
- [![Documentation](https://img.shields.io/badge/docs-API-informational?logo=readthedocs&logoColor=violet)](https://www.rubydoc.info/gems/nestedtext/)
4
+ [![Documentation](https://img.shields.io/badge/docs-API-informational?logo=readthedocs&logoColor=violet)](https://www.rubydoc.info/gems/nestedtext/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/Official%20Tests-Passing-success?logo=cachet)](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/)
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)
@@ -16,13 +16,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. -->
20
- <a href="#" ><img src="https://raw.githubusercontent.com/erikw/nestedtext-ruby/main/img/logo.webp" align="right" width="420px" alt="Project logo" /></a>
19
+ <!-- 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. -->
20
+ <a href="#" ><img src="https://raw.githubusercontent.com/erikw/nestedtext-ruby/main/img/logo.webp" align="right" width="420px" alt="nestedtext-ruby logo" /></a>
21
21
 
22
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).
23
23
 
24
24
  This library is inspired Ruby 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.
25
25
 
26
+ To make this library practically useful, you should pair it with a [schema validator](#schema).
27
+
26
28
  # What is NestedText?
27
29
  Citing from the official [introduction](https://nestedtext.org/en/latest/index.html) page:
28
30
  > 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.
@@ -57,7 +59,7 @@ vice president:
57
59
  See the [language introduction](https://nestedtext.org/en/latest/basic_syntax.html) for more details.
58
60
 
59
61
  # Usage
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).
62
+ The **full API documentation** can be found at [rubydocs.info](https://www.rubydoc.info/gems/nestedtext/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).
61
63
 
62
64
  ## Decoding (reading NT)
63
65
  This is how you can decode NestedText from a string or directly from a file (`*.nt`) to Ruby object instances:
@@ -72,14 +74,14 @@ obj1 = NestedText::load(ntstr)
72
74
  obj2 = NestedText::load_file("path/to/data.nt")
73
75
  ```
74
76
 
75
- The type of the returned object depends on the top level 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.
77
+ The type of the returned object depends on the top level 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.
76
78
 
77
79
  ```
78
80
  key1: value1
79
81
  key2: value2
80
82
  ```
81
83
 
82
- Thus you must know what you're parsing, or test what you decoded.
84
+ Thus you must know what you're parsing, or test what you decoded after.
83
85
 
84
86
  ### Explicit Top Level Type
85
87
  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.
@@ -159,7 +161,7 @@ Ruby | NestedText | Comment
159
161
  ---|---|---
160
162
  `nil` | *Custom Class Encoding* | (1.)
161
163
  `Symbol` | `String` |
162
- Custom Class | *Custom Class Encoding* | If the [Custom Class](#custom-classes-serialization) implements `#encode_nt_with` (2.)
164
+ Custom Class | *Custom Class Encoding* | If the [Custom Class](#custom-classes-serialization) implements `#encode_nt_with`
163
165
  Other Class | String | `#to_s` will be called if there is no `#encode_nt_with`
164
166
 
165
167
 
@@ -172,7 +174,7 @@ Other Class | String | `#to_s` will be called if there is no `#encode_nt_with`
172
174
  ## Custom Classes Serialization
173
175
  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.
174
176
 
175
- A class implementing `#encode_nt_with` is refered to as `Custom Class` in this document.
177
+ A class implementing `#encode_nt_with` is referred to as `Custom Class` in this document.
176
178
 
177
179
  ```ruby
178
180
  class Apple
@@ -221,10 +223,10 @@ data:
221
223
  - 12
222
224
  ```
223
225
 
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`:
226
+ If you want to add some more super powers to your custom class, you can add the `#to_nt` shortcut by including the `ToNTMixin`:
225
227
  ```ruby
226
228
  class Apple
227
- include NestedText::NTEncodeMixin
229
+ include NestedText::ToNTMixin
228
230
  ...
229
231
  end
230
232
 
@@ -245,6 +247,68 @@ Apple.new("granny smith", 12).to_nt
245
247
 
246
248
  See [encode_custom_classes_test.rb](test/nestedtext/encode_custom_classes_test.rb) for more real working examples.
247
249
 
250
+ # Schema
251
+ 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)!
252
+
253
+ A schema validators can:
254
+ * assert that the parsed values are like the expected
255
+ * automatically convert them to Ruby class instances like Integer, Float, etc.
256
+
257
+ 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).
258
+
259
+ ## Example with RSchema
260
+ 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).
261
+
262
+ Let's say that you have a program that should connect to a few servers. The list of servers should be stored in a configuration file. With NestedText, this file could look like:
263
+ ```yaml
264
+ -
265
+ name: global-service
266
+ ip: 192.167.1.1
267
+ port: 8080
268
+ -
269
+ name: aux-service
270
+ ip: 17.245.14.2
271
+ port: 67
272
+ # Unstable server, don't use this
273
+ stable: false
274
+ ```
275
+
276
+ After parsing this file with this NestedText library, the values for all keys will be string. But for to make practical use of this, we would of course like the values for `port` to be `Integer`, and `stable` should have a value of either `true` or `false`. RSchema can do this conversion for us!
277
+
278
+
279
+ ```ruby
280
+ # Define schema for our list of servers
281
+ schema = RSchema.define do
282
+ array(
283
+ hash(
284
+ 'name' => _String,
285
+ 'ip' => _String,
286
+ 'port' => _Integer,
287
+ optional('stable') => boolean
288
+ )
289
+ )
290
+ end
291
+
292
+ # The coercer will automatially convert types
293
+ coercer = RSchema::CoercionWrapper::RACK_PARAMS.wrap(schema)
294
+
295
+ # Parse config file with NestedText
296
+ data = NestedText.load_file('conf.nt')
297
+
298
+ # Validate
299
+ result = coercer.validate(data_success)
300
+
301
+ if result.valid?
302
+ servers = result.value
303
+ # Now 'servers' is known to be valid and have the types specified in the schema.
304
+ # Thus, we can use it now!
305
+ stable_servers = servers.select { |server| server['stable'] }
306
+ # Not a meaningful sum - just demonstrating that 'port's are integers and not strings anymore!
307
+ port_sum = servers.map { |server| server['port'] }.sum
308
+ else
309
+ puts result.error
310
+ end
311
+ ```
248
312
 
249
313
  # Installation
250
314
  1. Add this gem to your ruby project's Gemfile
@@ -278,12 +342,15 @@ See [encode_custom_classes_test.rb](test/nestedtext/encode_custom_classes_test.r
278
342
  1. For local testing, install the gem on local machine with: `$ bundle exec rake install`.
279
343
  * or manually with `$ gem build *.gemscpec && gem install *.gem`
280
344
 
281
- Make sure that only intended constants and methods are exposed publicly from the module `NestedText`. Check with
282
- ```
283
- irb> require 'nestedtext'
284
- irb> NestedText.constants
285
- irb> NestedText.methods(false)
286
- ```
345
+
346
+ Extra:
347
+ * Make sure that only intended constants and methods are exposed publicly from the module `NestedText`. Check with
348
+ ```
349
+ irb> require 'nestedtext'
350
+ irb> NestedText.constants
351
+ irb> NestedText.methods(false)
352
+ ```
353
+ * To see undocumented methods with [YARD](https://www.rubydoc.info/gems/yard/file/docs/GettingStarted.md): `$ yard stats --list-undoc`
287
354
 
288
355
  # Releasing
289
356
  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.
@@ -1,7 +1,10 @@
1
- require "stringio"
1
+ # frozen_string_literal: true
2
+
3
+ require 'stringio'
4
+
2
5
  module NestedText
3
- TOP_LEVEL_TYPES = [Object, Hash, Array, String]
4
- CUSTOM_CLASS_KEY = "__nestedtext_class__"
6
+ TOP_LEVEL_TYPES = [Object, Hash, Array, String].freeze
7
+ CUSTOM_CLASS_KEY = '__nestedtext_class__'
5
8
 
6
9
  private_constant :TOP_LEVEL_TYPES, :CUSTOM_CLASS_KEY
7
10
  end
@@ -1,28 +1,23 @@
1
- require "nestedtext/encode_helpers"
1
+ # frozen_string_literal: true
2
2
 
3
- # Extension of Ruby core types with the NestedText::NTEncodeMixin.
4
- #
5
- # TODO: add encoding of more Ruby native classes like Integer, Float etc plus commons like Set,....? Not covered in NestedText language.
6
- # Or leave this to a schema validator 3rd party plugin maybe? And replace my custom class decoding (and also encoding?)?
7
- # Or both: add encoding/decoding of more native classes, and allow decoding + applying a schema with 3rd party.
8
- # Or encourage using Marshal from core?
3
+ require 'nestedtext/encode_helpers'
9
4
 
10
5
  # Extended with the `#to_nt` method.
11
- class String include NestedText::NTEncodeMixin; end
6
+ class String include NestedText::ToNTMixin; end
12
7
 
13
8
  # Extended with the `#to_nt` method.
14
- class Array include NestedText::NTEncodeMixin; end
9
+ class Array include NestedText::ToNTMixin; end
15
10
 
16
11
  # Extended with the `#to_nt` method.
17
- class Hash include NestedText::NTEncodeMixin; end
12
+ class Hash include NestedText::ToNTMixin; end
18
13
 
19
14
  # Extended with NestedText support.
20
15
  class NilClass
21
- include NestedText::NTEncodeMixin
16
+ include NestedText::ToNTMixin
22
17
 
23
18
  # Adds support for encoding and decoding nil.
24
19
  def self.nt_create(_data) = nil
25
20
 
26
21
  # Adds support for encoding and decoding nil.
27
- def encode_nt_with() = ""
22
+ def encode_nt_with() = ''
28
23
  end
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NestedText
2
4
  # Hiding extensions for Kernel here away from users.
3
5
  # Reference: https://ruby-doc.org/core-3.1.0/doc/syntax/refinements_rdoc.html
4
6
  module CoreExtInternal
5
7
  refine String do
6
8
  def normalize_line_endings
7
- # windows/mac -> unix
9
+ # Windows/Mac -> Unix
8
10
  gsub(/\r\n?/, "\n")
9
11
  end
10
12
  end
@@ -1,16 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "nestedtext/parser"
4
- require "nestedtext/errors_internal"
3
+ require 'nestedtext/parser'
4
+ require 'nestedtext/errors_internal'
5
5
 
6
- require "logger"
7
- require "stringio"
6
+ require 'logger'
7
+ require 'stringio'
8
8
 
9
9
  module NestedText
10
10
  # Decode a NestedText string to Ruby objects.
11
11
  #
12
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`.
13
+ # @param top_class [String] Force the top level returned object to be of this type.
14
+ # Supported values are `Object`, `Array`, `Hash` and `String`.
14
15
  # @param strict [Boolean] If strict mode should be used.
15
16
  #
16
17
  # @return [Object, nil] The parsed object.
@@ -19,13 +20,14 @@ module NestedText
19
20
  def self.load(ntstring, top_class: Object, strict: false)
20
21
  raise Errors::WrongInputTypeError.new([String], ntstring) unless ntstring.nil? || ntstring.is_a?(String)
21
22
 
22
- Parser.new(StringIO.new(ntstring), top_class, strict: strict).parse
23
+ Parser.new(StringIO.new(ntstring), top_class, strict:).parse
23
24
  end
24
25
 
25
26
  # Decode a NestedText stored in a given file.
26
27
  #
27
28
  # @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 top_class [String] Force the top level returned object to be of this type.
30
+ # Supported values are `Object`, `Array`, `Hash` and `String`.
29
31
  # @param strict [Boolean] If strict mode should be used.
30
32
  #
31
33
  # @return [Object, nil] The parsed object.
@@ -36,8 +38,8 @@ module NestedText
36
38
  raise Errors::WrongInputTypeError.new([String], filename) unless !filename.nil? && filename.is_a?(String)
37
39
 
38
40
  # Open explicitly in text mode to detect \r as line ending.
39
- File.open(filename, mode = "rt") do |file|
40
- Parser.new(file, top_class, strict: strict).parse
41
+ File.open(filename, 'rt') do |file|
42
+ Parser.new(file, top_class, strict:).parse
41
43
  end
42
44
  end
43
45
  end
@@ -1,10 +1,12 @@
1
- require "nestedtext/core_ext_internal"
1
+ # frozen_string_literal: true
2
+
3
+ require 'nestedtext/core_ext_internal'
2
4
 
3
5
  module NestedText
4
6
  using NestedText.const_get(:CoreExtInternal)
5
7
 
6
8
  # Dumping with recursive DFS traversal of the object references.
7
- class Dumper
9
+ class Dumper # rubocop:disable Metrics/ClassLength
8
10
  def initialize(indentation, strict)
9
11
  @indentation = indentation
10
12
  @strict = strict
@@ -18,31 +20,31 @@ module NestedText
18
20
  dump_any obj
19
21
  end
20
22
 
21
- private
22
-
23
23
  def self.add_prefix(prefix, target)
24
24
  if target.empty? || target[0] == "\n"
25
- target.prepend(prefix)
25
+ "#{prefix}#{target}"
26
26
  else
27
- target.prepend(prefix, " ")
27
+ "#{prefix} #{target}"
28
28
  end
29
29
  end
30
30
 
31
31
  def self.multiline_key?(key)
32
- syntax1 = "{[#"
33
- syntax2 = ":->"
32
+ syntax1 = '{[#'
33
+ syntax2 = ':->'
34
34
 
35
35
  key.empty? ||
36
36
  key != key.strip ||
37
37
  key.include?("\n") ||
38
- key.include?(": ") ||
38
+ key.include?(': ') ||
39
39
  syntax1.include?(key.lstrip[0]) ||
40
- syntax2.include?(key.lstrip[0]) && key.lstrip[1] == " "
40
+ (syntax2.include?(key.lstrip[0]) && key.lstrip[1] == ' ')
41
41
  end
42
42
 
43
+ private
44
+
43
45
  def convert_key(key)
44
46
  if key.nil?
45
- ""
47
+ ''
46
48
  elsif key.is_a? String
47
49
  key.normalize_line_endings
48
50
  elsif !@strict
@@ -52,10 +54,11 @@ module NestedText
52
54
  end
53
55
  end
54
56
 
55
- def indent(target)
56
- indentstr = " " * @indentation
57
- indented = "\n" + target.lines.map { |line| indentstr + line }.join
58
- target.replace indented
57
+ def indent(target, depth)
58
+ return target unless depth.positive?
59
+
60
+ indentstr = ' ' * @indentation
61
+ "\n#{target.lines.map { |line| indentstr + line }.join}"
59
62
  end
60
63
 
61
64
  def trace_cycles(obj)
@@ -81,77 +84,79 @@ module NestedText
81
84
  def dump_any(obj, depth: 0, **kwargs)
82
85
  trace_cycles(obj) do
83
86
  case obj
84
- when Hash then dump_hash(obj, depth: depth, **kwargs)
85
- when Array then dump_array(obj, depth: depth, **kwargs)
86
- when String then dump_string(obj, depth: depth, **kwargs)
87
+ when Hash then dump_hash(obj, depth:, **kwargs)
88
+ when Array then dump_array(obj, depth:, **kwargs)
89
+ when String then dump_string(obj, depth:, **kwargs)
87
90
  when nil
88
- @strict ? "" : dump_custom_class(nil, depth: depth, **kwargs)
91
+ @strict ? '' : dump_custom_class(nil, depth:, **kwargs)
89
92
  else
90
- dump_custom_class(obj, depth: depth, **kwargs)
93
+ dump_custom_class(obj, depth:, **kwargs)
91
94
  end
92
95
  end
93
96
  end
94
97
 
95
- def dump_hash(obj, depth: 0, **kwargs)
96
- rep = if obj.empty?
97
- "{}"
98
- else
99
- obj.map do |key, value|
100
- trace_keys(key) do
101
- key = convert_key(key)
102
-
103
- if Dumper.multiline_key?(key)
104
- key_lines = key.empty? ? [""] : key.lines
105
- key_lines << "" if key_lines[-1][-1] =~ /\n|\r/
106
- rep_key = key_lines.map { |line| Dumper.add_prefix(":", line) }.join
107
- force_multiline = value.is_a? String
108
- rep_value = dump_any(value, depth: depth + 1, force_multiline: force_multiline, **kwargs)
109
- else
110
- rep_key = "#{key}:"
111
- rep_value = dump_any(value, depth: depth + 1, **kwargs)
112
- rep_key += " " unless rep_value.empty? || rep_value.include?("\n")
113
- end
114
- "#{rep_key}#{rep_value}"
115
- end
116
- end.join("\n")
117
- end
118
- indent(rep) if depth > 0
119
- rep
98
+ def dump_hash_key_multiline(key, value, depth, **kwargs)
99
+ key_lines = key.empty? ? [''] : key.lines
100
+ key_lines << '' if key_lines[-1][-1] =~ /\n|\r/
101
+ rep_key = key_lines.map { |line| Dumper.add_prefix(':', line) }.join
102
+ rep_value = dump_any(value, depth: depth + 1, force_multiline: value.is_a?(String), **kwargs)
103
+ [rep_key, rep_value].join
120
104
  end
121
105
 
122
- def dump_array(obj, depth: 0, **kwargs)
123
- rep = if obj.empty?
124
- # TODO: replace this special case with simply general inline rendering detection.
125
- "[]"
106
+ def dump_hash_key(key, value, depth, **kwargs)
107
+ rep_key = "#{key}:"
108
+ rep_value = dump_any(value, depth: depth + 1, **kwargs)
109
+ rep_key += ' ' unless rep_value.empty? || rep_value.include?("\n")
110
+ [rep_key, rep_value].join
111
+ end
112
+
113
+ def dump_hash_items(hash, depth, **kwargs)
114
+ hash.map do |key, value|
115
+ trace_keys(key) do
116
+ key = convert_key(key)
117
+ if Dumper.multiline_key?(key)
118
+ dump_hash_key_multiline(key, value, depth, **kwargs)
119
+ else
120
+ dump_hash_key(key, value, depth, **kwargs)
121
+ end
122
+ end
123
+ end.join("\n")
124
+ end
125
+
126
+ def dump_hash(hash, depth: 0, **kwargs)
127
+ rep = hash.empty? ? '{}' : dump_hash_items(hash, depth, **kwargs)
128
+ indent(rep, depth)
129
+ end
130
+
131
+ def dump_array(array, depth: 0, **kwargs)
132
+ rep = if array.empty?
133
+ '[]'
126
134
  else
127
- obj.each_with_index.map do |e, i|
135
+ array.each_with_index.map do |e, i|
128
136
  trace_keys(i) do
129
- e_rep = dump_any(e, depth: depth + 1, **kwargs)
130
- Dumper.add_prefix("-", e_rep)
137
+ Dumper.add_prefix('-', dump_any(e, depth: depth + 1, **kwargs))
131
138
  end
132
139
  end.join("\n")
133
140
  end
134
141
 
135
- indent(rep) if depth > 0
136
- rep
142
+ indent(rep, depth)
137
143
  end
138
144
 
139
- def dump_string(obj, depth: 0, force_multiline: false)
140
- obj = obj.normalize_line_endings
141
- lines = obj.lines
142
- lines << "\n" if !lines.empty? && lines[-1][-1] == "\n"
143
- if lines.length > 1 || depth == 0 || force_multiline
144
- lines.each do |line|
145
- Dumper.add_prefix(">", line)
146
- end
147
- end
145
+ def prefix_lines(lines, depth, multiline)
146
+ lines = lines.map { |line| Dumper.add_prefix('>', line) } if multiline || depth.zero?
147
+ lines << '>' if lines.empty? && (depth.zero? || multiline)
148
+ lines
149
+ end
150
+
151
+ def dump_string(string, depth: 0, force_multiline: false)
152
+ lines = string.normalize_line_endings.lines
153
+ lines << '' if !lines.empty? && lines.last[-1] == "\n"
154
+ multiline = lines.length > 1 || force_multiline
148
155
 
149
- # Case of empty input string. No space after '>'
150
- lines << ">" if lines.empty? && (depth == 0 || force_multiline)
156
+ lines = prefix_lines(lines, depth, multiline)
151
157
 
152
158
  rep = lines.join.chomp
153
- indent(rep) if !rep.empty? && depth > 0 && (rep.include?("\n") || force_multiline)
154
- rep
159
+ multiline ? indent(rep, depth) : rep
155
160
  end
156
161
 
157
162
  def dump_custom_class(obj, **kwargs)
@@ -160,8 +165,8 @@ module NestedText
160
165
  if obj.is_a? Symbol
161
166
  dump_string(obj.id2name, **kwargs)
162
167
  elsif obj.respond_to? :encode_nt_with
163
- class_name = obj.nil? ? "nil" : obj.class.name
164
- enc = { CUSTOM_CLASS_KEY => class_name, "data" => obj.encode_nt_with }
168
+ class_name = obj.nil? ? 'nil' : obj.class.name
169
+ enc = { CUSTOM_CLASS_KEY => class_name, 'data' => obj.encode_nt_with }
165
170
  dump_any(enc, **kwargs)
166
171
  else
167
172
  dump_string(obj.to_s, **kwargs)
@@ -1,21 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "nestedtext/dumper"
4
- require "nestedtext/errors_internal"
3
+ require 'nestedtext/dumper'
4
+ require 'nestedtext/errors_internal'
5
5
 
6
6
  module NestedText
7
7
  # Encode a Ruby object to a NestedText string.
8
8
  #
9
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.
10
+ # @param io [IO] Additionally write the output to this IO object.
11
+ # The caller is responsible for that the IO is closed after the call to this method.
11
12
  # @param indentation [Integer] The indentation of nested levels to use.
12
13
  # @param strict [Boolean] If strict mode should be used.
13
14
  #
14
- # @return A String containing NestedText data.
15
+ # @return [String, nil] A String containing NestedText data, or nil when obj is represented as empty.
15
16
  # @raise [NestedText::Error] if anything went wrong.
16
17
  # @raise Whatever the `io` can raise, if supplied.
17
18
  def self.dump(obj, io: nil, indentation: 4, strict: false)
18
- raise Errors::DumpBadIOError, io unless io.nil? || io.respond_to?(:write) && io.respond_to?(:fsync)
19
+ raise Errors::DumpBadIOError, io unless io.nil? || (io.respond_to?(:write) && io.respond_to?(:fsync))
19
20
 
20
21
  dumper = Dumper.new(indentation, strict)
21
22
  result = dumper.dump obj
@@ -41,7 +42,7 @@ module NestedText
41
42
  def self.dump_file(obj, filename, **kwargs)
42
43
  raise Errors::DumpFileBadPathError, filename unless filename.is_a? String
43
44
 
44
- File.open(filename, mode = "wt") do |file|
45
+ File.open(filename, 'wt') do |file|
45
46
  dump(obj, io: file, **kwargs)
46
47
  end
47
48
  end
@@ -1,9 +1,10 @@
1
- require "nestedtext/dumper"
1
+ # frozen_string_literal: true
2
+
3
+ require 'nestedtext/dumper'
2
4
 
3
5
  module NestedText
4
6
  # A mixin for Custom Classes to get the to_nt shortcut.
5
- # TODO rename to: ToNTMixin
6
- module NTEncodeMixin
7
+ module ToNTMixin
7
8
  # Encode this object to a NestedText string.
8
9
  #
9
10
  # This method takes the same named arguments as {NestedText.dump}.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NestedText
2
4
  # Top level error to rescue on.
3
5
  #