lutaml-model 0.3.1 → 0.3.3

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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +0 -5
  3. data/.rubocop_todo.yml +21 -101
  4. data/Gemfile +3 -18
  5. data/README.adoc +145 -6
  6. data/lib/lutaml/model/attribute.rb +15 -2
  7. data/lib/lutaml/model/config.rb +0 -1
  8. data/lib/lutaml/model/json_adapter/json_document.rb +20 -0
  9. data/lib/lutaml/model/json_adapter/json_object.rb +28 -0
  10. data/lib/lutaml/model/json_adapter/{multi_json.rb → multi_json_adapter.rb} +2 -3
  11. data/lib/lutaml/model/json_adapter/{standard.rb → standard_json_adapter.rb} +2 -3
  12. data/lib/lutaml/model/json_adapter.rb +1 -31
  13. data/lib/lutaml/model/key_value_mapping.rb +0 -1
  14. data/lib/lutaml/model/key_value_mapping_rule.rb +0 -1
  15. data/lib/lutaml/model/mapping_hash.rb +0 -2
  16. data/lib/lutaml/model/mapping_rule.rb +0 -1
  17. data/lib/lutaml/model/schema/json_schema.rb +0 -1
  18. data/lib/lutaml/model/schema/relaxng_schema.rb +0 -1
  19. data/lib/lutaml/model/schema/xsd_schema.rb +0 -1
  20. data/lib/lutaml/model/schema/yaml_schema.rb +0 -1
  21. data/lib/lutaml/model/schema.rb +0 -1
  22. data/lib/lutaml/model/serializable.rb +0 -1
  23. data/lib/lutaml/model/serialize.rb +22 -4
  24. data/lib/lutaml/model/toml_adapter/toml_document.rb +20 -0
  25. data/lib/lutaml/model/toml_adapter/toml_object.rb +28 -0
  26. data/lib/lutaml/model/toml_adapter/toml_rb_adapter.rb +2 -3
  27. data/lib/lutaml/model/toml_adapter/tomlib_adapter.rb +2 -3
  28. data/lib/lutaml/model/toml_adapter.rb +0 -31
  29. data/lib/lutaml/model/type/date_time.rb +20 -0
  30. data/lib/lutaml/model/type/json.rb +34 -0
  31. data/lib/lutaml/model/type/time_without_date.rb +4 -3
  32. data/lib/lutaml/model/type.rb +61 -124
  33. data/lib/lutaml/model/version.rb +1 -1
  34. data/lib/lutaml/model/xml_adapter/nokogiri_adapter.rb +9 -8
  35. data/lib/lutaml/model/xml_adapter/oga_adapter.rb +4 -5
  36. data/lib/lutaml/model/xml_adapter/ox_adapter.rb +14 -13
  37. data/lib/lutaml/model/xml_adapter/xml_attribute.rb +27 -0
  38. data/lib/lutaml/model/xml_adapter/xml_document.rb +185 -0
  39. data/lib/lutaml/model/xml_adapter/xml_element.rb +94 -0
  40. data/lib/lutaml/model/xml_adapter/xml_namespace.rb +49 -0
  41. data/lib/lutaml/model/xml_adapter.rb +0 -285
  42. data/lib/lutaml/model/xml_mapping.rb +7 -7
  43. data/lib/lutaml/model/xml_mapping_rule.rb +3 -4
  44. data/lib/lutaml/model/yaml_adapter/standard_yaml_adapter.rb +34 -0
  45. data/lib/lutaml/model/yaml_adapter/yaml_document.rb +20 -0
  46. data/lib/lutaml/model/yaml_adapter/yaml_object.rb +28 -0
  47. data/lib/lutaml/model/yaml_adapter.rb +1 -27
  48. data/lib/lutaml/model.rb +0 -5
  49. metadata +17 -5
  50. data/lib/lutaml/model/xml_namespace.rb +0 -47
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 91bd7beffbcc68f12d2db941b64bb6c353598505f775e8b45b878070de028f89
4
- data.tar.gz: 85462da21104e72066652ae84d7260d8675e6d3027cdb1d0c90e51476e88834f
3
+ metadata.gz: c1c49d91f74f3454b6a0e2aaf34bfef142630fa77e98a509535b53b156f603e7
4
+ data.tar.gz: 12880e7b701bf68c2a77efad6148daeac040a1956f1f1e1a0d790657c79f346c
5
5
  SHA512:
