shale 1.2.1 → 1.3.0
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/CHANGELOG.md +16 -0
- data/README.md +74 -11
- data/lib/shale/adapter/rack_url_encoded.rb +63 -0
- data/lib/shale/mapper.rb +35 -0
- data/lib/shale/schema/compiler/decimal.rb +21 -0
- data/lib/shale/schema/json_generator/ref.rb +2 -2
- data/lib/shale/schema/json_generator.rb +1 -1
- data/lib/shale/schema/xml_compiler.rb +9 -1
- data/lib/shale/schema/xml_generator.rb +1 -0
- data/lib/shale/type/complex.rb +62 -3
- data/lib/shale/type/date.rb +11 -0
- data/lib/shale/type/decimal.rb +56 -0
- data/lib/shale/type/time.rb +11 -0
- data/lib/shale/type/value.rb +22 -0
- data/lib/shale/version.rb +1 -1
- data/lib/shale.rb +18 -2
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b99860dbcfe4f9b8b33b2e1ecb4d439b7f75f96583f723795dd5ded0e6c13449
|
|
4
|
+
data.tar.gz: 31e9f73080862fbe6fcd5a6973451697590872d4cd88c8201528358528c42d9a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1e035087255e1f8d29c081666ddd186e9737e82ee4de6776536b24913c50cc7c0df86523c4f3f1c1366a6a339125e8c957db97da865e0ac6d6a327fb91180c3a
|
|
7
|
+
data.tar.gz: 897b07c8301ff0e519d6f1aa1f36f70ceb164b7f1bbd653a01a2d992744ed7a6a35e97dedd62d38e632a4740dd415df485f101a74928dfb628d51d82253b1657
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
## [1.3.0] - 2026-12-02
|
|
2
|
+
|
|
3
|
+
### Added
|
|
4
|
+
- [Verseth] Add support for x-www-form-urlencoded format
|
|
5
|
+
|
|
6
|
+
### Fixed
|
|
7
|
+
- [gaoflow] Fix `Type::Decimal.cast` raising on floats with a long decimal expansion (e.g. `0.1 + 0.2`)
|
|
8
|
+
|
|
9
|
+
## [1.2.2] - 2025-03-13
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
- [kjeldahl] Adds BigDecimal mapper
|
|
13
|
+
|
|
14
|
+
### Fixed
|
|
15
|
+
- [ollietulloch] Allow `Shale::Mapper` attributes to define a `schema`
|
|
16
|
+
|
|
1
17
|
## [1.2.1] - 2025-01-09
|
|
2
18
|
|
|
3
19
|
### Fixed
|
data/README.md
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
# Shale
|
|
2
2
|
|
|
3
|
-
Shale is a Ruby object mapper and serializer for JSON, YAML, TOML, CSV and
|
|
4
|
-
It allows you to parse JSON, YAML, TOML, CSV and
|
|
5
|
-
as well as serialize data structures into JSON, YAML, TOML, CSV or
|
|
6
|
-
|
|
7
|
-
Documentation with interactive examples is available at [Shale website](https://www.shalerb.org)
|
|
3
|
+
Shale is a Ruby object mapper and serializer for JSON, YAML, TOML, CSV, XML and x-www-form-urlencoded.
|
|
4
|
+
It allows you to parse JSON, YAML, TOML, CSV, XML and x-www-form-urlencoded data and convert it into Ruby data structures,
|
|
5
|
+
as well as serialize data structures into JSON, YAML, TOML, CSV, XML or x-www-form-urlencoded.
|
|
8
6
|
|
|
9
7
|
## Features
|
|
10
8
|
|
|
11
|
-
* Convert JSON, YAML, TOML, CSV and
|
|
12
|
-
* Convert Ruby data model to JSON, YAML, TOML, CSV and
|
|
9
|
+
* Convert JSON, YAML, TOML, CSV, XML and x-www-form-urlencoded to Ruby data model
|
|
10
|
+
* Convert Ruby data model to JSON, YAML, TOML, CSV, XML and x-www-form-urlencoded
|
|
13
11
|
* Generate JSON and XML Schema from Ruby models
|
|
14
12
|
* Compile JSON and XML Schema into Ruby models
|
|
15
|
-
* Out of the box support for JSON, YAML, Tomlib, toml-rb, CSV, Nokogiri, REXML
|
|
13
|
+
* Out of the box support for JSON, YAML, Tomlib, toml-rb, CSV, Nokogiri, REXML, Ox, Rack x-www-form-urlencoded parsers
|
|
16
14
|
* Support for custom adapters
|
|
17
15
|
|
|
18
16
|
## Installation
|
|
@@ -53,10 +51,13 @@ $ gem install shale
|
|
|
53
51
|
* [Converting object to XML](#converting-object-to-xml)
|
|
54
52
|
* [Converting CSV to object](#converting-csv-to-object)
|
|
55
53
|
* [Converting object to CSV](#converting-object-to-csv)
|
|
54
|
+
* [Converting urlencoded to object](#converting-urlencoded-to-object)
|
|
55
|
+
* [Converting object to urlencoded](#converting-object-to-urlencoded)
|
|
56
56
|
* [Converting collections](#converting-collections)
|
|
57
57
|
* [Mapping JSON keys to object attributes](#mapping-json-keys-to-object-attributes)
|
|
58
58
|
* [Mapping YAML keys to object attributes](#mapping-yaml-keys-to-object-attributes)
|
|
59
59
|
* [Mapping TOML keys to object attributes](#mapping-toml-keys-to-object-attributes)
|
|
60
|
+
* [Mapping urlencoded keys to object attributes](#mapping-urlencoded-keys-to-object-attributes)
|
|
60
61
|
* [Mapping CSV columns to object attributes](#mapping-csv-columns-to-object-attributes)
|
|
61
62
|
* [Mapping Hash keys to object attributes](#mapping-hash-keys-to-object-attributes)
|
|
62
63
|
* [Mapping XML elements and attributes to object attributes](#mapping-xml-elements-and-attributes-to-object-attributes)
|
|
@@ -378,7 +379,7 @@ but you can use methods to map properties to complex types
|
|
|
378
379
|
(see [Using methods to extract and generate data](#using-methods-to-extract-and-generate-data)
|
|
379
380
|
section).
|
|
380
381
|
|
|
381
|
-
`.from_csv` method
|
|
382
|
+
`.from_csv` method always returns an array of records.
|
|
382
383
|
|
|
383
384
|
```ruby
|
|
384
385
|
people = Person.from_csv(<<~DATA)
|
|
@@ -396,6 +397,41 @@ people[0].to_csv # or Person.to_csv(people) if you want to convert a collection
|
|
|
396
397
|
# John,Doe,50,false
|
|
397
398
|
```
|
|
398
399
|
|
|
400
|
+
### Converting urlencoded to object
|
|
401
|
+
|
|
402
|
+
To use x-www-form-urlencoded with Shale you have to set an adapter.
|
|
403
|
+
Shale comes with adapter for [rack/utils](https://github.com/rack/rack).
|
|
404
|
+
For details see [Adapters](#adapters) section.
|
|
405
|
+
|
|
406
|
+
To set it, first make sure the `rack` gem is installed:
|
|
407
|
+
|
|
408
|
+
```
|
|
409
|
+
$ gem install rack
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
then setup the adapter:
|
|
413
|
+
|
|
414
|
+
```ruby
|
|
415
|
+
require 'shale/adapter/rack_url_encoded'
|
|
416
|
+
Shale.urlencoded_adapter = Shale::Adapter::RackURLEncoded
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
Now you can use x-www-form-urlencoded data with Shale.
|
|
420
|
+
|
|
421
|
+
`.from_urlencoded` method always returns an array of records.
|
|
422
|
+
|
|
423
|
+
```ruby
|
|
424
|
+
person = Person.from_urlencoded("first_name=John&last_name=Doe&age=50&married=false")
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
### Converting object to urlencoded
|
|
428
|
+
|
|
429
|
+
```ruby
|
|
430
|
+
person.to_urlencoded
|
|
431
|
+
|
|
432
|
+
# => "first_name=John&last_name=Doe&age=50&married=false"
|
|
433
|
+
```
|
|
434
|
+
|
|
399
435
|
### Converting collections
|
|
400
436
|
|
|
401
437
|
Shale allows converting collections for formats that support it (JSON, YAML and CSV).
|
|
@@ -481,6 +517,24 @@ class Person < Shale::Mapper
|
|
|
481
517
|
end
|
|
482
518
|
```
|
|
483
519
|
|
|
520
|
+
### Mapping urlencoded keys to object attributes
|
|
521
|
+
|
|
522
|
+
By default keys are named the same as attributes. To use custom keys use:
|
|
523
|
+
|
|
524
|
+
:warning: **Declaring custom mapping removes default mapping for given format!**
|
|
525
|
+
|
|
526
|
+
```ruby
|
|
527
|
+
class Person < Shale::Mapper
|
|
528
|
+
attribute :first_name, :string
|
|
529
|
+
attribute :last_name, :string
|
|
530
|
+
|
|
531
|
+
urlencoded do
|
|
532
|
+
map 'firstName', to: :first_name
|
|
533
|
+
map 'lastName', to: :last_name
|
|
534
|
+
end
|
|
535
|
+
end
|
|
536
|
+
```
|
|
537
|
+
|
|
484
538
|
### Mapping CSV columns to object attributes
|
|
485
539
|
|
|
486
540
|
For CSV the order of mapping matters, the first argument in the `map` method is only
|
|
@@ -758,7 +812,7 @@ end
|
|
|
758
812
|
|
|
759
813
|
### Using methods to extract and generate data
|
|
760
814
|
|
|
761
|
-
If you need full
|
|
815
|
+
If you need full control over extracting and generating data from/to document,
|
|
762
816
|
you can use methods to do so:
|
|
763
817
|
|
|
764
818
|
```ruby
|
|
@@ -1041,7 +1095,7 @@ person.to_json(pretty: true)
|
|
|
1041
1095
|
```
|
|
1042
1096
|
|
|
1043
1097
|
You can also add an XML declaration by passing `declaration: true` and `encoding: true`
|
|
1044
|
-
or if you want to
|
|
1098
|
+
or if you want to specify version: `declaration: '1.1'` and `encoding: 'ASCII'` to `#to_xml`
|
|
1045
1099
|
|
|
1046
1100
|
```ruby
|
|
1047
1101
|
person.to_xml(pretty: true, declaration: true, encoding: true)
|
|
@@ -1211,6 +1265,7 @@ Shale supports these types out of the box:
|
|
|
1211
1265
|
- `:boolean` (`Shale::Type::Boolean`)
|
|
1212
1266
|
- `:date` (`Shale::Type::Date`)
|
|
1213
1267
|
- `:float` (`Shale::Type::Float`)
|
|
1268
|
+
- `:decimal` (`Shale::Type::Decimal`)
|
|
1214
1269
|
- `:integer` (`Shale::Type::Integer`)
|
|
1215
1270
|
- `:string` (`Shale::Type::String`)
|
|
1216
1271
|
- `:time` (`Shale::Type::Time`)
|
|
@@ -1326,6 +1381,14 @@ require 'shale/adapter/ox'
|
|
|
1326
1381
|
Shale.xml_adapter = Shale::Adapter::Ox
|
|
1327
1382
|
```
|
|
1328
1383
|
|
|
1384
|
+
To handle x-www-form-urlencoded documents you have to set the urlencoded adapter. Shale provides an adapter for the `rack/utils` parser:
|
|
1385
|
+
|
|
1386
|
+
```ruby
|
|
1387
|
+
require 'shale'
|
|
1388
|
+
require 'shale/adapter/rack_url_encoded'
|
|
1389
|
+
Shale.urlencoded_adapter = Shale::Adapter::RackURLEncoded
|
|
1390
|
+
```
|
|
1391
|
+
|
|
1329
1392
|
### Generating JSON Schema
|
|
1330
1393
|
|
|
1331
1394
|
:warning: Only **[Draft 2020-12](https://json-schema.org/draft/2020-12/schema)** JSON Schema is supported
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rack/utils'
|
|
4
|
+
|
|
5
|
+
module Shale
|
|
6
|
+
module Adapter
|
|
7
|
+
# RackURLEncoded adapter. Adapter for url-encoded format using rack utils.
|
|
8
|
+
#
|
|
9
|
+
# @api public
|
|
10
|
+
class RackURLEncoded
|
|
11
|
+
# Synthetic key used to coax Rack into parsing a top-level array.
|
|
12
|
+
# Rack's parser always produces a Hash at the root and refuses to treat
|
|
13
|
+
# a leading "[]" as array nesting, so we prefix each pair with this key,
|
|
14
|
+
# let Rack build the array under it, then unwrap the result.
|
|
15
|
+
TOP_LEVEL_ARRAY_KEY = 'r'
|
|
16
|
+
|
|
17
|
+
class << self
|
|
18
|
+
# Parse x-www-form-urlencoded into Hash
|
|
19
|
+
#
|
|
20
|
+
# @param [String] text x-www-form-urlencoded text
|
|
21
|
+
# @param [Hash] options
|
|
22
|
+
#
|
|
23
|
+
# @return [Hash]
|
|
24
|
+
#
|
|
25
|
+
# @api private
|
|
26
|
+
def load(text, **_options)
|
|
27
|
+
return ::Rack::Utils.parse_nested_query(text) unless top_level_array?(text)
|
|
28
|
+
|
|
29
|
+
prefixed = text.split('&').map { |pair| "#{TOP_LEVEL_ARRAY_KEY}#{pair}" }.join('&')
|
|
30
|
+
::Rack::Utils.parse_nested_query(prefixed)[TOP_LEVEL_ARRAY_KEY]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Serialize Hash into x-www-form-urlencoded
|
|
34
|
+
#
|
|
35
|
+
# @param [Hash] obj Hash object
|
|
36
|
+
# @param [Hash] options
|
|
37
|
+
#
|
|
38
|
+
# @return [String]
|
|
39
|
+
#
|
|
40
|
+
# @api private
|
|
41
|
+
def dump(obj, **_options)
|
|
42
|
+
::Rack::Utils.build_nested_query(obj)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
# Detect whether the document encodes a top-level array. Such documents
|
|
48
|
+
# start with "[]" (escaped as "%5B%5D")
|
|
49
|
+
#
|
|
50
|
+
# @param [String] text x-www-form-urlencoded text
|
|
51
|
+
#
|
|
52
|
+
# @return [Boolean]
|
|
53
|
+
#
|
|
54
|
+
# @api private
|
|
55
|
+
def top_level_array?(text)
|
|
56
|
+
return false if text.nil? || text.empty?
|
|
57
|
+
|
|
58
|
+
text.start_with?('%5B%5D')
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
data/lib/shale/mapper.rb
CHANGED
|
@@ -47,6 +47,7 @@ module Shale
|
|
|
47
47
|
@model = nil
|
|
48
48
|
@attributes = {}
|
|
49
49
|
@hash_mapping = Mapping::Dict.new
|
|
50
|
+
@urlencoded_mapping = Mapping::Dict.new
|
|
50
51
|
@json_mapping = Mapping::Dict.new
|
|
51
52
|
@yaml_mapping = Mapping::Dict.new
|
|
52
53
|
@toml_mapping = Mapping::Dict.new
|
|
@@ -68,6 +69,13 @@ module Shale
|
|
|
68
69
|
# @api public
|
|
69
70
|
attr_reader :hash_mapping
|
|
70
71
|
|
|
72
|
+
# Return x-www-form-urlencoded mapping object
|
|
73
|
+
#
|
|
74
|
+
# @return [Shale::Mapping::Dict]
|
|
75
|
+
#
|
|
76
|
+
# @api public
|
|
77
|
+
attr_reader :urlencoded_mapping
|
|
78
|
+
|
|
71
79
|
# Return JSON mapping object
|
|
72
80
|
#
|
|
73
81
|
# @return [Shale::Mapping::Dict]
|
|
@@ -115,6 +123,7 @@ module Shale
|
|
|
115
123
|
subclass.instance_variable_set('@attributes', @attributes.dup)
|
|
116
124
|
|
|
117
125
|
subclass.instance_variable_set('@__hash_mapping_init', @hash_mapping.dup)
|
|
126
|
+
subclass.instance_variable_set('@__urlencoded_mapping_init', @urlencoded_mapping.dup)
|
|
118
127
|
subclass.instance_variable_set('@__json_mapping_init', @json_mapping.dup)
|
|
119
128
|
subclass.instance_variable_set('@__yaml_mapping_init', @yaml_mapping.dup)
|
|
120
129
|
subclass.instance_variable_set('@__toml_mapping_init', @toml_mapping.dup)
|
|
@@ -122,6 +131,7 @@ module Shale
|
|
|
122
131
|
subclass.instance_variable_set('@__xml_mapping_init', @xml_mapping.dup)
|
|
123
132
|
|
|
124
133
|
subclass.instance_variable_set('@hash_mapping', @hash_mapping.dup)
|
|
134
|
+
subclass.instance_variable_set('@urlencoded_mapping', @urlencoded_mapping.dup)
|
|
125
135
|
subclass.instance_variable_set('@json_mapping', @json_mapping.dup)
|
|
126
136
|
subclass.instance_variable_set('@yaml_mapping', @yaml_mapping.dup)
|
|
127
137
|
subclass.instance_variable_set('@toml_mapping', @toml_mapping.dup)
|
|
@@ -186,6 +196,7 @@ module Shale
|
|
|
186
196
|
@attributes[name] = Attribute.new(name, type, collection, default)
|
|
187
197
|
|
|
188
198
|
@hash_mapping.map(name.to_s, to: name) unless @hash_mapping.finalized?
|
|
199
|
+
@urlencoded_mapping.map(name.to_s, to: name) unless @urlencoded_mapping.finalized?
|
|
189
200
|
@json_mapping.map(name.to_s, to: name) unless @json_mapping.finalized?
|
|
190
201
|
@yaml_mapping.map(name.to_s, to: name) unless @yaml_mapping.finalized?
|
|
191
202
|
@toml_mapping.map(name.to_s, to: name) unless @toml_mapping.finalized?
|
|
@@ -225,6 +236,30 @@ module Shale
|
|
|
225
236
|
@hash_mapping.instance_eval(&block)
|
|
226
237
|
end
|
|
227
238
|
|
|
239
|
+
# Define x-www-form-urlencoded mapping
|
|
240
|
+
#
|
|
241
|
+
# @param [Proc] block
|
|
242
|
+
#
|
|
243
|
+
# @example
|
|
244
|
+
# class Person < Shale::Mapper
|
|
245
|
+
# attribute :first_name, :string
|
|
246
|
+
# attribute :last_name, :string
|
|
247
|
+
# attribute :age, :integer
|
|
248
|
+
#
|
|
249
|
+
# urlencoded do
|
|
250
|
+
# map 'firstName', to: :first_name
|
|
251
|
+
# map 'lastName', to: :last_name
|
|
252
|
+
# map 'age', to: :age
|
|
253
|
+
# end
|
|
254
|
+
# end
|
|
255
|
+
#
|
|
256
|
+
# @api public
|
|
257
|
+
def urlencoded(&block)
|
|
258
|
+
@urlencoded_mapping = @__urlencoded_mapping_init.dup
|
|
259
|
+
@urlencoded_mapping.finalize!
|
|
260
|
+
@urlencoded_mapping.instance_eval(&block)
|
|
261
|
+
end
|
|
262
|
+
|
|
228
263
|
# Define JSON mapping
|
|
229
264
|
#
|
|
230
265
|
# @param [Proc] block
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Shale
|
|
4
|
+
module Schema
|
|
5
|
+
module Compiler
|
|
6
|
+
# Class that maps Schema type to Shale Decimal type
|
|
7
|
+
#
|
|
8
|
+
# @api private
|
|
9
|
+
class Decimal
|
|
10
|
+
# Return name of the Shale type
|
|
11
|
+
#
|
|
12
|
+
# @return [String]
|
|
13
|
+
#
|
|
14
|
+
# @api private
|
|
15
|
+
def name
|
|
16
|
+
'Shale::Type::Decimal'
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -87,7 +87,7 @@ module Shale
|
|
|
87
87
|
next unless attribute
|
|
88
88
|
|
|
89
89
|
if mapper_type?(attribute.type)
|
|
90
|
-
json_type = Ref.new(mapping.name, attribute.type.model.name)
|
|
90
|
+
json_type = Ref.new(mapping.name, attribute.type.model.name, schema: mapping.schema)
|
|
91
91
|
else
|
|
92
92
|
json_klass = self.class.get_json_type(attribute.type)
|
|
93
93
|
|
|
@@ -6,6 +6,7 @@ require_relative '../../shale'
|
|
|
6
6
|
require_relative '../error'
|
|
7
7
|
require_relative 'compiler/boolean'
|
|
8
8
|
require_relative 'compiler/date'
|
|
9
|
+
require_relative 'compiler/decimal'
|
|
9
10
|
require_relative 'compiler/float'
|
|
10
11
|
require_relative 'compiler/integer'
|
|
11
12
|
require_relative 'compiler/string'
|
|
@@ -103,11 +104,16 @@ module Shale
|
|
|
103
104
|
# XML Schema "float" types
|
|
104
105
|
# @api private
|
|
105
106
|
XS_TYPE_FLOAT = [
|
|
106
|
-
"#{XS_NAMESPACE_URI}:decimal",
|
|
107
107
|
"#{XS_NAMESPACE_URI}:float",
|
|
108
108
|
"#{XS_NAMESPACE_URI}:double",
|
|
109
109
|
].freeze
|
|
110
110
|
|
|
111
|
+
# XML Schema "decimal" types
|
|
112
|
+
# @api private
|
|
113
|
+
XS_TYPE_DECIMAL = [
|
|
114
|
+
"#{XS_NAMESPACE_URI}:decimal",
|
|
115
|
+
].freeze
|
|
116
|
+
|
|
111
117
|
# XML Schema "integer" types
|
|
112
118
|
# @api private
|
|
113
119
|
XS_TYPE_INTEGER = [
|
|
@@ -612,6 +618,8 @@ module Shale
|
|
|
612
618
|
Compiler::Time.new
|
|
613
619
|
elsif XS_TYPE_STRING.include?(type)
|
|
614
620
|
Compiler::String.new
|
|
621
|
+
elsif XS_TYPE_DECIMAL.include?(type)
|
|
622
|
+
Compiler::Decimal.new
|
|
615
623
|
elsif XS_TYPE_FLOAT.include?(type)
|
|
616
624
|
Compiler::Float.new
|
|
617
625
|
elsif XS_TYPE_INTEGER.include?(type)
|
|
@@ -52,6 +52,7 @@ module Shale
|
|
|
52
52
|
register_xml_type(Shale::Type::Boolean, 'boolean')
|
|
53
53
|
register_xml_type(Shale::Type::Date, 'date')
|
|
54
54
|
register_xml_type(Shale::Type::Float, 'decimal')
|
|
55
|
+
register_xml_type(Shale::Type::Decimal, 'decimal')
|
|
55
56
|
register_xml_type(Shale::Type::Integer, 'integer')
|
|
56
57
|
register_xml_type(Shale::Type::Time, 'dateTime')
|
|
57
58
|
register_xml_type(Shale::Type::Value, 'anyType')
|
data/lib/shale/type/complex.rb
CHANGED
|
@@ -14,9 +14,9 @@ module Shale
|
|
|
14
14
|
# @api private
|
|
15
15
|
class Complex < Value
|
|
16
16
|
class << self
|
|
17
|
-
%i[hash json yaml toml csv].each do |format|
|
|
17
|
+
%i[hash urlencoded json yaml toml csv].each do |format|
|
|
18
18
|
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
|
19
|
-
# Convert Hash to Object using Hash/JSON/YAML/TOML mapping
|
|
19
|
+
# Convert Hash to Object using Hash/JSON/YAML/TOML/urlencoded mapping
|
|
20
20
|
#
|
|
21
21
|
# @param [Hash, Array] hash Hash to convert
|
|
22
22
|
# @param [Array<Symbol>] only
|
|
@@ -136,7 +136,7 @@ module Shale
|
|
|
136
136
|
instance
|
|
137
137
|
end
|
|
138
138
|
|
|
139
|
-
# Convert Object to Hash using Hash/JSON/YAML/TOML mapping
|
|
139
|
+
# Convert Object to Hash using Hash/JSON/YAML/TOML/urlencoded mapping
|
|
140
140
|
#
|
|
141
141
|
# @param [any, Array<any>] instance Object to convert
|
|
142
142
|
# @param [Array<Symbol>] only
|
|
@@ -290,6 +290,45 @@ module Shale
|
|
|
290
290
|
)
|
|
291
291
|
end
|
|
292
292
|
|
|
293
|
+
# Convert x-www-form-urlencoded to Object
|
|
294
|
+
#
|
|
295
|
+
# @param [String] urlencoded x-www-form-urlencoded text to convert
|
|
296
|
+
# @param [Array<Symbol>] only
|
|
297
|
+
# @param [Array<Symbol>] except
|
|
298
|
+
# @param [any] context
|
|
299
|
+
# @param [Hash] urlencoded_options
|
|
300
|
+
#
|
|
301
|
+
# @return [model instance]
|
|
302
|
+
#
|
|
303
|
+
# @api public
|
|
304
|
+
def from_urlencoded(urlencoded, only: nil, except: nil, context: nil, **urlencoded_options)
|
|
305
|
+
of_urlencoded(
|
|
306
|
+
Shale.urlencoded_adapter.load(urlencoded, **urlencoded_options),
|
|
307
|
+
only: only,
|
|
308
|
+
except: except,
|
|
309
|
+
context: context
|
|
310
|
+
)
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
# Convert Object to x-www-form-urlencoded
|
|
314
|
+
#
|
|
315
|
+
# @param [model instance] instance Object to convert
|
|
316
|
+
# @param [Array<Symbol>] only
|
|
317
|
+
# @param [Array<Symbol>] except
|
|
318
|
+
# @param [any] context
|
|
319
|
+
# @param [true, false] pretty
|
|
320
|
+
# @param [Hash] urlencoded_options
|
|
321
|
+
#
|
|
322
|
+
# @return [String]
|
|
323
|
+
#
|
|
324
|
+
# @api public
|
|
325
|
+
def to_urlencoded(instance, only: nil, except: nil, context: nil, pretty: false, **urlencoded_options)
|
|
326
|
+
Shale.urlencoded_adapter.dump(
|
|
327
|
+
as_urlencoded(instance, only: only, except: except, context: context),
|
|
328
|
+
**urlencoded_options.merge(pretty: pretty)
|
|
329
|
+
)
|
|
330
|
+
end
|
|
331
|
+
|
|
293
332
|
# Convert YAML to Object
|
|
294
333
|
#
|
|
295
334
|
# @param [String] yaml YAML to convert
|
|
@@ -1024,6 +1063,26 @@ module Shale
|
|
|
1024
1063
|
)
|
|
1025
1064
|
end
|
|
1026
1065
|
|
|
1066
|
+
# Convert Object to x-www-form-urlencoded
|
|
1067
|
+
#
|
|
1068
|
+
# @param [Array<Symbol>] only
|
|
1069
|
+
# @param [Array<Symbol>] except
|
|
1070
|
+
# @param [any] context
|
|
1071
|
+
# @param [Hash] urlencoded_options
|
|
1072
|
+
#
|
|
1073
|
+
# @return [String]
|
|
1074
|
+
#
|
|
1075
|
+
# @api public
|
|
1076
|
+
def to_urlencoded(only: nil, except: nil, context: nil, **urlencoded_options)
|
|
1077
|
+
self.class.to_urlencoded(
|
|
1078
|
+
self,
|
|
1079
|
+
only: only,
|
|
1080
|
+
except: except,
|
|
1081
|
+
context: context,
|
|
1082
|
+
**urlencoded_options
|
|
1083
|
+
)
|
|
1084
|
+
end
|
|
1085
|
+
|
|
1027
1086
|
# Convert Object to YAML
|
|
1028
1087
|
#
|
|
1029
1088
|
# @param [Array<Symbol>] only
|
data/lib/shale/type/date.rb
CHANGED
|
@@ -58,6 +58,17 @@ module Shale
|
|
|
58
58
|
value&.iso8601
|
|
59
59
|
end
|
|
60
60
|
|
|
61
|
+
# Use ISO 8601 format in x-www-form-urlencoded document
|
|
62
|
+
#
|
|
63
|
+
# @param [Date] value
|
|
64
|
+
#
|
|
65
|
+
# @return [String]
|
|
66
|
+
#
|
|
67
|
+
# @api private
|
|
68
|
+
def self.as_urlencoded(value, **)
|
|
69
|
+
value&.iso8601
|
|
70
|
+
end
|
|
71
|
+
|
|
61
72
|
# Use ISO 8601 format in XML document
|
|
62
73
|
#
|
|
63
74
|
# @param [Date] value Value to convert to XML
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'value'
|
|
4
|
+
|
|
5
|
+
module Shale
|
|
6
|
+
module Type
|
|
7
|
+
# Cast value to BigDecimal
|
|
8
|
+
#
|
|
9
|
+
# @api public
|
|
10
|
+
class Decimal < Value
|
|
11
|
+
class << self
|
|
12
|
+
# @param [String, Float, Integer, nil] value Value to cast
|
|
13
|
+
#
|
|
14
|
+
# @return [BigDecimal, nil]
|
|
15
|
+
#
|
|
16
|
+
# @api private
|
|
17
|
+
def cast(value)
|
|
18
|
+
return if value.nil?
|
|
19
|
+
|
|
20
|
+
case value
|
|
21
|
+
when ::BigDecimal then value
|
|
22
|
+
# BigDecimal(float, n) caps n at 16, so parse the float's shortest round-trip string
|
|
23
|
+
when ::Float then BigDecimal(value.to_s)
|
|
24
|
+
else BigDecimal(value)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def as_json(value, **)
|
|
29
|
+
value.to_f
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def as_yaml(value, **)
|
|
33
|
+
value.to_f
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def as_csv(value, **)
|
|
37
|
+
value.to_f
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def as_urlencoded(value, **)
|
|
41
|
+
value.to_s('F')
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def as_toml(value, **)
|
|
45
|
+
value.to_f
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def as_xml_value(value, **)
|
|
49
|
+
value.to_s('F')
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
register(:decimal, Decimal)
|
|
55
|
+
end
|
|
56
|
+
end
|
data/lib/shale/type/time.rb
CHANGED
|
@@ -58,6 +58,17 @@ module Shale
|
|
|
58
58
|
value&.iso8601
|
|
59
59
|
end
|
|
60
60
|
|
|
61
|
+
# Use ISO 8601 format in x-www-form-urlencoded document
|
|
62
|
+
#
|
|
63
|
+
# @param [Time] value
|
|
64
|
+
#
|
|
65
|
+
# @return [String]
|
|
66
|
+
#
|
|
67
|
+
# @api private
|
|
68
|
+
def self.as_urlencoded(value, **)
|
|
69
|
+
value&.iso8601
|
|
70
|
+
end
|
|
71
|
+
|
|
61
72
|
# Use ISO 8601 format in XML document
|
|
62
73
|
#
|
|
63
74
|
# @param [Time] value Value to convert to XML
|
data/lib/shale/type/value.rb
CHANGED
|
@@ -67,6 +67,28 @@ module Shale
|
|
|
67
67
|
value
|
|
68
68
|
end
|
|
69
69
|
|
|
70
|
+
# Extract value from x-www-form-urlencoded document
|
|
71
|
+
#
|
|
72
|
+
# @param [any] value
|
|
73
|
+
#
|
|
74
|
+
# @return [any]
|
|
75
|
+
#
|
|
76
|
+
# @api private
|
|
77
|
+
def of_urlencoded(value, **)
|
|
78
|
+
value
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Convert value to form accepted by x-www-form-urlencoded document
|
|
82
|
+
#
|
|
83
|
+
# @param [any] value
|
|
84
|
+
#
|
|
85
|
+
# @return [any]
|
|
86
|
+
#
|
|
87
|
+
# @api private
|
|
88
|
+
def as_urlencoded(value, **)
|
|
89
|
+
value.to_s
|
|
90
|
+
end
|
|
91
|
+
|
|
70
92
|
# Extract value from YAML document
|
|
71
93
|
#
|
|
72
94
|
# @param [any] value
|
data/lib/shale/version.rb
CHANGED
data/lib/shale.rb
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'yaml'
|
|
4
|
+
require 'bigdecimal'
|
|
4
5
|
|
|
5
6
|
require_relative 'shale/mapper'
|
|
6
7
|
require_relative 'shale/adapter/json'
|
|
7
8
|
require_relative 'shale/type'
|
|
8
9
|
require_relative 'shale/type/boolean'
|
|
9
10
|
require_relative 'shale/type/date'
|
|
11
|
+
require_relative 'shale/type/decimal'
|
|
10
12
|
require_relative 'shale/type/float'
|
|
11
13
|
require_relative 'shale/type/integer'
|
|
12
14
|
require_relative 'shale/type/string'
|
|
@@ -30,6 +32,10 @@ require_relative 'shale/version'
|
|
|
30
32
|
# Shale.json_adapter = MultiJson
|
|
31
33
|
# Shale.json_adapter # => MultiJson
|
|
32
34
|
#
|
|
35
|
+
# @example setting Rack::Utils for handling x-www-form-urlencoded documents
|
|
36
|
+
# Shale.urlencoded_adapter = RackURLEncoded
|
|
37
|
+
# Shale.urlencoded_adapter # => RackURLEncoded
|
|
38
|
+
#
|
|
33
39
|
# @example setting TOML adapter for handling TOML documents
|
|
34
40
|
# require 'shale/adapter/toml_rb'
|
|
35
41
|
#
|
|
@@ -43,8 +49,8 @@ require_relative 'shale/version'
|
|
|
43
49
|
# @example setting Nokogiri adapter for handling XML documents
|
|
44
50
|
# require 'shale/adapter/nokogiri'
|
|
45
51
|
#
|
|
46
|
-
# Shale.xml_adapter = Shale::Adapter::
|
|
47
|
-
# Shale.xml_adapter # => Shale::Adapter::
|
|
52
|
+
# Shale.xml_adapter = Shale::Adapter::Nokogiri
|
|
53
|
+
# Shale.xml_adapter # => Shale::Adapter::Nokogiri
|
|
48
54
|
#
|
|
49
55
|
# @example setting Ox adapter for handling XML documents
|
|
50
56
|
# require 'shale/adapter/ox'
|
|
@@ -71,6 +77,16 @@ module Shale
|
|
|
71
77
|
# @api public
|
|
72
78
|
attr_writer :json_adapter
|
|
73
79
|
|
|
80
|
+
# Set x-www-form-urlencoded adapter
|
|
81
|
+
#
|
|
82
|
+
# @param [.load, .dump] adapter
|
|
83
|
+
#
|
|
84
|
+
# @example
|
|
85
|
+
# Shale.urlencoded_adapter = Shale::Adapter::RackURLEncoded
|
|
86
|
+
#
|
|
87
|
+
# @api public
|
|
88
|
+
attr_accessor :urlencoded_adapter
|
|
89
|
+
|
|
74
90
|
# Set YAML adapter
|
|
75
91
|
#
|
|
76
92
|
# @param [.load, .dump] adapter
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: shale
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Kamil Giszczak
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-12-02 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bigdecimal
|
|
@@ -45,6 +45,7 @@ files:
|
|
|
45
45
|
- lib/shale/adapter/ox.rb
|
|
46
46
|
- lib/shale/adapter/ox/document.rb
|
|
47
47
|
- lib/shale/adapter/ox/node.rb
|
|
48
|
+
- lib/shale/adapter/rack_url_encoded.rb
|
|
48
49
|
- lib/shale/adapter/rexml.rb
|
|
49
50
|
- lib/shale/adapter/rexml/document.rb
|
|
50
51
|
- lib/shale/adapter/rexml/node.rb
|
|
@@ -72,6 +73,7 @@ files:
|
|
|
72
73
|
- lib/shale/schema/compiler/boolean.rb
|
|
73
74
|
- lib/shale/schema/compiler/complex.rb
|
|
74
75
|
- lib/shale/schema/compiler/date.rb
|
|
76
|
+
- lib/shale/schema/compiler/decimal.rb
|
|
75
77
|
- lib/shale/schema/compiler/float.rb
|
|
76
78
|
- lib/shale/schema/compiler/integer.rb
|
|
77
79
|
- lib/shale/schema/compiler/property.rb
|
|
@@ -109,6 +111,7 @@ files:
|
|
|
109
111
|
- lib/shale/type/boolean.rb
|
|
110
112
|
- lib/shale/type/complex.rb
|
|
111
113
|
- lib/shale/type/date.rb
|
|
114
|
+
- lib/shale/type/decimal.rb
|
|
112
115
|
- lib/shale/type/float.rb
|
|
113
116
|
- lib/shale/type/integer.rb
|
|
114
117
|
- lib/shale/type/string.rb
|