lutaml-model 0.3.27 → 0.3.29

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: e00c3fab326d719d8341687a8411797d60ccaf54820380ba7a2dacead6a89792
4
- data.tar.gz: e46c1dd3d50f6b2bea7da738d94ace488655205deb049170057490f6d1526bf8
3
+ metadata.gz: fba70c8a3fca206b3109603109812019773027580c47b2e3615d873df6e0ce2d
4
+ data.tar.gz: 21313fbfb52c29c620eb1020672d337689eed963c25fe56c20baeef139e569b5
5
5
  SHA512:
6
- metadata.gz: ab260d59203839c5fd9b1aa670501c1953c741fe75f0f7ab7261795c54cd19ed6de9807f9b8252c83ce9eee8cf2e6a9116087066e464ca1f35b8b46e29082351
7
- data.tar.gz: c579a87612ce8e249edcca0c79f0ee2550d40af899e31ee2405ffdd48509e584df449c19bb3489a6871cdf4dfc08c4bc51f482422ea9a7d91f02d706d653cac6
6
+ metadata.gz: 274739542b8473ae58b4fc6b78e1152aaef604a55f3c9db0ec5678743573d683b4afdd2429d97d6a24c03fdb2c14a72904bf4e057f480e6bafc635a21fde085a
7
+ data.tar.gz: b1a7ad49313463676b3afe8732f94ed8ab37f79935f33449c70011bd8a8449934425aa27b9dd25d2a7e25a064afa1e665bf95f8b2eac4ec7cd63be4bcfdb7b8d
data/.rubocop_todo.yml CHANGED
@@ -1,12 +1,12 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2024-11-11 10:52:30 UTC using RuboCop version 1.68.0.
3
+ # on 2024-11-25 13:01:09 UTC using RuboCop version 1.66.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
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 132
9
+ # Offense count: 185
10
10
  # This cop supports safe autocorrection (--autocorrect).
11
11
  # Configuration parameters: Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
12
12
  # URISchemes: http, https
@@ -26,13 +26,12 @@ Lint/ConstantDefinitionInBlock:
26
26
  - 'spec/lutaml/model/validation_spec.rb'
27
27
  - 'spec/lutaml/model/xml_adapter/xml_namespace_spec.rb'
28
28
 
29
- # Offense count: 2
29
+ # Offense count: 1
30
30
  Lint/DuplicateMethods:
31
31
  Exclude:
32
- - 'lib/lutaml/model/attribute.rb'
33
32
  - 'lib/lutaml/model/type/float.rb'
34
33
 
35
- # Offense count: 36
34
+ # Offense count: 37
36
35
  # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes, Max.
37
36
  Metrics/AbcSize:
38
37
  Exclude:
@@ -45,6 +44,7 @@ Metrics/AbcSize:
45
44
  - 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb'
46
45
  - 'lib/lutaml/model/xml_adapter/ox_adapter.rb'
47
46
  - 'lib/lutaml/model/xml_adapter/xml_document.rb'
47
+ - 'lib/lutaml/model/xml_mapping.rb'
48
48
  - 'lib/lutaml/model/xml_mapping_rule.rb'
49
49
 
50
50
  # Offense count: 6
@@ -53,7 +53,7 @@ Metrics/AbcSize:
53
53
  Metrics/BlockLength:
54
54
  Max: 47
55
55
 
56
- # Offense count: 26
56
+ # Offense count: 28
57
57
  # Configuration parameters: AllowedMethods, AllowedPatterns, Max.
58
58
  Metrics/CyclomaticComplexity:
59
59
  Exclude:
@@ -65,7 +65,7 @@ Metrics/CyclomaticComplexity:
65
65
  - 'lib/lutaml/model/xml_adapter/ox_adapter.rb'
66
66
  - 'lib/lutaml/model/xml_adapter/xml_document.rb'
67
67
 