6
- metadata.gz: 2ed73bf2c0ba1a646dbab5ea4f35171786e043f3bd593aec08ee1f07ec4dd3350b2839b93091df4fbf4bfd13ee6db31a22af31d7207b43691f4e530e72f0f303
7
- data.tar.gz: c7b31f2cabd5aea847414bfaa0a7d53b48b2be43059c3501aeac41917844ef128e482072c87ab63a92930f1115e1f7cab22e23dd9e424a2c9d066a4221094f27
6
+ metadata.gz: '08020dbbadaea322601ce9c0536cf3a49aeaa44679d8caf278995d0f7b1c41ba12761897db7f54390bb1288c84ccedc6a0a45bee980926fecca18927a17c8de7'
7
+ data.tar.gz: 363806235f0d4308f127d84590d1b2d2bf2af5adafd27974fa6a8600f5e67afac6efc27f3ee810eea95265d6764b6f4538672f18c5c9a3e243c31f236595fe21
data/.rubocop.yml CHANGED
@@ -2,12 +2,8 @@ inherit_from:
2
2
  - https://raw.githubusercontent.com/riboseinc/oss-guides/master/ci/rubocop.yml
3
3
  - .rubocop_todo.yml
4
4
 
5
- # local repo-specific modifications
6
- # ...
7
- #
8
5
  require:
9
6
  - rubocop-performance
10
- - rubocop-rails
11
7
  - rubocop-rake
12
8
  - rubocop-rspec
13
9
 
@@ -15,5 +11,4 @@ AllCops:
15
11
  TargetRubyVersion: 3.0
16
12
  NewCops: enable
17
13
  Exclude:
18
- - 'lib/sts/mapper.rb'
19
14
  - 'vendor/**/*'
data/.rubocop_todo.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2024-07-31 10:20:40 UTC using RuboCop version 1.64.1.
3
+ # on 2024-08-16 03:28:27 UTC using RuboCop version 1.65.1.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
@@ -14,7 +14,7 @@ Gemspec/RequireMFA:
14
14
  Exclude:
15
15
  - 'lutaml-model.gemspec'
16
16
 
17
- # Offense count: 15
17
+ # Offense count: 32
18
18
  # This cop supports safe autocorrection (--autocorrect).
19
19
  # Configuration parameters: Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
20
20
  # URISchemes: http, https
@@ -22,10 +22,11 @@ Layout/LineLength:
22
22
  Exclude:
23
23
  - 'lib/lutaml/model/serialize.rb'
24
24
  - 'lib/lutaml/model/type.rb'
25
- - 'lib/lutaml/model/xml_adapter.rb'
26
25
  - 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb'
27
26
  - 'lib/lutaml/model/xml_adapter/ox_adapter.rb'
28
- - 'spec/lutaml/model/xml_namespace_spec.rb'
27
+ - 'lib/lutaml/model/xml_adapter/xml_document.rb'
28
+ - 'spec/lutaml/model/delegation_spec.rb'
29
+ - 'spec/lutaml/model/serializable_spec.rb'
29
30
 
30
31
  # Offense count: 20
31
32
  # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes, Max.
@@ -33,9 +34,9 @@ Metrics/AbcSize:
33
34
  Exclude:
34
35
  - 'lib/lutaml/model/serialize.rb'
35
36
  - 'lib/lutaml/model/type.rb'
36
- - 'lib/lutaml/model/xml_adapter.rb'
37
37
  - 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb'
38
38
  - 'lib/lutaml/model/xml_adapter/ox_adapter.rb'
39
+ - 'lib/lutaml/model/xml_adapter/xml_document.rb'
39
40
 
40
41
  # Offense count: 3
41
42
  # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode.
