shale 1.0.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +20 -0
  3. data/README.md +263 -69
  4. data/lib/shale/adapter/json.rb +10 -7
  5. data/lib/shale/adapter/nokogiri.rb +1 -1
  6. data/lib/shale/adapter/ox.rb +2 -1
  7. data/lib/shale/adapter/toml_rb.rb +5 -3
  8. data/lib/shale/adapter/tomlib.rb +36 -0
  9. data/lib/shale/error.rb +26 -1
  10. data/lib/shale/mapper.rb +36 -30
  11. data/lib/shale/mapping/descriptor/dict.rb +10 -1
  12. data/lib/shale/mapping/dict.rb +4 -3
  13. data/lib/shale/mapping/dict_base.rb +29 -2
  14. data/lib/shale/schema/json_generator/base.rb +9 -3
  15. data/lib/shale/schema/json_generator/boolean.rb +2 -1
  16. data/lib/shale/schema/json_generator/collection.rb +18 -2
  17. data/lib/shale/schema/json_generator/date.rb +3 -1
  18. data/lib/shale/schema/json_generator/float.rb +7 -1
  19. data/lib/shale/schema/json_generator/integer.rb +7 -1
  20. data/lib/shale/schema/json_generator/object.rb +12 -2
  21. data/lib/shale/schema/json_generator/string.rb +6 -1
  22. data/lib/shale/schema/json_generator/time.rb +3 -1
  23. data/lib/shale/schema/json_generator/value.rb +2 -1
  24. data/lib/shale/schema/json_generator.rb +7 -3
  25. data/lib/shale/schema/xml_compiler.rb +12 -12
  26. data/lib/shale/type/boolean.rb +2 -0
  27. data/lib/shale/type/complex.rb +43 -18
  28. data/lib/shale/type/date.rb +2 -0
  29. data/lib/shale/type/float.rb +2 -0
  30. data/lib/shale/type/integer.rb +2 -0
  31. data/lib/shale/type/string.rb +2 -0
  32. data/lib/shale/type/time.rb +2 -0
  33. data/lib/shale/type.rb +56 -0
  34. data/lib/shale/utils.rb +1 -1
  35. data/lib/shale/version.rb +1 -1
  36. data/lib/shale.rb +18 -20
  37. data/shale.gemspec +3 -1
  38. metadata +21 -5
@@ -257,13 +257,14 @@ module Shale
257
257
  # @param [Array<Symbol>] only
258
258
  # @param [Array<Symbol>] except
259
259
  # @param [any] context
260
+ # @param [Hash] json_options
260
261
  #
261
262
  # @return [model instance]
262
263
  #
263
264
  # @api public