68
- # Offense count: 53
68
+ # Offense count: 55
69
69
  # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
70
70
  Metrics/MethodLength:
71
71
  Max: 46
@@ -75,7 +75,7 @@ Metrics/MethodLength:
75
75
  Metrics/ParameterLists:
76
76
  Max: 14
77
77
 
78
- # Offense count: 23
78
+ # Offense count: 25
79
79
  # Configuration parameters: AllowedMethods, AllowedPatterns, Max.
80
80
  Metrics/PerceivedComplexity:
81
81
  Exclude:
@@ -87,7 +87,7 @@ Metrics/PerceivedComplexity:
87
87
  - 'lib/lutaml/model/xml_adapter/ox_adapter.rb'
88
88
  - 'lib/lutaml/model/xml_adapter/xml_document.rb'
89
89
 
90
- # Offense count: 7
90
+ # Offense count: 8
91
91
  # Configuration parameters: Prefixes, AllowedPatterns.
92
92
  # Prefixes: when, with, without
93
93
  RSpec/ContextWording:
@@ -97,7 +97,7 @@ RSpec/ContextWording:
97
97
  - 'spec/lutaml/model/xml_adapter/xml_namespace_spec.rb'
98
98
  - 'spec/lutaml/model/xml_mapping_spec.rb'
99
99
 
100
- # Offense count: 117
100
+ # Offense count: 127
101
101
  # Configuration parameters: CountAsOne.
102
102
  RSpec/ExampleLength:
103
103
  Max: 54
@@ -127,16 +127,16 @@ RSpec/MultipleDescribes:
127
127
  - 'spec/lutaml/model/xml_adapter/xml_namespace_spec.rb'
128
128
  - 'spec/lutaml/model/xml_adapter_spec.rb'
129
129
 
130
- # Offense count: 149
130
+ # Offense count: 153
131
131
  RSpec/MultipleExpectations:
132
132
  Max: 14
133
133
 
134
- # Offense count: 18
134
+ # Offense count: 24
135
135
  # Configuration parameters: AllowSubject.
136
136
  RSpec/MultipleMemoizedHelpers:
137
137
  Max: 9
138
138
 
139
- # Offense count: 9
139
+ # Offense count: 11
140
140
  # Configuration parameters: AllowedGroups.
141
141
  RSpec/NestedGroups:
142
142
  Max: 4
@@ -154,10 +154,9 @@ RSpec/PendingWithoutReason:
154
154
  - 'spec/lutaml/model/xml_adapter/xml_namespace_spec.rb'
155
155
  - 'spec/lutaml/model/xml_adapter_spec.rb'
156
156
 
157
- # Offense count: 2
157
+ # Offense count: 1
158
158
  RSpec/RemoveConst:
159
159
  Exclude:
160
- - 'spec/lutaml/model/type/decimal_spec.rb'
161
160
  - 'spec/lutaml/model/type_spec.rb'
162
161
 
163
162
  # Offense count: 2
data/README.adoc CHANGED
@@ -874,6 +874,7 @@ class Example < Lutaml::Model::Serializable
874
874
  end
875
875
  ----
876
876
 
877
+
877
878
  === XML
878
879
 
879
880
  ==== Setting root element name
@@ -937,8 +938,7 @@ This includes:
937
938
  * attributes
938
939
  * text nodes
939
940
 
940
- The `map_all` tag is **exclusive** and cannot be combined with other mappings
941
- (`map_element`, `map_attribute`, `map_content`) for the same element, ensuring
941
+ The `map_all` tag is **exclusive** and cannot be combined with other mappings (`map_element`, `map_content`) except for `map_attribute` for the same element, ensuring
942
942
  it captures the entire inner XML content.
943
943
 
944
944
  NOTE: An error is raised if `map_all` is defined alongside any other mapping in