@@ -43,50 +44,35 @@ Metrics/AbcSize:
43
44
  Metrics/BlockLength:
44
45
  Max: 30
45
46
 
46
- # Offense count: 17
47
+ # Offense count: 19
47
48
  # Configuration parameters: AllowedMethods, AllowedPatterns, Max.
48
49
  Metrics/CyclomaticComplexity:
49
50
  Exclude:
50
51
  - 'lib/lutaml/model/serialize.rb'
51
52
  - 'lib/lutaml/model/type.rb'
52
- - 'lib/lutaml/model/xml_adapter.rb'
53
53
  - 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb'
54
54
  - 'lib/lutaml/model/xml_adapter/ox_adapter.rb'
55
+ - 'lib/lutaml/model/xml_adapter/xml_document.rb'
55
56
 
56
- # Offense count: 24
57
+ # Offense count: 27
57
58
  # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
58
59
  Metrics/MethodLength:
59
- Max: 42
60
+ Max: 41
60
61
 
61
- # Offense count: 3
62
+ # Offense count: 4
62
63
  # Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
63
64
  Metrics/ParameterLists:
64
65
  Max: 9
65
66
 
66
- # Offense count: 16
67
+ # Offense count: 17
67
68
  # Configuration parameters: AllowedMethods, AllowedPatterns, Max.
68
69
  Metrics/PerceivedComplexity:
69
70
  Exclude:
70
71
  - 'lib/lutaml/model/serialize.rb'
71
72
  - 'lib/lutaml/model/type.rb'
72
- - 'lib/lutaml/model/xml_adapter.rb'
73
73
  - 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb'
74
74
  - 'lib/lutaml/model/xml_adapter/ox_adapter.rb'
75
-
76
- # Offense count: 2
77
- # This cop supports unsafe autocorrection (--autocorrect-all).
78
- RSpec/BeEq:
79
- Exclude:
80
- - 'spec/lutaml/model/collection_spec.rb'
81
- - 'spec/lutaml/model/simple_model_spec.rb'
82
-
83
- # Offense count: 1
84
- # This cop supports safe autocorrection (--autocorrect).
85
- # Configuration parameters: EnforcedStyle.
86
- # SupportedStyles: be, be_nil
87
- RSpec/BeNil:
88
- Exclude:
89
- - 'spec/lutaml/model_spec.rb'
75
+ - 'lib/lutaml/model/xml_adapter/xml_document.rb'
90
76
 
91
77
  # Offense count: 6
92
78
  # Configuration parameters: Prefixes, AllowedPatterns.
@@ -97,22 +83,10 @@ RSpec/ContextWording:
97
83
  - 'spec/lutaml/model/xml_adapter/oga_adapter_spec.rb'
98
84
  - 'spec/lutaml/model/xml_adapter/ox_adapter_spec.rb'
99
85
 
100
- # Offense count: 18
101
- # This cop supports unsafe autocorrection (--autocorrect-all).
102
- # Configuration parameters: SkipBlocks, EnforcedStyle, OnlyStaticConstants.
103
- # SupportedStyles: described_class, explicit
104
- RSpec/DescribedClass:
105
- Exclude:
106
- - 'spec/address_spec.rb'
107
- - 'spec/lutaml/model/custom_serialization_spec.rb'
108
- - 'spec/lutaml/model/defaults_spec.rb'
109
- - 'spec/lutaml/model/render_nil_spec.rb'
110
- - 'spec/person_spec.rb'
111
-
112
- # Offense count: 60
86
+ # Offense count: 64
113
87
  # Configuration parameters: CountAsOne.
114
88
  RSpec/ExampleLength:
115
- Max: 36
89
+ Max: 35
116
90
 
117
91
  # Offense count: 2
118
92
  # Configuration parameters: Max, AllowedIdentifiers, AllowedPatterns.
@@ -120,27 +94,19 @@ RSpec/IndexedLet:
120
94
  Exclude:
121
95
  - 'spec/address_spec.rb'
122
96
 
