Class: Lutaml::Model::Xml::MappingRule

Inherits:
MappingRule show all
Defined in:
lib/lutaml/model/xml/mapping_rule.rb

Constant Summary

Constants inherited from MappingRule

MappingRule::ALLOWED_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from MappingRule

#attribute, #custom_methods, #delegate, #format, #name, #polymorphic, #polymorphic_map, #render_default, #render_empty, #render_nil, #to, #transform, #treat_empty, #treat_nil, #treat_omitted

Instance Method Summary collapse

Methods inherited from MappingRule

#default_value_map, #deserialize, #eql?, #has_custom_method_for_deserialization?, #has_custom_method_for_serialization?, #multiple_mappings?, #polymorphic_mapping?, #raw_mapping?, #render?, #render_as, #render_empty?, #render_nil?, #render_omitted?, #render_value_for, #serialize, #serialize_attribute, #to_value_for, #treat?, #treat_as, #treat_empty?, #treat_nil?, #treat_omitted?, #value_for_option, #value_map

Constructor Details

#initialize(name, to:, render_nil: false, render_default: false, render_empty: false, treat_nil: nil, treat_empty: nil, treat_omitted: nil, with: {}, delegate: nil, namespace: nil, prefix: nil, mixed_content: false, cdata: false, namespace_set: false, prefix_set: false, attribute: false, default_namespace: nil, polymorphic: {}, polymorphic_map: {}, transform: {}, value_map: {}) ⇒ MappingRule

Returns a new instance of MappingRule.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lutaml/model/xml/mapping_rule.rb', line 9

def initialize(
  name,
  to:,
  render_nil: false,
  render_default: false,
  render_empty: false,
  treat_nil: nil,
  treat_empty: nil,
  treat_omitted: nil,
  with: {},
  delegate: nil,
  namespace: nil,
  prefix: nil,
  mixed_content: false,
  cdata: false,
  namespace_set: false,
  prefix_set: false,
  attribute: false,
  default_namespace: nil,
  polymorphic: {},
  polymorphic_map: {},
  transform: {},
  value_map: {}
)
  super(
    name,
    to: to,
    render_nil: render_nil,
    render_default: render_default,
    render_empty: render_empty,
    treat_nil: treat_nil,
    treat_empty: treat_empty,
    treat_omitted: treat_omitted,
    with: with,
    delegate: delegate,
    attribute: attribute,
    polymorphic: polymorphic,
    polymorphic_map: polymorphic_map,
    transform: transform,
    value_map: value_map,
  )

  @namespace = if namespace.to_s == "inherit"
               # we are using inherit_namespace in xml builder by
               # default so no need to do anything here.
               else
                 namespace
               end
  @prefix = prefix
  @mixed_content = mixed_content
  @cdata = cdata

  @default_namespace = default_namespace

  @namespace_set = namespace_set
  @prefix_set = prefix_set
end

Instance Attribute Details

#cdataObject (readonly)

Returns the value of attribute cdata.



7
8
9
# File 'lib/lutaml/model/xml/mapping_rule.rb', line 7

def cdata
  @cdata
end

#default_namespaceObject (readonly)

Returns the value of attribute default_namespace.



7
8
9
# File 'lib/lutaml/model/xml/mapping_rule.rb', line 7

def default_namespace
  @default_namespace
end

#mixed_contentObject (readonly)

Returns the value of attribute mixed_content.



7
8
9
# File 'lib/lutaml/model/xml/mapping_rule.rb', line 7

def mixed_content
  @mixed_content
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



7
8
9
# File 'lib/lutaml/model/xml/mapping_rule.rb', line 7

def namespace
  @namespace
end

#prefixObject (readonly)

Returns the value of attribute prefix.



7
8
9
# File 'lib/lutaml/model/xml/mapping_rule.rb', line 7

def prefix
  @prefix
end

Instance Method Details

#content_keyObject



79
80
81
# File 'lib/lutaml/model/xml/mapping_rule.rb', line 79

def content_key
  cdata ? "#cdata-section" : "text"
end

#content_mapping?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/lutaml/model/xml/mapping_rule.rb', line 75

def content_mapping?
  name.nil?
end

#deep_dupObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/lutaml/model/xml/mapping_rule.rb', line 116

def deep_dup
  self.class.new(
    name.dup,
    to: to,
    render_nil: render_nil,
    render_default: render_default,
    with: Utils.deep_dup(custom_methods),
    delegate: delegate,
    namespace: namespace.dup,
    prefix: prefix.dup,
    mixed_content: mixed_content,
    cdata: cdata,
    namespace_set: namespace_set?,
    prefix_set: prefix_set?,
    attribute: attribute,
    polymorphic: polymorphic.dup,
    default_namespace: default_namespace.dup,
    transform: transform.dup,
    render_empty: render_empty.dup,
    value_map: Utils.deep_dup(@value_map),
  )
end

#mixed_content?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/lutaml/model/xml/mapping_rule.rb', line 83

def mixed_content?
  !!@mixed_content
end

#namespace_set?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/lutaml/model/xml/mapping_rule.rb', line 67

def namespace_set?
  !!@namespace_set
end

#namespaced_name(parent_namespace = nil, name = self.name) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/lutaml/model/xml/mapping_rule.rb', line 104

def namespaced_name(parent_namespace = nil, name = self.name)
  if name.to_s == "lang"
    "#{prefix}:#{name}"
  elsif namespace_set? || @attribute
    [namespace, name].compact.join(":")
  elsif default_namespace
    "#{default_namespace}:#{name}"
  else
    [parent_namespace, name].compact.join(":")
  end
end

#namespaced_names(parent_namespace = nil) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/lutaml/model/xml/mapping_rule.rb', line 96

def namespaced_names(parent_namespace = nil)
  if multiple_mappings?
    name.map { |rule_name| namespaced_name(parent_namespace, rule_name) }
  else
    [namespaced_name(parent_namespace)]
  end
end

#prefix_set?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/lutaml/model/xml/mapping_rule.rb', line 71

def prefix_set?
  !!@prefix_set
end

#prefixed_nameObject



87
88
89
90
91
92
93
94
# File 'lib/lutaml/model/xml/mapping_rule.rb', line 87

def prefixed_name
  rule_name = multiple_mappings? ? name.first : name
  if prefix
    "#{prefix}:#{rule_name}"
  else
    rule_name
  end
end