@@ -1789,7 +1789,7 @@ https://www.w3.org/TR/xmlschema-1/#xsi_schemaLocation[W3C XML standard].
1789
1789
  Key-value data models like JSON, YAML, and TOML all share a similar structure
1790
1790
  where data is stored as key-value pairs.
1791
1791
 
1792
- Lutaml::Model works with these formats in a similar way.
1792
+ `Lutaml::Model` works with these formats in a similar way.
1793
1793
 
1794
1794
  ==== Mapping
1795
1795
 
@@ -1799,12 +1799,85 @@ Syntax:
1799
1799
 
1800
1800
  [source,ruby]
1801
1801
  ----
1802
- json | yaml | toml do
1802
+ json | yaml | toml | key_value do
1803
1803
  map 'key_value_model_attribute_name', to: :name_of_attribute
1804
1804
  end
1805
1805
  ----
1806
1806
 
1807
- .Using the `map` method to define key-value mappings
1807
+
1808
+ ==== Unified mapping
1809
+
1810
+ The `key_value` method is a streamlined way to map all attributes for
1811
+ serialization into key-value formats including JSON, YAML, and TOML.
1812
+
1813
+ If there is no definite differentiation between the key value formats, the
1814
+ `key_value` method simplifies defining mappings and improves code readability.
1815
+
1816
+
1817
+ .Using the `map` method to define the same mappings across all key-value formats
1818
+ [example]
1819
+ ====
1820
+ This example shows how to define a key-value data model with the `key_value`
1821
+ method which maps the same attributes across all key-value formats.
1822
+
1823
+ [source,ruby]
1824
+ ----
1825
+ class CeramicModel < Lutaml::Model::Serializable
1826
+ attribute :color, :string
1827
+ attribute :glaze, :string
1828
+ attribute :description, :string
1829
+
1830
+ key_value do
1831
+ map :color, to: color
1832
+ map :glz, to: :glaze
1833
+ map :desc, to: :description
1834
+ end
1835
+
1836
+ # Equivalent to the JSON, YAML, and TOML mappings.
1837
+ #
1838
+ # json and yaml and toml do
1839
+ # map :id, to: color
1840
+ # map :name, to: :full_name
1841
+ # map :status, to: :current_status
1842
+ # end
1843
+ end
1844
+ ----
1845
+
1846
+ [source,json]
1847
+ ----
1848
+ {
1849
+ "color": "Navy Blue",
1850
+ "glz": "Clear",
1851
+ "desc": "A ceramic with a navy blue color and clear glaze."
1852
+ }
1853
+ ----
1854
+
1855
+ [source,yaml]
1856
+ ----
1857
+ color: Navy Blue
1858
+ glz: Clear
1859
+ desc: A ceramic with a navy blue color and clear glaze.
1860
+ ----
1861
+
1862
+ [source,ruby]
1863
+ ----
1864
+ > CeramicModel.from_json(json)
1865
+ > #<CeramicModel:0x0000000104ac7240 @color="Navy Blue", @glaze="Clear", @description="A ceramic with a navy blue color and clear glaze.">
1866
+ > CeramicModel.new(color: "Navy Blue", glaze: "Clear", description: "A ceramic with a navy blue color and clear glaze.").to_json
1867
+ > #{"color"=>"Navy Blue", "glz"=>"Clear", "desc"=>"A ceramic with a navy blue color and clear glaze."}
1868
+ ----
1869
+ ====
1870
+
1871
+ ==== Specific format mappings
1872
+
1873
+ Specific key value formats can be mapping independently of other formats, including:
1874
+
1875
+ * `json` for the JSON format
1876
+ * `yaml` for the YAML format
1877
+ * `toml` for the TOML format
1878
+
1879
+
1880
+ .Using the `map` method to define key-value mappings per format
1808
1881
  [example]
1809
1882
  ====
1810
1883
  [source,ruby]
@@ -9,6 +9,7 @@ module Lutaml
9
9
  attr_accessor :xml_adapter, :toml_adapter
