lutaml-model 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,10 @@ module Lutaml
8
8
  @name = name
9
9
  @type = type
10
10
  @options = options
11
+
12
+ if collection? && !options[:default]
13
+ @options[:default] = -> { [] }
14
+ end
11
15
  end
12
16
 
13
17
  def collection?
@@ -0,0 +1,18 @@
1
+ module Lutaml
2
+ module Model
3
+ class InvalidValueError < Error
4
+ def initialize(attr_name, value, allowed_values)
5
+ @attr_name = attr_name
6
+ @value = value
7
+ @allowed_values = allowed_values
8
+
9
+ super()
10
+ end
11
+
12
+ def to_s
13
+ "#{@attr_name} is `#{@value}`, must be one of the " \
14
+ "following [#{@allowed_values.join(', ')}]"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ module Lutaml
2
+ module Model
3
+ class Error < StandardError
4
+ end
5
+ end
6
+ end
7
+
8
+ require_relative "error/invalid_value_error"
@@ -10,13 +10,21 @@ module Lutaml
10
10
  @mappings = []
11
11
  end
12
12
 
13
- def map(name, to:, render_nil: false, with: {}, delegate: nil)
13
+ def map(
14
+ name,
15
+ to:,
16
+ render_nil: false,
17
+ with: {},
18
+ delegate: nil,
19
+ child_mappings: nil
20
+ )
14
21
  @mappings << KeyValueMappingRule.new(
15
22
  name,
16
23
  to: to,
17
24
  render_nil: render_nil,
18
25
  with: with,
19
26
  delegate: delegate,
27
+ child_mappings: child_mappings,
20
28
  )
21
29
  end
22
30
 
@@ -0,0 +1,42 @@
1
+ # lib/lutaml/model/type.rb
2
+
3
+ module Lutaml
4
+ module Model
5
+ class MappingHash < Hash
6
+ attr_accessor :ordered
7
+
8
+ def initialize
9
+ @ordered = false
10
+ @item_order = []
11
+
12
+ super
13
+ end
14
+
15
+ def item_order
16
+ @item_order&.map { |key| normalize(key) } || keys
17
+ end
18
+
19
+ def item_order=(order)
20
+ raise "`item order` must be an array" unless order.is_a?(Array)
21
+
22
+ @item_order = order
23
+ end
24
+
25
+ def ordered?
26
+ @ordered
27
+ end
28
+
29
+ private
30
+
31
+ def normalize(key)
32
+ if self[key.to_s]
33
+ key.to_s
34
+ elsif self[key.to_sym]
35
+ key.to_sym
36
+ else
37
+ key
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -2,14 +2,32 @@
2
2
  module Lutaml
3
3
  module Model
4
4
  class MappingRule
5
- attr_reader :name, :to, :render_nil, :custom_methods, :delegate
5
+ attr_reader :name,
6
+ :to,
7
+ :render_nil,
8
+ :custom_methods,
9
+ :delegate,
10
+ :mixed_content,
11
+ :child_mappings
6
12
 
7
- def initialize(name, to:, render_nil: false, with: {}, delegate: nil)
13
+ def initialize(
14
+ name,
15
+ to:,
16
+ render_nil: false,
17
+ with: {},
18
+ delegate: nil,
19
+ mixed_content: false,
20
+ namespace_set: false,
21
+ child_mappings: nil
22
+ )
8
23
  @name = name
9
24
  @to = to
10
25
  @render_nil = render_nil
11
26
  @custom_methods = with
12
27
  @delegate = delegate
28
+ @mixed_content = mixed_content
29
+ @namespace_set = namespace_set
30
+ @child_mappings = child_mappings
13
31
  end
14
32
 
15
33
  alias from name
@@ -38,6 +56,10 @@ module Lutaml
38
56
  doc[name.to_s]
39
57
  end
40
58
  end
59
+
60
+ def namespace_set?
61
+ @namespace_set
62
+ end
41
63
  end
42
64
  end
43
65
  end