ruby-toon 1.0.0 β 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +47 -25
- data/lib/extensions/active_support.rb +38 -9
- data/lib/extensions/core.rb +49 -0
- data/lib/toon/version.rb +1 -1
- data/lib/toon.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e32d5758d404c58449d7d8100a0d40d445c0595997620832c48f45dfd36bff9c
|
|
4
|
+
data.tar.gz: cc942b447807a72d595e66bf76479c525545ac6ad8b2c3dd2686d04669075f89
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ca22d215cf9b70115a6e9da0d5ba29dbd1788c8d19d5c7ddbb579521bb8826604c1f3a53ad3322ea298101126cf826b31eec65265d0d55b5b4d9cc883c4ef3e4
|
|
7
|
+
data.tar.gz: bffb33b3605dccf8a0cb011ed7c3cf1c1c27bf0a41ef8d52f2d1d09e03ea038949b35fd8fab9848c857fc6335ec97573462df8619836d7e6bf8fe78869f186de
|
data/README.md
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
# π Toon β Token-Oriented Object Notation for Ruby
|
|
1
|
+
# **Toon** (Token-Oriented Object Notation for Ruby)
|
|
3
2
|
|
|
4
|
-
`toon` is a Ruby implementation of **TOON (Token-Oriented Object Notation)**
|
|
3
|
+
`toon` is a Ruby implementation of **TOON (Token-Oriented Object Notation)**
|
|
5
4
|
a compact, readable, indentation-based data format designed for humans *and* machines.
|
|
6
5
|
|
|
7
6
|
This gem provides:
|
|
8
7
|
|
|
9
|
-
- A **TOON encoder** (Ruby
|
|
10
|
-
- A **TOON decoder** (TOON
|
|
8
|
+
- A **TOON encoder** (Ruby to TOON)
|
|
9
|
+
- A **TOON decoder** (TOON to Ruby)
|
|
11
10
|
- A **CLI** (`bin/toon`) for converting TOON β JSON
|
|
12
11
|
- Optional **ActiveSupport integration** (`Object#to_toon`)
|
|
12
|
+
- Built-in `Hash#to_toon` / `Array#to_toon` plus lightweight `#to_json` helpers
|
|
13
13
|
- Full RSpec test suite
|
|
14
14
|
|
|
15
15
|
---
|
|
16
16
|
|
|
17
|
-
##
|
|
17
|
+
## Features
|
|
18
18
|
|
|
19
|
-
###
|
|
19
|
+
### Encode Ruby objects to TOON
|
|
20
20
|
```ruby
|
|
21
21
|
Toon.generate({ "name" => "Alice", "age" => 30 })
|
|
22
22
|
````
|
|
@@ -28,7 +28,7 @@ name:Alice
|
|
|
28
28
|
age:30
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
###
|
|
31
|
+
### Decode TOON to Ruby
|
|
32
32
|
|
|
33
33
|
```ruby
|
|
34
34
|
Toon.parse("name:Alice\nage:30")
|
|
@@ -42,7 +42,7 @@ Returns:
|
|
|
42
42
|
|
|
43
43
|
---
|
|
44
44
|
|
|
45
|
-
##
|
|
45
|
+
## Arrays
|
|
46
46
|
|
|
47
47
|
### Nested arrays (encoder output)
|
|
48
48
|
|
|
@@ -67,7 +67,7 @@ Both decode correctly.
|
|
|
67
67
|
|
|
68
68
|
---
|
|
69
69
|
|
|
70
|
-
##
|
|
70
|
+
## Tabular Arrays
|
|
71
71
|
|
|
72
72
|
### Nested tabular (encoder output)
|
|
73
73
|
|
|
@@ -78,7 +78,7 @@ users:
|
|
|
78
78
|
2,B
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
numeric fields parsed (`id` becomes integer)
|
|
82
82
|
|
|
83
83
|
### Flat tabular (user input)
|
|
84
84
|
|
|
@@ -88,27 +88,49 @@ users[2]{id,name}:
|
|
|
88
88
|
2,Bob
|
|
89
89
|
```
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
fields remain **strings**
|
|
92
92
|
|
|
93
93
|
---
|
|
94
94
|
|
|
95
|
-
##
|
|
95
|
+
## ActiveSupport Integration
|
|
96
96
|
|
|
97
|
-
If ActiveSupport is installed
|
|
97
|
+
If ActiveSupport (and by extension Rails/Active Record) is installedβregardless of whether it loads before or after `toon`βevery object gains `#to_toon`.
|
|
98
98
|
|
|
99
99
|
```ruby
|
|
100
|
+
require "toon"
|
|
100
101
|
require "active_support"
|
|
102
|
+
|
|
103
|
+
class User < ApplicationRecord; end
|
|
104
|
+
|
|
105
|
+
User.first.to_toon
|
|
106
|
+
# => "id:1\nname:Alice\n..."
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
- Automatically hooks in as soon as ActiveSupport finishes loading (thanks to a TracePoint watcher)
|
|
110
|
+
- Falls back to `#as_json` when present, so Active Record / ActiveModel instances serialize their attributes instead of opaque object IDs
|
|
111
|
+
|
|
112
|
+
## Core Extensions
|
|
113
|
+
|
|
114
|
+
`toon` now provides handy helpers even without ActiveSupport:
|
|
115
|
+
|
|
116
|
+
```ruby
|
|
101
117
|
require "toon"
|
|
102
118
|
|
|
103
|
-
{
|
|
104
|
-
# => "
|
|
119
|
+
{foo: "bar"}.to_toon
|
|
120
|
+
# => "foo:bar"
|
|
121
|
+
|
|
122
|
+
[1, 2, 3].to_toon
|
|
123
|
+
# => "[3]:\n 1\n 2\n 3\n"
|
|
124
|
+
|
|
125
|
+
{foo: "bar"}.to_json
|
|
126
|
+
# => "{\"foo\":\"bar\"}"
|
|
105
127
|
```
|
|
106
128
|
|
|
107
|
-
|
|
129
|
+
Both `Hash` and `Array` gain `#to_toon` and `#to_json` implementations so you can round-trip data between TOON and JSON with a single method call.
|
|
108
130
|
|
|
109
131
|
---
|
|
110
132
|
|
|
111
|
-
##
|
|
133
|
+
## Installation
|
|
112
134
|
|
|
113
135
|
Gem coming soon. For now:
|
|
114
136
|
|
|
@@ -126,15 +148,15 @@ require_relative "lib/toon"
|
|
|
126
148
|
|
|
127
149
|
---
|
|
128
150
|
|
|
129
|
-
##
|
|
151
|
+
## CLI Usage
|
|
130
152
|
|
|
131
|
-
### Encode JSON
|
|
153
|
+
### Encode JSON to TOON
|
|
132
154
|
|
|
133
155
|
```bash
|
|
134
156
|
echo '{"name":"Alice","age":30}' | bin/toon --encode
|
|
135
157
|
```
|
|
136
158
|
|
|
137
|
-
### Decode TOON
|
|
159
|
+
### Decode TOON to JSON
|
|
138
160
|
|
|
139
161
|
```bash
|
|
140
162
|
echo "name:Alice\nage:30" | bin/toon --decode
|
|
@@ -148,7 +170,7 @@ bin/toon --encode < input.json
|
|
|
148
170
|
|
|
149
171
|
---
|
|
150
172
|
|
|
151
|
-
##
|
|
173
|
+
## Running Tests
|
|
152
174
|
|
|
153
175
|
Ensure CLI is executable:
|
|
154
176
|
|
|
@@ -171,7 +193,7 @@ Tests include:
|
|
|
171
193
|
|
|
172
194
|
---
|
|
173
195
|
|
|
174
|
-
##
|
|
196
|
+
## Supported TOON Grammar (Current)
|
|
175
197
|
|
|
176
198
|
### Key-value
|
|
177
199
|
|
|
@@ -225,7 +247,7 @@ users[2]{id,name}:
|
|
|
225
247
|
|
|
226
248
|
---
|
|
227
249
|
|
|
228
|
-
##
|
|
250
|
+
## Error Handling
|
|
229
251
|
|
|
230
252
|
Malformed input (e.g., missing indentation):
|
|
231
253
|
|
|
@@ -237,7 +259,7 @@ Decoder stops with a friendly `Toon::Error`.
|
|
|
237
259
|
|
|
238
260
|
---
|
|
239
261
|
|
|
240
|
-
##
|
|
262
|
+
## Roadmap
|
|
241
263
|
|
|
242
264
|
* Multiline values
|
|
243
265
|
* Quoted strings
|
|
@@ -1,12 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
module Toon
|
|
2
|
+
module Extensions
|
|
3
|
+
module ActiveSupport
|
|
4
|
+
module ObjectMethods
|
|
5
|
+
def to_toon(**opts)
|
|
6
|
+
payload = respond_to?(:as_json) ? as_json : self
|
|
7
|
+
Toon.generate(payload, **opts)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def install!
|
|
14
|
+
return if Object.method_defined?(:to_toon)
|
|
15
|
+
Object.include(ObjectMethods)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def ensure_installed!
|
|
19
|
+
return unless defined?(::ActiveSupport)
|
|
20
|
+
install!
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def watch_for_active_support!
|
|
24
|
+
return if defined?(@tracepoint) && @tracepoint&.enabled?
|
|
25
|
+
@tracepoint = TracePoint.new(:end) do |tp|
|
|
26
|
+
next unless tp.self.is_a?(Module)
|
|
27
|
+
next unless tp.self.name == "ActiveSupport"
|
|
28
|
+
ensure_installed!
|
|
29
|
+
@tracepoint.disable
|
|
30
|
+
end
|
|
31
|
+
@tracepoint.enable
|
|
32
|
+
end
|
|
8
33
|
end
|
|
9
34
|
end
|
|
10
|
-
|
|
11
|
-
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
if defined?(::ActiveSupport)
|
|
38
|
+
Toon::Extensions::ActiveSupport.ensure_installed!
|
|
39
|
+
else
|
|
40
|
+
Toon::Extensions::ActiveSupport.watch_for_active_support!
|
|
12
41
|
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
module Toon
|
|
4
|
+
module Extensions
|
|
5
|
+
module Core
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
def hash_to_toon(target, **opts)
|
|
9
|
+
Toon.generate(target, **opts)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def array_to_toon(target, **opts)
|
|
13
|
+
Toon.generate(target, **opts)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def to_json_payload(target, *args)
|
|
17
|
+
JSON.generate(target, *args)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class Hash
|
|
24
|
+
unless method_defined?(:to_toon)
|
|
25
|
+
def to_toon(**opts)
|
|
26
|
+
Toon::Extensions::Core.hash_to_toon(self, **opts)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
unless method_defined?(:to_json)
|
|
31
|
+
def to_json(*args)
|
|
32
|
+
Toon::Extensions::Core.to_json_payload(self, *args)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
class Array
|
|
38
|
+
unless method_defined?(:to_toon)
|
|
39
|
+
def to_toon(**opts)
|
|
40
|
+
Toon::Extensions::Core.array_to_toon(self, **opts)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
unless method_defined?(:to_json)
|
|
45
|
+
def to_json(*args)
|
|
46
|
+
Toon::Extensions::Core.to_json_payload(self, *args)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/toon/version.rb
CHANGED
data/lib/toon.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ruby-toon
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Anil Yanduri
|
|
@@ -49,6 +49,7 @@ files:
|
|
|
49
49
|
- LICENSE
|
|
50
50
|
- README.md
|
|
51
51
|
- lib/extensions/active_support.rb
|
|
52
|
+
- lib/extensions/core.rb
|
|
52
53
|
- lib/toon.rb
|
|
53
54
|
- lib/toon/decoder.rb
|
|
54
55
|
- lib/toon/encoder.rb
|