123
- # Offense count: 3
124
- # This cop supports safe autocorrection (--autocorrect).
125
- # Configuration parameters: AutoCorrect.
126
- RSpec/LetBeforeExamples:
127
- Exclude:
128
- - 'spec/lutaml/model/defaults_spec.rb'
129
- - 'spec/person_spec.rb'
130
-
131
97
  # Offense count: 4
132
98
  RSpec/MultipleDescribes:
133
99
  Exclude:
134
100
  - 'spec/lutaml/model/json_adapter_spec.rb'
135
101
  - 'spec/lutaml/model/toml_adapter_spec.rb'
102
+ - 'spec/lutaml/model/xml_adapter/xml_namespace_spec.rb'
136
103
  - 'spec/lutaml/model/xml_adapter_spec.rb'
137
- - 'spec/lutaml/model/xml_namespace_spec.rb'
138
104
 
139
- # Offense count: 54
105
+ # Offense count: 63
140
106
  RSpec/MultipleExpectations:
141
107
  Max: 15
142
108
 
143
- # Offense count: 1
109
+ # Offense count: 11
144
110
  # Configuration parameters: AllowSubject.
145
111
  RSpec/MultipleMemoizedHelpers:
146
112
  Max: 9
@@ -149,59 +115,13 @@ RSpec/MultipleMemoizedHelpers:
149
115
  RSpec/PendingWithoutReason:
150
116
  Exclude:
151
117
  - 'spec/lutaml/model/xml_adapter/oga_adapter_spec.rb'
118
+ - 'spec/lutaml/model/xml_adapter/xml_namespace_spec.rb'
152
119
  - 'spec/lutaml/model/xml_adapter_spec.rb'
153
- - 'spec/lutaml/model/xml_namespace_spec.rb'
154
-
155
- # Offense count: 3
156
- # This cop supports safe autocorrection (--autocorrect).
157
- # Configuration parameters: AutoCorrect.
158
- RSpec/ScatteredLet:
159
- Exclude:
160
- - 'spec/lutaml/model/defaults_spec.rb'
161
- - 'spec/person_spec.rb'
162
120
 
163
- # Offense count: 5
121
+ # Offense count: 1
164
122
  # Configuration parameters: Include, CustomTransform, IgnoreMethods, IgnoreMetadata.
165
123
  # Include: **/*_spec.rb
166
124
  RSpec/SpecFilePathFormat:
167
125
  Exclude:
126
+ - '**/spec/routing/**/*'
168
127
  - 'spec/lutaml/model/collection_spec.rb'
169
- - 'spec/lutaml/model/xml_adapter/nokogiri_adapter_spec.rb'
170
- - 'spec/lutaml/model/xml_adapter/oga_adapter_spec.rb'
171
- - 'spec/lutaml/model/xml_adapter/ox_adapter_spec.rb'
172
- - 'spec/lutaml/model/yaml_adapter_spec.rb'
173
-
174
- # Offense count: 2
175
- # This cop supports unsafe autocorrection (--autocorrect-all).
176
- # Configuration parameters: Whitelist, AllowedMethods, AllowedReceivers.
177
- # Whitelist: find_by_sql, find_by_token_for
178
- # AllowedMethods: find_by_sql, find_by_token_for
179
- # AllowedReceivers: Gem::Specification, page
180
- Rails/DynamicFindBy:
181
- Exclude:
182
- - 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb'
183
- - 'lib/lutaml/model/xml_adapter/ox_adapter.rb'
184
-
185
- # Offense count: 1
186
- # This cop supports unsafe autocorrection (--autocorrect-all).
187
- Rails/NegateInclude:
188
- Exclude:
189
- - 'lib/lutaml/model/serialize.rb'
190
-
191
- # Offense count: 2
192
- # This cop supports safe autocorrection (--autocorrect).
193
- # Configuration parameters: NotNilAndNotEmpty, NotBlank, UnlessBlank.
194
- Rails/Present:
195
- Exclude:
196
- - 'lib/lutaml/model/serialize.rb'
197
- - 'lib/lutaml/model/xml_namespace.rb'
198
-
199
- # Offense count: 6
200
- # This cop supports unsafe autocorrection (--autocorrect-all).
201
- # Configuration parameters: EnforcedStyle.
202
- # SupportedStyles: strict, flexible
203
- Rails/TimeZone:
204
- Exclude:
205
- - 'lib/lutaml/model/type.rb'
206
- - 'lib/lutaml/model/type/time_without_date.rb'
207
- - 'spec/person_spec.rb'
data/Gemfile CHANGED
@@ -5,32 +5,17 @@ source "https://rubygems.org"
5
5
  # Specify your gem's dependencies in lutaml-model.gemspec
