xommelier 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module Xommelier
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
@@ -64,6 +64,7 @@ module Xommelier
64
64
  if options[:builder] # Non-root element
65
65
  builder = options.delete(:builder)
66
66
  attribute_values = {}
67
+ namespaces = builder.doc.namespaces
67
68
  prefix = builder.doc.namespaces.key(xmlns.uri)[6..-1].presence
68
69
  else # Root element
69
70
  builder = Nokogiri::XML::Builder.new(options)
@@ -72,19 +73,27 @@ module Xommelier
72
73
  hash
73
74
  end
74
75
  attribute_values.delete("xmlns:#{xmlns.as.to_s}")
76
+ attribute_values.delete("xmlns:xml")
77
+ namespaces = attribute_values
75
78
  prefix = nil
76
79
  end
77
80
  current_xmlns = builder.doc.namespaces[prefix ? "xmlns:#{prefix}" : 'xmlns']
78
81
  attributes.each do |name, value|
79
- if (ns = self.class.attributes[name][:ns]).uri != current_xmlns && attr_prefix = builder.doc.namespaces.key(ns.uri).try(:[], 6..-1).presence
80
- name = "#{attr_prefix}:#{name}"
82
+ attribute_options = attribute_options(name)
83
+ attribute_name = attribute_options[:attribute_name]
84
+ if (ns = attribute_options[:ns]).uri != current_xmlns
85
+ if ns.as == :xml
86
+ attribute_name = "xml:#{attribute_options[:attribute_name]}"
87
+ elsif attr_prefix = namespaces.key(ns.uri).try(:[], 6..-1).presence
88
+ attribute_name = "#{attr_prefix}:#{attribute_options[:attribute_name]}"
89
+ end
81
90
  end
82
- serialize_attribute(name, value, attribute_values)
91
+ serialize_attribute(attribute_name, value, attribute_values)
83
92
  end
84
93
  (prefix ? builder[prefix] : builder).
85
94
  send(element_name, attribute_values) do |xml|
86
95
  elements.each do |name, value|
87
- serialize_element(name, value, xml, self.class.elements[name].merge(parent_ns_prefix: prefix))
96
+ serialize_element(name, value, xml, element_options(name).merge(parent_ns_prefix: prefix))
88
97
  end
89
98
  if respond_to?(:text)
90
99
  xml.text @text
@@ -104,8 +113,11 @@ module Xommelier
104
113
  elements.inject(namespaces) do |result, (name, children)|
105
114
  element_options = self.class.elements[name]
106
115
  result << element_options[:ns]
116
+ result += attributes.keys.map { |name| attribute_options(name)[:ns] }
107
117
  if element_options[:type] < Xml::Element
108
- Array(children).each { |child| result += child.children_namespaces }
118
+ Array(children).each do |child|
119
+ result += child.children_namespaces
120
+ end
109
121
  end
110
122
  result
111
123
  end
@@ -67,7 +67,7 @@ module Xommelier
67
67
  def inherited(child)
68
68
  child.elements = elements.dup
69
69
  child.attributes = attributes.dup
70
- include Xml::CommonAttributes
70
+ child.send(:include, Xml::CommonAttributes)
71
71
  end
72
72
 
73
73
  # Defines containing element
@@ -86,6 +86,7 @@ module Xommelier
86
86
 
87
87
  # Defines containing attribute
88
88
  def attribute(name, options = {})
89
+ options[:attribute_name] = options.delete(:as) { name }
89
90
  options[:ns] ||= xmlns
90
91
  attributes[name] = DEFAULT_OPTIONS.merge(options)
91
92
  define_attribute_accessors(name)
@@ -27,6 +27,14 @@ module Xommelier
27
27
  @attributes = {}
28
28
  @text = nil
29
29
 
30
+ self.class.attributes.each do |name, options|
31
+ send("#{name}=", options[:default]) if options[:default]
32
+ end
33
+
34
+ self.class.elements.each do |name, options|
35
+ send("#{name}=", options[:default]) if options[:default]
36
+ end
37
+
30
38
  case contents
31
39
  when Hash
32
40
  contents.each do |name, value|
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Xommelier do
4
- it { Xommelier::VERSION.should == '0.1.4' }
4
+ it { Xommelier::VERSION.should == '0.1.5' }
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xommelier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &70270196058780 !ruby/object:Gem::Requirement
16
+ requirement: &70275292162600 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.5.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70270196058780
24
+ version_requirements: *70275292162600
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &70270196058120 !ruby/object:Gem::Requirement
27
+ requirement: &70275292149460 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.2.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70270196058120
35
+ version_requirements: *70275292149460
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activemodel
38
- requirement: &70270196057660 !ruby/object:Gem::Requirement
38
+ requirement: &70275292120260 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 3.2.0
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70270196057660
46
+ version_requirements: *70275292120260
47
47
  description: XML-Object Mapper with many built-in XML formats supported
48
48
  email:
49
49
  - al@semyonov.us