264
- def from_json(json, only: nil, except: nil, context: nil)
265
+ def from_json(json, only: nil, except: nil, context: nil, **json_options)
265
266
  of_json(
266
- Shale.json_adapter.load(json),
267
+ Shale.json_adapter.load(json, **json_options),
267
268
  only: only,
268
269
  except: except,
269
270
  context: context
@@ -277,14 +278,15 @@ module Shale
277
278
  # @param [Array<Symbol>] except
278
279
  # @param [any] context
279
280
  # @param [true, false] pretty
281
+ # @param [Hash] json_options
280
282
  #
281
283
  # @return [String]
282
284
  #
283
285
  # @api public
284
- def to_json(instance, only: nil, except: nil, context: nil, pretty: false)
286
+ def to_json(instance, only: nil, except: nil, context: nil, pretty: false, **json_options)
285
287
  Shale.json_adapter.dump(
286
288
  as_json(instance, only: only, except: except, context: context),
287
- pretty: pretty
289
+ **json_options.merge(pretty: pretty)
288
290
  )
289
291
  end
290
292
 
@@ -294,13 +296,14 @@ module Shale
294
296
  # @param [Array<Symbol>] only
295
297
  # @param [Array<Symbol>] except
296
298
  # @param [any] context
299
+ # @param [Hash] yaml_options
297
300
  #
298
301
  # @return [model instance]
299
302
  #
300
303
  # @api public
301
- def from_yaml(yaml, only: nil, except: nil, context: nil)
304
+ def from_yaml(yaml, only: nil, except: nil, context: nil, **yaml_options)
302
305
  of_yaml(
303
- Shale.yaml_adapter.load(yaml),
306
+ Shale.yaml_adapter.load(yaml, **yaml_options),
304
307
  only: only,
305
308
  except: except,
306
309
  context: context
@@ -313,13 +316,15 @@ module Shale
313
316
  # @param [Array<Symbol>] only
314
317
  # @param [Array<Symbol>] except
315
318
  # @param [any] context
319
+ # @param [Hash] yaml_options
316
320
  #
317
321
  # @return [String]
318
322
  #
319
323
  # @api public
320
- def to_yaml(instance, only: nil, except: nil, context: nil)
324
+ def to_yaml(instance, only: nil, except: nil, context: nil, **yaml_options)
321
325
  Shale.yaml_adapter.dump(
322
- as_yaml(instance, only: only, except: except, context: context)
326
+ as_yaml(instance, only: only, except: except, context: context),
327
+ **yaml_options
323
328
  )
324
329
  end
325
330
 
@@ -329,14 +334,15 @@ module Shale
329
334
  # @param [Array<Symbol>] only
330
335
  # @param [Array<Symbol>] except
331
336
  # @param [any] context
337
+ # @param [Hash] toml_options
332
338
  #
333
339
  # @return [model instance]
334
340
  #
335
341
  # @api public
336
- def from_toml(toml, only: nil, except: nil, context: nil)
342
+ def from_toml(toml, only: nil, except: nil, context: nil, **toml_options)
337
343
  validate_toml_adapter
338
344
  of_toml(
339
- Shale.toml_adapter.load(toml),
345
+ Shale.toml_adapter.load(toml, **toml_options),
340
346
  only: only,
341
347
  except: except,
342
348
  context: context
@@ -349,14 +355,16 @@ module Shale
349
355
  # @param [Array<Symbol>] only
350
356
  # @param [Array<Symbol>] except
351
357
  # @param [any] context
358
+ # @param [Hash] toml_options
352
359
  #
353
360
  # @return [String]
354
361
  #
355
362
  # @api public
356
- def to_toml(instance, only: nil, except: nil, context: nil)
363
+ def to_toml(instance, only: nil, except: nil, context: nil, **toml_options)
357
364
  validate_toml_adapter
358
365
  Shale.toml_adapter.dump(
359
- as_toml(instance, only: only, except: except, context: context)
366
+ as_toml(instance, only: only, except: except, context: context),
367
+ **toml_options
360
368
  )
361
369
  end
362
370
 
@@ -373,6 +381,7 @@ module Shale
373
381
  #
374
382
  # @api public
375
383
  def from_csv(csv, only: nil, except: nil, context: nil, headers: false, **csv_options)
384
+ validate_csv_adapter
376
385
  data = Shale.csv_adapter.load(csv, **csv_options.merge(headers: csv_mapping.keys.keys))
377
386
 
378
387
  data.shift if headers
@@ -398,6 +407,7 @@ module Shale
398
407
  #
399
408
  # @api public
400
409
  def to_csv(instance, only: nil, except: nil, context: nil, headers: false, **csv_options)
410
+ validate_csv_adapter
401
411
  data = as_csv([*instance], only: only, except: except, context: context)
402
412
 
403
413
  cols = csv_mapping.keys.values
@@ -892,6 +902,15 @@ module Shale
892
902
  raise AdapterError, XML_ADAPTER_NOT_SET_MESSAGE unless Shale.xml_adapter
893
903
  end
894
904
 
905
+ # Validate CSV adapter
906
+ #
907
+ # @raise [AdapterError]
908
+ #
909
+ # @api private
910
+ def validate_csv_adapter
911
+ raise AdapterError, CSV_ADAPTER_NOT_SET_MESSAGE unless Shale.csv_adapter
912
+ end
913
+
895
914
  # Convert array with attributes to a hash
896
915
  #
897
916
  # @param [Array] ary
@@ -989,17 +1008,19 @@ module Shale
989
1008
  # @param [Array<Symbol>] except
990
1009
  # @param [any] context
991
1010
  # @param [true, false] pretty
1011
+ # @param [Hash] json_options
992
1012
  #
993
1013
  # @return [String]
994
1014
  #
995
1015
  # @api public
996
- def to_json(only: nil, except: nil, context: nil, pretty: false)
1016
+ def to_json(only: nil, except: nil, context: nil, pretty: false, **json_options)
997
1017
  self.class.to_json(
998
1018
  self,
999
1019
  only: only,
1000
1020
  except: except,
1001
1021
  context: context,
1002
- pretty: pretty
1022
+ pretty: pretty,
1023
+ **json_options
1003
1024
  )
1004
1025
  end
1005
1026
 
@@ -1008,12 +1029,13 @@ module Shale
1008
1029
  # @param [Array<Symbol>] only
1009
1030
  # @param [Array<Symbol>] except
1010
1031
  # @param [any] context
1032
+ # @param [Hash] yaml_options
1011
1033
  #
1012
1034
  # @return [String]
1013
1035
  #
1014
1036
  # @api public
1015
- def to_yaml(only: nil, except: nil, context: nil)
1016
- self.class.to_yaml(self, only: only, except: except, context: context)
1037
+ def to_yaml(only: nil, except: nil, context: nil, **yaml_options)
1038
+ self.class.to_yaml(self, only: only, except: except, context: context, **yaml_options)
1017
1039
  end
1018
1040
 
1019
1041
  # Convert Object to TOML
@@ -1021,12 +1043,13 @@ module Shale
1021
1043
  # @param [Array<Symbol>] only
1022
1044
  # @param [Array<Symbol>] except
1023
1045
  # @param [any] context
1046
+ # @param [Hash] toml_options
1024
1047
  #
1025
1048
  # @return [String]
1026
1049
  #
1027
1050
  # @api public
1028
- def to_toml(only: nil, except: nil, context: nil)
1029
- self.class.to_toml(self, only: only, except: except, context: context)
1051
+ def to_toml(only: nil, except: nil, context: nil, **toml_options)
1052
+ self.class.to_toml(self, only: only, except: except, context: context, **toml_options)
1030
1053
  end
1031
1054
 
1032
1055
  # Convert Object to CSV
@@ -1034,6 +1057,8 @@ module Shale
1034
1057
  # @param [Array<Symbol>] only
1035
1058
  # @param [Array<Symbol>] except
1036
1059
  # @param [any] context
1060
+ # @param [true, false] headers
1061
+ # @param [Hash] csv_options
1037
1062
  #
1038
1063
  # @return [String]
1039
1064
  #
@@ -69,5 +69,7 @@ module Shale
69
69
  value&.iso8601
70
70
  end
71
71
  end
72
+
73
+ register(:date, Date)
72
74
  end
73
75
  end
@@ -25,5 +25,7 @@ module Shale
25
25
  end
26
26
  end
27
27
  end
28
+
29
+ register(:float, Float)
28
30
  end
29
31
  end
@@ -17,5 +17,7 @@ module Shale
17
17
  value&.to_i
18
18
  end
19
19
  end
20
+
21
+ register(:integer, Integer)
20
22
  end
21
23
  end
@@ -17,5 +17,7 @@ module Shale
17
17
  value&.to_s
18
18
  end
19
19
  end
20
+
21
+ register(:string, String)
20
22
  end
21
23
  end
@@ -69,5 +69,7 @@ module Shale
69
69
  value&.iso8601
70
70
  end
71
71
  end
72
+
73
+ register(:time, Time)
72
74
  end
73
75
  end
data/lib/shale/type.rb ADDED
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shale
4
+ module Type
5
+ class << self
6
+ # Register a symbol alias for a Shale::Type::Value class
7
+ #
8
+ # @param [Symbol] type Short type alias
9
+ # @param [Shale::Type::Value] klass Class to register
10
+ #
11
+ # @raise [NotATypeValueError] when klass is not a Shale::Type::Value
12
+ #
13
+ # @example
14
+ # class UnixTimestamp < Shale::Type::Value
15
+ # def self.cast(value)
16
+ # Time.at(value.to_i)
17
+ # end
18
+ # end
19
+ #
20
+ # Shale::Type.register(:unix_timestamp, UnixTimestamp)
21
+ #
22
+ # @api public
23
+ def register(type, klass)
24
+ @registry ||= {}
25
+
26
+ unless klass < Value
27
+ raise NotATypeValueError, "class '#{klass}' is not a valid Shale::Type::Value"
28
+ end
29
+
30
+ @registry[type] = klass
31
+ end
32
+
33
+ # Lookup a Shale::Type::Value class by type alias
34
+ #
35
+ # @param [Symbol] type Type alias
36
+ #
37
+ # @raise [UnknownTypeError] when type is not registered
38
+ #
39
+ # @return [Shale::Type::Value] Class registered for type
40
+ #
41
+ # @example
42
+ #
43
+ # Shale::Type.lookup(:unix_timestamp)
44
+ # # => UnixTimestamp
45
+ #
46
+ # @api public
47
+ def lookup(type)
48
+ klass = @registry[type]
49
+
50
+ raise UnknownTypeError, "unknown type '#{type}'" unless klass
51
+
52
+ klass
53
+ end
54
+ end
55
+ end
56
+ end
data/lib/shale/utils.rb CHANGED
@@ -31,7 +31,7 @@ module Shale
31
31
  # @api private
32
32
  def self.classify(str)
33
33
  # names may include a period, which will need to be stripped out
34
- str = str.to_s.gsub(/\./, '')
34
+ str = str.to_s.gsub('.', '')
35
35
 
36
36
  str = str.sub(/^[a-z\d]*/) { |match| upcase_first(match) || match }
37
37
 
data/lib/shale/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Shale
4
4
  # @api private
5
- VERSION = '1.0.0'
5
+ VERSION = '1.2.0'
6
6
  end
data/lib/shale.rb CHANGED
@@ -3,8 +3,8 @@
3
3
  require 'yaml'
4
4
 
5
5
  require_relative 'shale/mapper'
6
- require_relative 'shale/adapter/csv'
7
6
  require_relative 'shale/adapter/json'
7
+ require_relative 'shale/type'
8
8
  require_relative 'shale/type/boolean'
9
9
  require_relative 'shale/type/date'
10
10
  require_relative 'shale/type/float'
@@ -52,6 +52,12 @@ require_relative 'shale/version'
52
52
  # Shale.xml_adapter = Shale::Adapter::Ox
53
53
  # Shale.xml_adapter # => Shale::Adapter::Ox
54
54
  #
55
+ # @example setting CSV adapter for handling CSV documents
56
+ # require 'shale/adapter/csv'
57
+ #
58
+ # Shale.csv_adapter = Shale::Adapter::CSV
59
+ # Shale.csv_adapter # => Shale::Adapter::CSV
60
+ #
55
61
  # @api public
56
62
  module Shale
57
63
  class << self
@@ -75,16 +81,17 @@ module Shale
75
81
  # @api public
76
82
  attr_writer :yaml_adapter
77
83
 
78
- # TOML adapter accessor.
84
+ # TOML adapter accessor. Available adapters are Shale::Adapter::Tomlib
85
+ # and Shale::Adapter::TomRB
79
86
  #
80
- # @param [@see Shale::Adapter::TomlRB] adapter
87
+ # @param [@see Shale::Adapter::Tomlib] adapter
81
88
  #
82
89
  # @example setting adapter
83
- # Shale.toml_adapter = Shale::Adapter::TomlRB
90
+ # Shale.toml_adapter = Shale::Adapter::Tomlib
84
91
  #
85
92
  # @example getting adapter
86
93
  # Shale.toml_adapter
87
- # # => Shale::Adapter::TomlRB
94
+ # # => Shale::Adapter::Tomlib
88
95
  #
89
96
  # @api public
90
97
  attr_accessor :toml_adapter
@@ -93,11 +100,15 @@ module Shale
93
100
  #
94
101
  # @param [.load, .dump] adapter
95
102
  #
96
- # @example
103
+ # @example setting adapter
97
104
  # Shale.csv_adapter = Shale::Adapter::CSV
98
105
  #
106
+ # @example getting adapter
107
+ # Shale.csv_adapter
108
+ # # => Shale::Adapter::CSV
109
+ #
99
110
  # @api public
100
- attr_writer :csv_adapter
111
+ attr_accessor :csv_adapter
101
112
 
102
113
  # XML adapter accessor. Available adapters are Shale::Adapter::REXML,
103
114
  # Shale::Adapter::Nokogiri and Shale::Adapter::Ox
@@ -139,18 +150,5 @@ module Shale
139
150
  def yaml_adapter
140
151
  @yaml_adapter || YAML
141
152
  end
142
-
143
- # Return CSV adapter. By default CSV is used
144
- #
145
- # @return [.load, .dump]
146
- #
147
- # @example
148
- # Shale.csv_adapter
149
- # # => Shale::Adapter::CSV
150
- #
151
- # @api public
152
- def csv_adapter
153
- @csv_adapter || Adapter::CSV
154
- end
155
153
  end
156
154
  end
data/shale.gemspec CHANGED
@@ -26,5 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.bindir = 'exe'
27
27
  spec.executables = 'shaleb'
28
28
 
29
- spec.required_ruby_version = '>= 2.6.0'
29
+ spec.add_runtime_dependency 'bigdecimal'
30
+
31
+ spec.required_ruby_version = '>= 3.0.0'
30
32
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shale
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.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: 2023-07-15 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-10-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bigdecimal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: Ruby object mapper and serializer for XML, JSON, TOML, CSV and YAML.
14
28
  email:
15
29
  - beerkg@gmail.com
@@ -35,6 +49,7 @@ files:
35
49
  - lib/shale/adapter/rexml/document.rb
36
50
  - lib/shale/adapter/rexml/node.rb
37
51
  - lib/shale/adapter/toml_rb.rb
52
+ - lib/shale/adapter/tomlib.rb
38
53
  - lib/shale/attribute.rb
39
54
  - lib/shale/error.rb
40
55
  - lib/shale/mapper.rb
@@ -90,6 +105,7 @@ files:
90
105
  - lib/shale/schema/xml_generator/schema.rb
91
106
  - lib/shale/schema/xml_generator/typed_attribute.rb
92
107
  - lib/shale/schema/xml_generator/typed_element.rb
108
+ - lib/shale/type.rb
93
109
  - lib/shale/type/boolean.rb
94
110
  - lib/shale/type/complex.rb
95
111
  - lib/shale/type/date.rb
@@ -118,14 +134,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
134
  requirements:
119
135
  - - ">="
120
136
  - !ruby/object:Gem::Version
121
- version: 2.6.0
137
+ version: 3.0.0
122
138
  required_rubygems_version: !ruby/object:Gem::Requirement
123
139
  requirements:
124
140
  - - ">="
125
141
  - !ruby/object:Gem::Version
126
142
  version: '0'
127
143
  requirements: []
128
- rubygems_version: 3.4.10
144
+ rubygems_version: 3.5.3
129
145
  signing_key:
130
146
  specification_version: 4
131
147
  summary: Ruby object mapper and serializer for XML, JSON, TOML, CSV and YAML.