6
6
  gemspec
7
7
 
8
- gem "rake", "~> 13.0"
9
-
10
- gem "rspec", "~> 3.0"
11
-
12
- gem "rubocop", "~> 1.21"
13
-
14
8
  gem "equivalent-xml"
15
-
16
9
  gem "multi_json"
17
-
18
10
  gem "nokogiri"
19
-
20
11
  gem "oga"
21
-
22
12
  gem "ox"
23
-
24
13
  gem "pry"
25
-
14
+ gem "rake", "~> 13.0"
15
+ gem "rspec", "~> 3.0"
16
+ gem "rubocop", "~> 1.21"
26
17
  gem "rubocop-performance", require: false
27
-
28
- gem "rubocop-rails", require: false
29
-
30
18
  gem "rubocop-rake", require: false
31
-
32
19
  gem "rubocop-rspec", require: false
33
-
34
20
  gem "tomlib"
35
-
36
21
  gem "toml-rb"
data/README.adoc CHANGED
@@ -116,6 +116,68 @@ end
116
116
 
117
117
  == Defining attributes
118
118
 
119
+ === Supported attribute value types
120
+
121
+ Lutaml::Model supports the following attribute types, they can be
122
+ referred by a string, a symbol, or their class constant.
123
+
124
+ Syntax:
125
+
126
+ [source,ruby]
127
+ ----
128
+ attribute :name_of_attribute, {symbol | string | class}
129
+ ----
130
+
131
+ |===
132
+ | String | Symbol | Class name | Actual value class
133
+
134
+ | `String` | `:string` | `Lutaml::Model::Type::String` | `::String`
135
+ | `Integer` | `:integer` | `Lutaml::Model::Type::Integer` | `::Integer`
136
+ | `Float` | `:float` | `Lutaml::Model::Type::Float` | `::Float`
137
+ | `Date` | `:date` | `Lutaml::Model::Type::Date` | `::Date`
138
+ | `Time` | `:time` | `Lutaml::Model::Type::Time` | `::Time`
139
+ | `DateTime` | `:date_time` | `Lutaml::Model::Type::DateTime` | `::DateTime`
140
+ | `TimeWithoutDate` | `:time_without_date` | `Lutaml::Model::Type::TimeWithoutDate` | `::Time`
141
+ | `Boolean` | `:boolean` | `Lutaml::Model::Type::Boolean` | `Boolean`
142
+ | `Decimal` | `:decimal` | `Lutaml::Model::Type::Decimal` | `::BigDecimal`
143
+ | `Hash` | `:hash` | `Lutaml::Model::Type::Hash` | `::Hash`
144
+ | `Uuid` | `:uuid` | `Lutaml::Model::Type::Uuid` | `::String`
145
+ | `Symbol` | `:symbol` | `Lutaml::Model::Type::Symbol` | `Symbol`
146
+ | `Binary` | `:binary` | `Lutaml::Model::Type::Binary` | `Binary`
147
+ | `Url` | `:url` | `Lutaml::Model::Type::Url` | `::URI`
148
+ | `IpAddress` | `:ip_address` | `Lutaml::Model::Type::IpAddress` | `::IPAddr`
149
+ | `Json` | `:json` | `Lutaml::Model::Type::Json` | `::JSON`
150
+
151
+ |===
152
+
153
+
154
+ .Defining attributes with supported types via symbol, string and class
155
+ [example]
156
+ ====
157
+ [source,ruby]
158
+ ----
159
+ class Studio < Lutaml::Model::Serializable
160
+ # The following are equivalent
161
+ attribute :location, :string
162
+ attribute :potter, "String"
163
+ attribute :kiln, Lutaml::Model::Type::String
164
+ end
165
+ ----
166
+
167
+ [source,ruby]
168
+ ----
169
+ > s = Studio.new(location: 'London', potter: 'John Doe', kiln: 'Kiln 1')
170
+ > # <Studio:0x0000000104ac7240 @location="London", @potter="John Doe", @kiln="Kiln 1">
171
+ > s.location
172
+ > # "London"
173
+ > s.potter
174
+ > # "John Doe"
175
+ > s.kiln
176
+ > # "Kiln 1"
177
+ ----
178
+ ====
179
+
180
+
119
181
  === Attribute as a collection