10
10
 
11
11
  AVAILABLE_FORMATS = %i[xml json yaml toml].freeze
12
+ KEY_VALUE_FORMATS = AVAILABLE_FORMATS - %i[xml]
12
13
 
13
14
  def configure
14
15
  yield self
@@ -151,6 +151,13 @@ module Lutaml
151
151
  end
152
152
  end
153
153
 
154
+ def key_value(&block)
155
+ Lutaml::Model::Config::KEY_VALUE_FORMATS.each do |format|
156
+ mappings[format] ||= KeyValueMapping.new
157
+ mappings[format].instance_eval(&block)
158
+ end
159
+ end
160
+
154
161
  def hash_representation(instance, format, options = {})
155
162
  only = options[:only]
156
163
  except = options[:except]
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.3.27"
5
+ VERSION = "0.3.29"
6
6
  end
7
7
  end
@@ -3,6 +3,13 @@ require_relative "xml_mapping_rule"
3
3
  module Lutaml
4
4
  module Model
5
5
  class XmlMapping
6
+ TYPES = {
7
+ attribute: :map_attribute,
8
+ element: :map_element,
9
+ content: :map_content,
10
+ all_content: :map_all,
11
+ }.freeze
12
+
6
13
  attr_reader :root_element,
7
14
  :namespace_uri,
8
15
  :namespace_prefix,
@@ -53,7 +60,7 @@ module Lutaml
53
60
  prefix: (prefix_set = false
54
61
  nil)
55
62
  )
56
- validate!(name, to, with)
63
+ validate!(name, to, with, type: TYPES[:element])
57
64
 
58
65
  rule = XmlMappingRule.new(
59
66
  name,
@@ -84,7 +91,7 @@ module Lutaml
84
91
  prefix: (prefix_set = false
85
92
  nil)
86
93
  )
87
- validate!(name, to, with)
94
+ validate!(name, to, with, type: TYPES[:attribute])
88
95
 
89
96
  rule = XmlMappingRule.new(
90
97
  name,
@@ -114,7 +121,7 @@ module Lutaml
114
121
  mixed: false,
115
122
  cdata: false
116
123
  )
117
- validate!("content", to, with)
124
+ validate!("content", to, with, type: TYPES[:content])
118
125
 
119
126
  @content_mapping = XmlMappingRule.new(
120
127
  nil,
@@ -139,7 +146,7 @@ module Lutaml
139
146
  prefix: (prefix_set = false
140
147
  nil)
141
148
  )
142
- validate!("__raw_mapping", to, with)
149
+ validate!("__raw_mapping", to, with, type: TYPES[:all_content])
143
150
 
