lutaml-model 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -44,12 +44,8 @@ module Lutaml
44
44
  @root.order
45
45
  end
46
46
 
47
- def handle_nested_elements(builder, value, rule = nil)
48
- options = {}
49
-
50
- if rule&.namespace_set?
51
- options[:namespace_prefix] = rule.prefix
52
- end
47
+ def handle_nested_elements(builder, value, rule: nil, attribute: nil)
48
+ options = build_options_for_nested_elements(attribute, rule)
53
49
 
54
50
  case value
55
51
  when Array
@@ -59,6 +55,19 @@ module Lutaml
59
55
  end
60
56
  end
61
57
 
58
+ def build_options_for_nested_elements(attribute, rule)
59
+ return {} unless rule
60
+
61
+ options = {}
62
+
63
+ options[:namespace_prefix] = rule.prefix if rule&.namespace_set?
64
+ options[:mixed_content] = rule.mixed_content
65
+
66
+ options[:mapper_class] = attribute&.type if attribute
67
+
68
+ options
69
+ end
70
+
62
71
  def parse_element(element)
63
72
  result = Lutaml::Model::MappingHash.new
64
73
  result.item_order = element.order
@@ -81,14 +90,23 @@ module Lutaml
81
90
  result
82
91
  end
83
92
 
84
- def build_element(xml, element, _options = {})
85
- if element.ordered?
86
- build_ordered_element(xml, element, _options)
93
+ def build_element(xml, element, options = {})
94
+ if ordered?(element, options)
95
+ build_ordered_element(xml, element, options)
87
96
  else
88
- build_unordered_element(xml, element, _options)
97
+ build_unordered_element(xml, element, options)
89
98
  end
90
99
  end
91
100
 
101
+ def ordered?(element, options = {})
102
+ return false unless element.respond_to?(:element_order)
103
+ return element.ordered? if element.respond_to?(:ordered?)
104
+ return options[:mixed_content] if options.key?(:mixed_content)
105
+
106
+ mapper_class = options[:mapper_class]
107
+ mapper_class ? mapper_class.mappings_for(:xml).mixed_content? : false
108
+ end
109
+
92
110
  def build_namespace_attributes(klass, processed = {})
93
111
  xml_mappings = klass.mappings_for(:xml)
94
112
  attributes = klass.attributes
@@ -143,8 +161,9 @@ module Lutaml
143
161
  end
144
162
  end
145
163
 
146
- def attribute_definition_for(element, rule)
147
- return element.class.attributes[rule.to] unless rule.delegate
164
+ def attribute_definition_for(element, rule, mapper_class: nil)
165
+ klass = mapper_class || element.class
166
+ return klass.attributes[rule.to] unless rule.delegate
148
167
 
149
168
  element.send(rule.delegate).class.attributes[rule.to]
150
169
  end
@@ -4,9 +4,17 @@ require "yaml"
4
4
  module Lutaml
5
5
  module Model
6
6
  module YamlAdapter
7
- module Standard
8
- def self.to_yaml(model, *args)
9
- YAML.dump(model.hash_representation(:yaml), *args)
7
+ class Standard
8
+ def initialize(attributes)
9
+ @attributes = attributes
10
+ end
11
+
12
+ def to_yaml(options = {})
13
+ YAML.dump(@attributes, options)
14
+ end
15
+
16
+ def self.to_yaml(attributes, *args)
17
+ new(attributes).to_yaml(*args)
10
18
  end
11
19
 
12
20
  def self.from_yaml(yaml, klass)
data/lib/lutaml/model.rb CHANGED
@@ -12,9 +12,16 @@ require_relative "model/json_adapter"
12
12
  require_relative "model/yaml_adapter"
13
13
  require_relative "model/xml_adapter"
14
14
  require_relative "model/toml_adapter"
15
+ require_relative "model/error"
15
16
 
16
17
  module Lutaml
17
18
  module Model
19
+ # Error for passing incorrect model type
20
+ #
21
+ # @api private
22
+ class IncorrectModelError < StandardError
23
+ end
24
+
18
25
  class BaseModel < Serializable
19
26
  end
20
27
  end
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.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-02 00:00:00.000000000 Z
11
+ date: 2024-08-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'LutaML creating data models in Ruby
14
14
 
@@ -34,6 +34,8 @@ files:
34
34
  - lib/lutaml/model.rb
35
35
  - lib/lutaml/model/attribute.rb
36
36
  - lib/lutaml/model/config.rb
37
+ - lib/lutaml/model/error.rb
38
+ - lib/lutaml/model/error/invalid_value_error.rb
37
39
  - lib/lutaml/model/json_adapter.rb
38
40
  - lib/lutaml/model/json_adapter/multi_json.rb
39
41
  - lib/lutaml/model/json_adapter/standard.rb