120
182
 
121
183
  Define attributes as collections (arrays or hashes) to store multiple values
@@ -339,6 +401,42 @@ end
339
401
  ----
340
402
  ====
341
403
 
404
+ If an element is mapped to a model object with the XML `root` tag name set, the
405
+ mapped tag name will be used as the root name, overriding the root name.
406
+
407
+ .The mapped tag name is used as the root name
408
+ [example]
409
+ ====
410
+ [source,ruby]
411
+ ----
412
+ class RecordDate < Lutaml::Model::Serializable
413
+ attribute :content, :string
414
+
415
+ xml do
416
+ root "recordDate"
417
+ map_content to: :content
418
+ end
419
+ end
420
+
421
+ class OriginInfo < Lutaml::Model::Serializable
422
+ attribute :date_issued, RecordDate, collection: true
423
+
424
+ xml do
425
+ root "originInfo"
426
+ map_element "dateIssued", to: :date_issued
427
+ end
428
+ end
429
+ ----
430
+
431
+ [source,ruby]
432
+ ----
433
+ > RecordDate.new(date: "2021-01-01").to_xml
434
+ > #<recordDate>2021-01-01</recordDate>
435
+ > OriginInfo.new(date_issued: [RecordDate.new(date: "2021-01-01")]).to_xml
436
+ > #<originInfo><dateIssued>2021-01-01</dateIssued></originInfo>
437
+ ----
438
+ ====
439
+
342
440
  ==== Mapping attributes
343
441
 
344
442
  The `map_attribute` method maps an XML attribute to a data model attribute.
@@ -1200,6 +1298,28 @@ In this example:
1200
1298
  Lutaml::Model uses an adapter pattern to support multiple libraries for each
1201
1299
  serialization format.
1202
1300
 
1301
+ You will need to specify the configuration for the adapter you want to use. The
1302
+ easiest way is to copy and paste the following configuration into your code.
1303
+
1304
+ The default configuration is as follows:
1305
+
1306
+ [source,ruby]
1307
+ ----
1308
+ require 'lutaml/model'
1309
+ require 'lutaml/model/xml_adapter/nokogiri_adapter'
1310
+ require 'lutaml/model/json_adapter/standard_json_adapter'
1311
+ require 'lutaml/model/toml_adapter/toml_rb_adapter'
1312
+ require 'lutaml/model/yaml_adapter/standard_yaml_adapter'
1313
+
1314
+ Lutaml::Model::Config.configure do |config|
1315
+ config.xml_adapter = Lutaml::Model::XmlAdapter::NokogiriAdapter
1316
+ config.yaml_adapter = Lutaml::Model::YamlAdapter::StandardYamlAdapter
1317
+ config.json_adapter = Lutaml::Model::JsonAdapter::StandardJsonAdapter
1318
+ config.toml_adapter = Lutaml::Model::TomlAdapter::TomlRbAdapter
1319
+ end
1320
+ ----
1321
+
1322
+
1203
1323
  === XML
1204
1324
 
1205
1325
  Lutaml::Model supports the following XML adapters:
@@ -1242,6 +1362,25 @@ end
1242
1362
  ----
1243
1363
 
1244
1364
 
