lutaml-model 0.3.27 → 0.3.28

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: 7fbf76c4bb8fbe07fdaed6235a5e8edcd98dc17ace1d322d0e9a36c7c4a59aef
4
+ data.tar.gz: f63bf29850309ab1250e2713e62fd6ef624ad5c13a824dc6e7822dec77f75369
5
5
  SHA512:
6
- metadata.gz: ab260d59203839c5fd9b1aa670501c1953c741fe75f0f7ab7261795c54cd19ed6de9807f9b8252c83ce9eee8cf2e6a9116087066e464ca1f35b8b46e29082351
7
- data.tar.gz: c579a87612ce8e249edcca0c79f0ee2550d40af899e31ee2405ffdd48509e584df449c19bb3489a6871cdf4dfc08c4bc51f482422ea9a7d91f02d706d653cac6
6
+ metadata.gz: 58b261437656342dc5894e25dcde477f680e72b6eebd0ae6771a5d97f0212442ae5dab446d0e182f5a4a712516b8781a0849bdcb1fa2ee1fbc831ecf9dc33695
7
+ data.tar.gz: 0af5c64461b6e9f5ab6d67b18ebe81e19db49123b4241b201c786b1632b567290ab55288f7047e01e3c5938316d7e93b8aca511f6f4a3f3759f1d4575fe758ae
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
@@ -1789,7 +1790,7 @@ https://www.w3.org/TR/xmlschema-1/#xsi_schemaLocation[W3C XML standard].
1789
1790
  Key-value data models like JSON, YAML, and TOML all share a similar structure
1790
1791
  where data is stored as key-value pairs.
1791
1792
 
1792
- Lutaml::Model works with these formats in a similar way.
1793
+ `Lutaml::Model` works with these formats in a similar way.
1793
1794
 
1794
1795
  ==== Mapping
1795
1796
 
@@ -1799,12 +1800,85 @@ Syntax:
1799
1800
 
1800
1801
  [source,ruby]
1801
1802
  ----
1802
- json | yaml | toml do
1803
+ json | yaml | toml | key_value do
1803
1804
  map 'key_value_model_attribute_name', to: :name_of_attribute
1804
1805
  end
1805
1806
  ----
1806
1807
 
1807
- .Using the `map` method to define key-value mappings
1808
+
1809
+ ==== Unified mapping
1810
+
1811
+ The `key_value` method is a streamlined way to map all attributes for
1812
+ serialization into key-value formats including JSON, YAML, and TOML.
1813
+
1814
+ If there is no definite differentiation between the key value formats, the
1815
+ `key_value` method simplifies defining mappings and improves code readability.
1816
+
1817
+
1818
+ .Using the `map` method to define the same mappings across all key-value formats
1819
+ [example]
1820
+ ====
1821
+ This example shows how to define a key-value data model with the `key_value`
1822
+ method which maps the same attributes across all key-value formats.
1823
+
1824
+ [source,ruby]
1825
+ ----
1826
+ class CeramicModel < Lutaml::Model::Serializable
1827
+ attribute :color, :string
1828
+ attribute :glaze, :string
1829
+ attribute :description, :string
1830
+
1831
+ key_value do
1832
+ map :color, to: color
1833
+ map :glz, to: :glaze
1834
+ map :desc, to: :description
1835
+ end
1836
+
1837
+ # Equivalent to the JSON, YAML, and TOML mappings.
1838
+ #
1839
+ # json and yaml and toml do
1840
+ # map :id, to: color
1841
+ # map :name, to: :full_name
1842
+ # map :status, to: :current_status
1843
+ # end
1844
+ end
1845
+ ----
1846
+
1847
+ [source,json]
1848
+ ----
1849
+ {
1850
+ "color": "Navy Blue",
1851
+ "glz": "Clear",
1852
+ "desc": "A ceramic with a navy blue color and clear glaze."
1853
+ }
1854
+ ----
1855
+
1856
+ [source,yaml]
1857
+ ----
1858
+ color: Navy Blue
1859
+ glz: Clear
1860
+ desc: A ceramic with a navy blue color and clear glaze.
1861
+ ----
1862
+
1863
+ [source,ruby]
1864
+ ----
1865
+ > CeramicModel.from_json(json)
1866
+ > #<CeramicModel:0x0000000104ac7240 @color="Navy Blue", @glaze="Clear", @description="A ceramic with a navy blue color and clear glaze.">
1867
+ > CeramicModel.new(color: "Navy Blue", glaze: "Clear", description: "A ceramic with a navy blue color and clear glaze.").to_json
1868
+ > #{"color"=>"Navy Blue", "glz"=>"Clear", "desc"=>"A ceramic with a navy blue color and clear glaze."}
1869
+ ----
1870
+ ====
1871
+
1872
+ ==== Specific format mappings
1873
+
1874
+ Specific key value formats can be mapping independently of other formats, including:
1875
+
1876
+ * `json` for the JSON format
1877
+ * `yaml` for the YAML format
1878
+ * `toml` for the TOML format
1879
+
1880
+
1881
+ .Using the `map` method to define key-value mappings per format
1808
1882
  [example]
1809
1883
  ====
1810
1884
  [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.28"
6
6
  end
7
7
  end
@@ -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")
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.28
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-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor