Class: Shale::Mapping::XmlBase Private

Inherits:
Object
  • Object
show all
Defined in:
lib/shale/mapping/xml_base.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Base class for Mapping XML serialization format

Direct Known Subclasses

Xml, XmlGroup

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeXmlBase

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize instance



62
63
64
65
66
67
68
69
70
71
# File 'lib/shale/mapping/xml_base.rb', line 62

def initialize
  super
  @elements = {}
  @attributes = {}
  @content = nil
  @root = ''
  @default_namespace = Descriptor::XmlNamespace.new
  @finalized = false
  @render_nil_default = false
end

Instance Attribute Details

#attributesHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return attributes mapping hash

Returns:

  • (Hash)


25
26
27
# File 'lib/shale/mapping/xml_base.rb', line 25

def attributes
  @attributes
end

#contentSymbol (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return content mapping

Returns:

  • (Symbol)


32
33
34
# File 'lib/shale/mapping/xml_base.rb', line 32

def content
  @content
end

#default_namespaceShale::Mapping::Descriptor::XmlNamespace (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return default namespace



39
40
41
# File 'lib/shale/mapping/xml_base.rb', line 39

def default_namespace
  @default_namespace
end

#elementsHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return elements mapping hash

Returns:

  • (Hash)


18
19
20
# File 'lib/shale/mapping/xml_base.rb', line 18

def elements
  @elements
end

Instance Method Details

#finalize!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set the “finalized” instance variable to true



212
213
214
# File 'lib/shale/mapping/xml_base.rb', line 212

def finalize!
  @finalized = true
end

#finalized?truem false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Query the “finalized” instance variable

Returns:

  • (truem false)


221
222
223
# File 'lib/shale/mapping/xml_base.rb', line 221

def finalized?
  @finalized
end

#initialize_dup(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



226
227
228
229
230
231
232
233
# File 'lib/shale/mapping/xml_base.rb', line 226

def initialize_dup(other)
  @elements = other.instance_variable_get('@elements').dup
  @attributes = other.instance_variable_get('@attributes').dup
  @default_namespace = other.instance_variable_get('@default_namespace').dup
  @finalized = false

  super
end

#map_attribute(attribute, to: nil, receiver: nil, using: nil, group: nil, namespace: nil, prefix: nil, render_nil: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Map document’s attribute to object’s attribute

Parameters:

  • attribute (String)
  • to (Symbol, nil) (defaults to: nil)
  • receiver (Symbol, nil) (defaults to: nil)
  • using (Hash, nil) (defaults to: nil)
  • group (String, nil) (defaults to: nil)
  • namespace (String, nil) (defaults to: nil)
  • prefix (String, nil) (defaults to: nil)
  • render_nil (true, false, nil) (defaults to: nil)

Raises:



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/shale/mapping/xml_base.rb', line 138

def map_attribute(
  attribute,
  to: nil,
  receiver: nil,
  using: nil,
  group: nil,
  namespace: nil,
  prefix: nil,
  render_nil: nil
)
  Validator.validate_arguments(attribute, to, receiver, using)
  Validator.validate_namespace(attribute, namespace, prefix)

  namespaced_attribute = [namespace, attribute].compact.join(':')

  @attributes[namespaced_attribute] = Descriptor::Xml.new(
    name: attribute,
    attribute: to,
    receiver: receiver,
    methods: using,
    namespace: Descriptor::XmlNamespace.new(namespace, prefix),
    cdata: false,
    group: group,
    render_nil: render_nil.nil? ? @render_nil_default : render_nil
  )
end

#map_content(to: nil, receiver: nil, using: nil, group: nil, cdata: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Map document’s content to object’s attribute

Parameters:

  • to (Symbol) (defaults to: nil)
  • receiver (Symbol, nil) (defaults to: nil)
  • using (Hash, nil) (defaults to: nil)
  • group (String, nil) (defaults to: nil)
  • cdata (true, false) (defaults to: false)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/shale/mapping/xml_base.rb', line 174

def map_content(to: nil, receiver: nil, using: nil, group: nil, cdata: false)
  Validator.validate_arguments('content', to, receiver, using)

  @content = Descriptor::Xml.new(
    name: nil,
    attribute: to,
    receiver: receiver,
    methods: using,
    namespace: Descriptor::XmlNamespace.new(nil, nil),
    cdata: cdata,
    group: group,
    render_nil: false
  )
end

#map_element(element, to: nil, receiver: nil, using: nil, group: nil, namespace: :undefined, prefix: :undefined, cdata: false, render_nil: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Map element to attribute

Parameters:

  • element (String)
  • to (Symbol, nil) (defaults to: nil)
  • receiver (Symbol, nil) (defaults to: nil)
  • using (Hash, nil) (defaults to: nil)
  • group (String, nil) (defaults to: nil)
  • namespace (String, nil) (defaults to: :undefined)
  • prefix (String, nil) (defaults to: :undefined)
  • cdata (true, false) (defaults to: false)
  • render_nil (true, false, nil) (defaults to: nil)

Raises:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/shale/mapping/xml_base.rb', line 88

def map_element(
  element,
  to: nil,
  receiver: nil,
  using: nil,
  group: nil,
  namespace: :undefined,
  prefix: :undefined,
  cdata: false,
  render_nil: nil
)
  Validator.validate_arguments(element, to, receiver, using)
  Validator.validate_namespace(element, namespace, prefix)

  if namespace == :undefined && prefix == :undefined
    nsp = default_namespace.name
    pfx = default_namespace.prefix
  else
    nsp = namespace
    pfx = prefix
  end

  namespaced_element = [nsp, element].compact.join(':')

  @elements[namespaced_element] = Descriptor::Xml.new(
    name: element,
    attribute: to,
    receiver: receiver,
    methods: using,
    group: group,
    namespace: Descriptor::XmlNamespace.new(nsp, pfx),
    cdata: cdata,
    render_nil: render_nil.nil? ? @render_nil_default : render_nil
  )
end

#namespace(name, prefix) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set default namespace for root element

Parameters:

  • name (String)
  • prefix (String)


204
205
206
207
# File 'lib/shale/mapping/xml_base.rb', line 204

def namespace(name, prefix)
  @default_namespace.name = name
  @default_namespace.prefix = prefix
end

#prefixed_rootString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return prefixed root

Returns:

  • (String)


55
56
57
# File 'lib/shale/mapping/xml_base.rb', line 55

def prefixed_root
  [default_namespace.prefix, @root].compact.join(':')
end

#root(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set the name for root element

Parameters:

  • value (String)

    root’s name



194
195
196
# File 'lib/shale/mapping/xml_base.rb', line 194

def root(value)
  @root = value
end

#unprefixed_rootString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return unprefixed root

Returns:

  • (String)


46
47
48
# File 'lib/shale/mapping/xml_base.rb', line 46

def unprefixed_root
  @root
end