1365
+ === YAML
1366
+
1367
+ Lutaml::Model supports only one YAML adapter.
1368
+
1369
+ * YAML (default)
1370
+
1371
+ .Using the YAML adapter
1372
+ [source,ruby]
1373
+ ----
1374
+ require 'lutaml/model'
1375
+
1376
+ Lutaml::Model::Config.configure do |config|
1377
+ require 'lutaml/model/yaml_adapter/standard_yaml_adapter'
1378
+ config.yaml_adapter = Lutaml::Model::YamlAdapter::StandardYamlAdapter
1379
+ end
1380
+ ----
1381
+
1382
+
1383
+
1245
1384
  === JSON
1246
1385
 
1247
1386
  Lutaml::Model supports the following JSON adapters:
@@ -1255,8 +1394,8 @@ Lutaml::Model supports the following JSON adapters:
1255
1394
  require 'lutaml/model'
1256
1395
 
1257
1396
  Lutaml::Model::Config.configure do |config|
1258
- require 'lutaml/model/json_adapter/standard'
1259
- config.json_adapter = Lutaml::Model::JsonAdapter::StandardDocument
1397
+ require 'lutaml/model/json_adapter/standard_json_adapter'
1398
+ config.json_adapter = Lutaml::Model::JsonAdapter::StandardJsonAdapter
1260
1399
  end
1261
1400
  ----
1262
1401
 
@@ -1266,8 +1405,8 @@ end
1266
1405
  require 'lutaml/model'
1267
1406
 
1268
1407
  Lutaml::Model::Config.configure do |config|
1269
- require 'lutaml/model/json_adapter/multi_json'
1270
- config.json_adapter = Lutaml::Model::JsonAdapter::MultiJsonDocument
1408
+ require 'lutaml/model/json_adapter/multi_json_adapter'
1409
+ config.json_adapter = Lutaml::Model::JsonAdapter::MultiJsonAdapter
1271
1410
  end
1272
1411
  ----
1273
1412
 
@@ -1285,7 +1424,7 @@ require 'lutaml/model'
1285
1424
 
1286
1425
  Lutaml::Model::Config.configure do |config|
1287
1426
  require 'lutaml/model/toml_adapter/toml_rb_adapter'
1288
- config.toml_adapter = Lutaml::Model::TomlAdapter::TomlRbDocument
1427
+ config.toml_adapter = Lutaml::Model::TomlAdapter::TomlRbAdapter
1289
1428
  end
1290
1429
  ----
1291
1430
 
@@ -1295,7 +1434,7 @@ end
1295
1434
  require 'lutaml/model'
1296
1435
 
1297
1436
  Lutaml::Model::Config.configure do |config|
1298
- config.toml_adapter = Lutaml::Model::TomlAdapter::TomlibDocument
1437
+ config.toml_adapter = Lutaml::Model::TomlAdapter::TomlibAdapter
1299
1438
  require 'lutaml/model/toml_adapter/tomlib_adapter'
1300
1439
  end
1301
1440
  ----
@@ -1,4 +1,3 @@
1
- # lib/lutaml/model/attribute.rb
2
1
  module Lutaml
3
2
  module Model
4
3
  class Attribute
@@ -6,7 +5,8 @@ module Lutaml
6
5
 
7
6
  def initialize(name, type, options = {})
8
7
  @name = name
9
- @type = type
8
+ @type = cast_type(type)
9
+
10
10
  @options = options
11
11
 
12
12
  if collection? && !options[:default]
@@ -14,6 +14,19 @@ module Lutaml
14
14
  end
15
15
  end
16
16
 
17
+ def cast_type(type)
18
+ case type
19
+ when Class
20
+ type
21
+ when String
22
+ Type.const_get(type)
23
+ when Symbol
24
+ Type.const_get(type.to_s.split("_").collect(&:capitalize).join)
25
+ end
26
+ rescue NameError
27
+ raise ArgumentError, "Unknown Lutaml::Model::Type: #{type}"
28
+ end
29
+
17
30
  def collection?
18
31
  options[:collection] || false
19
32
  end
@@ -1,4 +1,3 @@
1
- # lib/lutaml/model/config.rb
2
1
  module Lutaml
3
2
  module Model