144
151
  rule = XmlMappingRule.new(
145
152
  "__raw_mapping",
@@ -158,8 +165,8 @@ module Lutaml
158
165
  @raw_mapping = rule
159
166
  end
160
167
 
161
- def validate!(key, to, with)
162
- validate_mappings!(key)
168
+ def validate!(key, to, with, type: nil)
169
+ validate_mappings!(type)
163
170
 
164
171
  if to.nil? && with.empty?
165
172
  msg = ":to or :with argument is required for mapping '#{key}'"
@@ -172,13 +179,14 @@ module Lutaml
172
179
  end
173
180
  end
174
181
 
175
- def validate_mappings!(key)
176
- unless @raw_mapping.nil?
177
- raise StandardError, "no other mappings are allowed with map_all"
182
+ def validate_mappings!(type)
183
+ if !@raw_mapping.nil? && type != TYPES[:attribute]
184
+ raise StandardError, "#{type} is not allowed, only #{TYPES[:attribute]} " \
185
+ "is allowed with #{TYPES[:all_content]}"
178
186
  end
179
187
 
180
- if !mappings.empty? && key == "__raw_mapping"
181
- raise StandardError, "map_all is not allowed with other mappings"
188
+ if !(elements.empty? && content_mapping.nil?) && type == TYPES[:all_content]
189
+ raise StandardError, "#{TYPES[:all_content]} is not allowed with other mappings"
182
190
  end
183
191
  end
184
192
 
@@ -25,6 +25,18 @@ module SerializeableSpec
25
25
  end
26
26
  end
27
27
 
28
+ class KeyValueMapper < Lutaml::Model::Serializable
29
+ attribute :first_name, :string
30
+ attribute :last_name, :string
31
+ attribute :age, :integer
32
+
33
+ key_value do
34
+ map :first_name, to: :first_name
35
+ map :last_name, to: :last_name
36
+ map :age, to: :age
37
+ end
38
+ end
39
+
28
40
  ### XML root mapping
29
41
 
30
42
  class RecordDate < Lutaml::Model::Serializable
@@ -207,6 +219,22 @@ RSpec.describe Lutaml::Model::Serializable do
207
219
  end
208
220
  end
209
221
 
222
+ describe "#key_value" do
223
+ let(:model) { SerializeableSpec::KeyValueMapper }
224
+
225
+ Lutaml::Model::Config::KEY_VALUE_FORMATS.each do |format|
226
+ it "defines 3 mappings for #{format}" do
227
+ expect(model.mappings_for(format).mappings.count).to eq(3)
228
+ end
229
+
230
+ it "defines mappings correctly for #{format}" do
231
+ defined_mappings = model.mappings_for(format).mappings.map(&:name)
232
+
233
+ expect(defined_mappings).to eq(%i[first_name last_name age])
234
+ end
235
+ end
236
+ end
237
+
210
238
  describe "XML root name override" do
211
239
  it "uses root name defined at the component class" do
212
240
  record_date = SerializeableSpec::RecordDate.new(content: "2021-01-01")
@@ -179,6 +179,7 @@ module XmlMapping
179
179
 
180
180
  class WithMapAll < Lutaml::Model::Serializable
181
181
  attribute :all_content, :string
182
+ attribute :attr, :string
182
183
 
183
184
  xml do
184
185
  root "WithMapAll"
@@ -777,30 +778,34 @@ RSpec.describe Lutaml::Model::XmlMapping do
777
778
 
778
779
  describe "#map_all" do
779
780
  context "when map_all is defined before any other mapping" do
780
- let(:error_message) { "no other mappings are allowed with map_all" }
781
-
782
781
  it "raise error when for map_element with map_all" do
783
782
  expect do
784
783
  XmlMapping::WithMapAll.xml do
785
784
  map_element "ele", to: :ele
786
785
  end
787
- end.to raise_error(StandardError, error_message)
786
+ end.to raise_error(
787
+ StandardError,
788
+ "map_element is not allowed, only map_attribute is allowed with map_all",
789
+ )
788
790
  end
789
791
 
790
- it "raise error when for map_attribute with map_all" do
792
+ it "raise error when for map_content with map_all" do
791
793
  expect do
792
794
  XmlMapping::WithMapAll.xml do
793
- map_attribute "attr", to: :attr
795
+ map_content to: :text
794
796
  end
795
- end.to raise_error(StandardError, error_message)
797
+ end.to raise_error(
798
+ StandardError,
799
+ "map_content is not allowed, only map_attribute is allowed with map_all",
800
+ )
796
801
  end
797
802
 
798
- it "raise error when for map_content with map_all" do
803
+ it "does not raise error for map_attribute with map_all" do
799
804
  expect do
800
805
  XmlMapping::WithMapAll.xml do
801
- map_content to: :text
806
+ map_attribute "attr", to: :attr
802
807
  end
803
- end.to raise_error(StandardError, error_message)
808
+ end.not_to raise_error
804
809
  end
805
810
  end
806
811
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lutaml-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.27
4
+ version: 0.3.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-19 00:00:00.000000000 Z
11
+ date: 2024-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor