lutaml-model 0.3.24 → 0.3.26

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +36 -17
  3. data/README.adoc +396 -30
  4. data/lib/lutaml/model/attribute.rb +52 -27
  5. data/lib/lutaml/model/error/pattern_not_matched_error.rb +17 -0
  6. data/lib/lutaml/model/error/type_error.rb +9 -0
  7. data/lib/lutaml/model/error/unknown_type_error.rb +9 -0
  8. data/lib/lutaml/model/error/validation_error.rb +0 -1
  9. data/lib/lutaml/model/error.rb +3 -0
  10. data/lib/lutaml/model/mapping_hash.rb +8 -0
  11. data/lib/lutaml/model/serialize.rb +10 -5
  12. data/lib/lutaml/model/type/boolean.rb +38 -0
  13. data/lib/lutaml/model/type/date.rb +35 -0
  14. data/lib/lutaml/model/type/date_time.rb +32 -4
  15. data/lib/lutaml/model/type/decimal.rb +42 -0
  16. data/lib/lutaml/model/type/float.rb +37 -0
  17. data/lib/lutaml/model/type/hash.rb +62 -0
  18. data/lib/lutaml/model/type/integer.rb +41 -0
  19. data/lib/lutaml/model/type/string.rb +49 -0
  20. data/lib/lutaml/model/type/time.rb +49 -0
  21. data/lib/lutaml/model/type/time_without_date.rb +37 -5
  22. data/lib/lutaml/model/type/value.rb +52 -0
  23. data/lib/lutaml/model/type.rb +50 -114
  24. data/lib/lutaml/model/validation.rb +2 -1
  25. data/lib/lutaml/model/version.rb +1 -1
  26. data/lib/lutaml/model/xml_adapter/builder/nokogiri.rb +12 -3
  27. data/lib/lutaml/model/xml_adapter/builder/ox.rb +7 -1
  28. data/lib/lutaml/model/xml_adapter/nokogiri_adapter.rb +3 -1
  29. data/lib/lutaml/model/xml_adapter/ox_adapter.rb +12 -8
  30. data/lib/lutaml/model/xml_adapter/xml_document.rb +6 -8
  31. data/lib/lutaml/model/xml_mapping.rb +6 -2
  32. data/lib/lutaml/model/xml_mapping_rule.rb +7 -1
  33. data/lutaml-model.gemspec +1 -1
  34. data/spec/address_spec.rb +170 -0
  35. data/spec/fixtures/address.rb +33 -0
  36. data/spec/fixtures/person.rb +73 -0
  37. data/spec/fixtures/sample_model.rb +40 -0
  38. data/spec/fixtures/vase.rb +38 -0
  39. data/spec/fixtures/xml/special_char.xml +13 -0
  40. data/spec/lutaml/model/attribute_spec.rb +139 -0
  41. data/spec/lutaml/model/cdata_spec.rb +520 -0
  42. data/spec/lutaml/model/collection_spec.rb +299 -0
  43. data/spec/lutaml/model/comparable_model_spec.rb +106 -0
  44. data/spec/lutaml/model/custom_model_spec.rb +410 -0
  45. data/spec/lutaml/model/custom_serialization_spec.rb +170 -0
  46. data/spec/lutaml/model/defaults_spec.rb +221 -0
  47. data/spec/lutaml/model/delegation_spec.rb +340 -0
  48. data/spec/lutaml/model/inheritance_spec.rb +92 -0
  49. data/spec/lutaml/model/json_adapter_spec.rb +37 -0
  50. data/spec/lutaml/model/key_value_mapping_spec.rb +86 -0
  51. data/spec/lutaml/model/map_content_spec.rb +118 -0
  52. data/spec/lutaml/model/mixed_content_spec.rb +625 -0
  53. data/spec/lutaml/model/namespace_spec.rb +57 -0
  54. data/spec/lutaml/model/ordered_content_spec.rb +83 -0
  55. data/spec/lutaml/model/render_nil_spec.rb +138 -0
  56. data/spec/lutaml/model/schema/json_schema_spec.rb +79 -0
  57. data/spec/lutaml/model/schema/relaxng_schema_spec.rb +60 -0
  58. data/spec/lutaml/model/schema/xsd_schema_spec.rb +55 -0
  59. data/spec/lutaml/model/schema/yaml_schema_spec.rb +47 -0
  60. data/spec/lutaml/model/serializable_spec.rb +297 -0
  61. data/spec/lutaml/model/serializable_validation_spec.rb +90 -0
  62. data/spec/lutaml/model/simple_model_spec.rb +314 -0
  63. data/spec/lutaml/model/toml_adapter_spec.rb +39 -0
  64. data/spec/lutaml/model/type/boolean_spec.rb +54 -0
  65. data/spec/lutaml/model/type/date_spec.rb +118 -0
  66. data/spec/lutaml/model/type/date_time_spec.rb +127 -0
  67. data/spec/lutaml/model/type/decimal_spec.rb +125 -0
  68. data/spec/lutaml/model/type/float_spec.rb +191 -0
  69. data/spec/lutaml/model/type/hash_spec.rb +63 -0
  70. data/spec/lutaml/model/type/integer_spec.rb +145 -0
  71. data/spec/lutaml/model/type/string_spec.rb +150 -0
  72. data/spec/lutaml/model/type/time_spec.rb +142 -0
  73. data/spec/lutaml/model/type/time_without_date_spec.rb +125 -0
  74. data/spec/lutaml/model/type_spec.rb +276 -0
  75. data/spec/lutaml/model/utils_spec.rb +79 -0
  76. data/spec/lutaml/model/validation_spec.rb +83 -0
  77. data/spec/lutaml/model/with_child_mapping_spec.rb +174 -0
  78. data/spec/lutaml/model/xml_adapter/nokogiri_adapter_spec.rb +56 -0
  79. data/spec/lutaml/model/xml_adapter/oga_adapter_spec.rb +56 -0
  80. data/spec/lutaml/model/xml_adapter/ox_adapter_spec.rb +61 -0
  81. data/spec/lutaml/model/xml_adapter/xml_namespace_spec.rb +251 -0
  82. data/spec/lutaml/model/xml_adapter_spec.rb +178 -0
  83. data/spec/lutaml/model/xml_mapping_spec.rb +863 -0
  84. data/spec/lutaml/model/yaml_adapter_spec.rb +30 -0
  85. data/spec/lutaml/model_spec.rb +1 -0
  86. data/spec/person_spec.rb +161 -0
  87. data/spec/spec_helper.rb +33 -0
  88. metadata +68 -2