4
3
  module Config
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "json_object"
4
+
5
+ module Lutaml
6
+ module Model
7
+ module JsonAdapter
8
+ # Base class for JSON documents
9
+ class JsonDocument < JsonObject
10
+ def self.parse(json)
11
+ raise NotImplementedError, "Subclasses must implement `parse`."
12
+ end
13
+
14
+ def to_json(*args)
15
+ raise NotImplementedError, "Subclasses must implement `to_json`."
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module Model
5
+ module JsonAdapter
6
+ # Base class for JSON objects
7
+ class JsonObject
8
+ attr_reader :attributes
9
+
10
+ def initialize(attributes = {})
11
+ @attributes = attributes
12
+ end
13
+
14
+ def [](key)
15
+ @attributes[key]
16
+ end
17
+
18
+ def []=(key, value)
19
+ @attributes[key] = value
20
+ end
21
+
22
+ def to_h
23
+ @attributes
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,11 +1,10 @@
1
- # lib/lutaml/model/json_adapter/multi_json.rb
2
1
  require "multi_json"
3
- require_relative "../json_adapter"
2
+ require_relative "json_document"
4
3
 
5
4
  module Lutaml
6
5
  module Model
7
6
  module JsonAdapter
8
- class MultiJsonDocument < Document
7
+ class MultiJsonAdapter < JsonDocument
9
8
  def self.parse(json)
10
9
  data = MultiJson.load(json)
11
10
  new(data)
@@ -1,11 +1,10 @@
1
- # lib/lutaml/model/json_adapter/standard.rb
2
1
  require "json"
3
- require_relative "../json_adapter"
2
+ require_relative "json_document"
4
3
 
5
4
  module Lutaml
6
5
  module Model
7
6
  module JsonAdapter
8
- class StandardDocument < Document
7
+ class StandardJsonAdapter < JsonDocument
9
8
  def self.parse(json)
10
9
  attributes = JSON.parse(json, create_additions: false)
11
10
  new(attributes)
@@ -1,38 +1,8 @@
1
- # lib/lutaml/model/json_adapter.rb
2
- require "json"
1
+ # frozen_string_literal: true
3
2
 
4
3
  module Lutaml
5
4
  module Model
6
5
  module JsonAdapter
7
- class JsonObject
8
- attr_reader :attributes
9
-
10
- def initialize(attributes = {})
11
- @attributes = attributes
12
- end
13
-
14
- def [](key)
15
- @attributes[key]
16
- end
17
-
18
- def []=(key, value)
19
- @attributes[key] = value
20
- end
21
-
22
- def to_h
23
- @attributes
24
- end
25
- end
26
-
27
- class Document < JsonObject
28
- def self.parse(json)
29
- raise NotImplementedError, "Subclasses must implement `parse`."
30
- end
31
-
32
- def to_json(*args)
33
- raise NotImplementedError, "Subclasses must implement `to_json`."
34
- end
35
- end
36
6
  end
37
7
  end
38
8
  end
@@ -1,4 +1,3 @@
1
- # lib/lutaml/model/key_value_mapping.rb
2
1
  require_relative "key_value_mapping_rule"
3
2
 
4
3
  module Lutaml
@@ -1,4 +1,3 @@
1
- # lib/lutaml/model/key_value_mapping_rule.rb
2
1
  require_relative "mapping_rule"
3
2
 
4
3
  module Lutaml
@@ -1,5 +1,3 @@
1
- # lib/lutaml/model/type.rb
2
-
3
1
  module Lutaml
4
2
  module Model
5
3
  class MappingHash < Hash
@@ -1,4 +1,3 @@
1
- # lib/lutaml/model/mapping_rule.rb
2
1
  module Lutaml
3
2
  module Model
4
3
  class MappingRule
@@ -1,4 +1,3 @@
1
- # lib/lutaml/model/schema/json_schema.rb
2
1
  require "json"
3
2
 
4
3
  module Lutaml
@@ -1,4 +1,3 @@
1
- # lib/lutaml/model/schema/relaxng_schema.rb
2
1
  require "nokogiri"
3
2
 
4
3
  module Lutaml