@@ -0,0 +1,86 @@
1
+ require "spec_helper"
2
+ require_relative "../../../lib/lutaml/model/key_value_mapping"
3
+ require_relative "../../../lib/lutaml/model/key_value_mapping_rule"
4
+
5
+ RSpec.describe Lutaml::Model::KeyValueMapping do
6
+ let(:mapping) { described_class.new }
7
+
8
+ context "with delegate option" do
9
+ before do
10
+ mapping.map("type", to: :type, delegate: :some_delegate)
11
+ mapping.map("name", to: :name)
12
+ end
13
+
14
+ it "adds mappings with delegate option" do
15
+ expect(mapping.mappings.size).to eq(2)
16
+ expect(mapping.mappings[0].name).to eq("type")
17
+ expect(mapping.mappings[0].delegate).to eq(:some_delegate)
18
+ expect(mapping.mappings[1].name).to eq("name")
19
+ expect(mapping.mappings[1].delegate).to be_nil
20
+ end
21
+ end
22
+
23
+ describe "#deep_dup" do
24
+ before do
25
+ mapping.map(
26
+ "type",
27
+ to: :type,
28
+ render_nil: true,
29
+ delegate: :some_delegate,
30
+ child_mappings: {
31
+ id: :key,
32
+ path: %i[path link],
33
+ name: %i[path name],
34
+ },
35
+ )
36
+
37
+ mapping.map(
38
+ "name",
39
+ with: {
40
+ from: :model_from_json,
41
+ to: :json_from_model,
42
+ },
43
+ )
44
+ end
45
+
46
+ let(:dup_mapping) { mapping.deep_dup }
47
+
48
+ it "creates a deep duplicate" do
49
+ expect(dup_mapping.object_id).not_to eq(mapping.object_id)
50
+ end
51
+
52
+ it "correctly duplicates mapping with `to:`" do
53
+ m = mapping.mappings[0]
54
+ dup_m = dup_mapping.mappings[0]
55
+
56
+ expect(m.name).to eq(dup_m.name)
57
+ expect(m.name.object_id).not_to eq(dup_m.name.object_id)
58
+
59
+ # using symbols so object_id will remain same
60
+ expect(m.to).to eq(dup_m.to)
61
+
62
+ # render_nil is boolean so is constant with same object_id
63
+ expect(m.render_nil).to eq(dup_m.render_nil)
64
+
65
+ # using symbols so object_id will remain same
66
+ expect(m.delegate).to eq(dup_m.delegate)
67
+
68
+ expect(m.child_mappings).to eq(dup_m.child_mappings)
69
+ expect(m.child_mappings.object_id).not_to eq(dup_m.child_mappings.object_id)
70
+ end
71
+
72
+ it "correctly duplicates mapping with custom methods" do
73
+ m = mapping.mappings[0]
74
+ dup_m = dup_mapping.mappings[0]
75
+
76
+ expect(m.name).to eq(dup_m.name)
77
+ expect(m.name.object_id).not_to eq(dup_m.name.object_id)
78
+
79
+ # render_nil is boolean so is constant with same object_id
80
+ expect(m.render_nil).to eq(dup_m.render_nil)
81
+
82
+ expect(m.custom_methods).to eq(dup_m.custom_methods)
83
+ expect(m.custom_methods.object_id).not_to eq(dup_m.custom_methods.object_id)
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,118 @@
1
+ require "spec_helper"
2
+ require "lutaml/model"
3
+
4
+ # module MapContent
5
+ # class Italic < Lutaml::Model::Serializable
6
+ # attribute :text, Lutaml::Model::Type::String
7
+
8
+ # xml do
9
+ # root "italic"
10
+ # map_content to: :text
11
+ # end
12
+ # end
13
+
14
+ # class Bold < Lutaml::Model::Serializable
15
+ # attribute :text, Lutaml::Model::Type::String
16
+ # attribute :italic, Italic
17
+
18
+ # xml do
19
+ # root "b"
20
+ # mixed do
21
+ # map_content to: :text
22
+ # map "italic", to: :italic
23
+ # end
24
+ # end
25
+ # end
26
+
27
+ # class RichText < Lutaml::Model::Serializable
28
+ # attribute :text, Lutaml::Model::Type::String
29
+ # attribute :bold, Bold
30
+ # attribute :italic, Italic
31
+
32
+ # xml do
33
+ # mixed do
34
+ # map_content to: :text
35
+ # map_element "bold", to: :bold
36
+ # map_element "italic", to: :italic
37
+ # end
38
+ # end
39
+ # end
40
+
41
+ # class Car < Lutaml::Model::Serializable
42
+ # attribute :description, RichText
43
+ # attribute :make, Lutaml::Model::Type::String
44
+
45
+ # xml do
46
+ # root "car"
47
+ # map_element "description", to: :description
48
+ # map_element "make", to: :make
49
+ # end
50
+ # end
51
+ # end
52
+
53
+ # RSpec.xdescribe MapContent do
54
+ # let(:italic_text) { MapContent::Italic.new(text: "italic text") }
55
+ # let(:bold_text) do
56
+ # MapContent::Bold.new(
57
+ # text: "bold text",
58
+ # italic: italic_text,
59
+ # )
60
+ # end
61
+ # let(:rich_text) do
62
+ # MapContent::RichText.new(
63
+ # text: "regular text",
64
+ # bold: bold_text,
65
+ # italic: italic_text,
66
+ # )
67
+ # end
68
+ # let(:attributes) { { description: rich_text, make: "Toyota" } }
69
+ # let(:model) { MapContent::Car.new(attributes) }
70
+
71
+ # let(:model_xml) {
72
+ # <<~XML
73
+ # <car>
74
+ # <description>
75
+ # regular text
76
+ # <bold>
77
+ # bold text
78
+ # <italic>italic text</italic>
79
+ # </bold>
80
+ # <italic>italic text</italic>
81
+ # </description>
82
+ # <make>Toyota</make>
83
+ # </car>
84
+ # XML
85
+ # }
86
+
87
+ # it "initializes with default values" do
88
+ # default_model = Car.new
89
+ # expect(default_model.description).to eq(nil)
90
+ # expect(default_model.make).to eq(nil)
91
+ # end
92
+
93
+ # it "serializes to XML" do
94
+ # expected_xml = model_xml.strip
95
+ # expect(model.to_xml.strip).to eq(expected_xml)
96
+ # end
97
+
98
+ # it "deserializes from XML" do
99
+ # sample = Car.from_xml(model_xml)
100
+ # expect(sample.description.text).to eq("regular text")
101
+ # expect(sample.description.bold.text).to eq("bold text")
102
+ # expect(sample.description.bold.italic.text).to eq("italic text")
103
+ # expect(sample.description.italic.text).to eq("italic text")
104
+ # expect(sample.make).to eq("Toyota")
105
+ # end
106
+
107
+ # it "round-trips XML" do
108
+ # xml = model.to_xml
109
+ # new_model = Car.from_xml(xml)
110
+ # expect(new_model.description.text).to eq(model.description.text)
111
+ # expect(new_model.description.bold.text).to eq(model.description.bold.text)
112
+ # expect(new_model.description.bold.italic.text)
113
+ # .to eq(model.description.bold.italic.text)
114
+ # expect(new_model.description.italic.text)
115
+ # .to eq(model.description.italic.text)
116
+ # expect(new_model.make).to eq(model.make)
117
+ # end
118
+ # end