xommelier 0.1.11 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,8 +23,8 @@ module Xommelier
23
23
  element :author, type: Person
24
24
  element :category, type: Category
25
25
  element :contributor, type: Person
26
- element :entry, type: Entry
27
26
  element :link, type: Link
27
+ element :entry, type: Entry
28
28
  end
29
29
  end
30
30
  end
@@ -1,3 +1,3 @@
1
1
  module Xommelier
2
- VERSION = '0.1.11'
2
+ VERSION = '0.1.12'
3
3
  end
@@ -63,11 +63,12 @@ module Xommelier
63
63
  element_name = options.delete(:element_name) { self.element_name }
64
64
  element_name = element_name.to_s
65
65
  element_name << '_' if %w(text class id).include?(element_name)
66
+ xmlns = options[:ns] || self.xmlns
66
67
  if options[:builder] # Non-root element
67
68
  builder = options.delete(:builder)
68
69
  attribute_values = {}
69
70
  namespaces = builder.doc.namespaces
70
- prefix = builder.doc.namespaces.key(xmlns.uri)[6..-1].presence
71
+ prefix = options[:prefix] || builder.doc.namespaces.key(xmlns.uri)[6..-1].presence
71
72
  else # Root element
72
73
  builder = Nokogiri::XML::Builder.new(options)
73
74
  attribute_values = children_namespaces.inject({xmlns: xmlns.uri}) do |hash, ns|
@@ -83,7 +84,8 @@ module Xommelier
83
84
  attributes.each do |name, value|
84
85
  attribute_options = attribute_options(name)
85
86
  attribute_name = attribute_options[:attribute_name]
86
- if (ns = attribute_options[:ns]).uri != current_xmlns
87
+ ns = attribute_options[:ns]
88
+ if ns.uri != current_xmlns
87
89
  if ns.as == :xml
88
90
  attribute_name = "xml:#{attribute_options[:attribute_name]}"
89
91
  elsif attr_prefix = namespaces.key(ns.uri).try(:[], 6..-1).presence
@@ -94,13 +96,12 @@ module Xommelier
94
96
  end
95
97
  @_xml_node = (prefix ? builder[prefix] : builder).
96
98
  send(element_name, attribute_values) do |xml|
97
- elements.each do |name, value|
98
- serialize_element(
99
- name,
100
- value,
101
- xml,
102
- element_options(name).merge(parent_ns_prefix: prefix)
103
- )
99
+ self.class.elements.each do |name, element_options|
100
+ value = elements.fetch(name, options[:default])
101
+ if value
102
+ serialize_element(name, value, xml,
103
+ element_options.merge(overriden_xmlns: xmlns))
104
+ end
104
105
  end
105
106
  xml.text(@text) if respond_to?(:text)
106
107
  end.instance_variable_get(:@node)
@@ -183,19 +184,22 @@ module Xommelier
183
184
  end
184
185
 
185
186
  def serialize_element(name, value, xml, options = {})
186
- prefix = if options[:ns].try(:!=, xmlns)
187
- xml.doc.namespaces.key(options[:ns].uri)[6..-1].presence
188
- else
189
- nil
190
- end
191
187
  case options[:count]
192
188
  when :any, :many
193
189
  single_element = options.merge(count: :one)
194
190
  value.each { |item| serialize_element(name, item, xml, single_element) }
195
191
  else
192
+ xmlns = options[:overriden_xmlns] || self.xmlns
193
+ prefix = if options[:prefix]
194
+ options[:prefix]
195
+ elsif options[:ns].try(:!=, xmlns)
196
+ xml.doc.namespaces.key(options[:ns].uri)[6..-1].presence
197
+ else
198
+ nil
199
+ end
196
200
  case value
197
201
  when Xommelier::Xml::Element
198
- value.to_xommelier(builder: xml, element_name: options[:element_name])
202
+ value.to_xommelier(builder: xml, element_name: options[:element_name], prefix: prefix, ns: options[:ns])
199
203
  else
200
204
  element_name = options[:element_name].to_s
201
205
  element_name << '_' if %w(text class id).include?(element_name)
@@ -77,7 +77,7 @@ module Xommelier
77
77
  def element(name, options = {})
78
78
  options[:element_name] = options.delete(:as) { name }
79
79
  options[:ns] ||= if options[:type].try(:<, Xml::Element)
80
- options[:ns] = options[:type].xmlns
80
+ options[:type].xmlns
81
81
  else
82
82
  xmlns
83
83
  end
@@ -2,14 +2,14 @@
2
2
  <feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0">
3
3
  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
4
4
  <title>Example Feed</title>
5
- <link href="http://example.org/"/>
6
5
  <updated>2003-12-13T18:30:02Z</updated>
7
6
  <author>
8
7
  <name>John Doe</name>
9
8
  </author>
9
+ <link href="http://example.org/"/>
10
10
  <entry>
11
- <title>Atom-Powered Robots Run Amok</title>
12
11
  <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
12
+ <title>Atom-Powered Robots Run Amok</title>
13
13
  <updated>2003-12-13T18:30:02Z</updated>
14
14
  <summary>Some text.</summary>
15
15
  <link href="http://example.org/2003/12/13/atom03"/>
@@ -1,12 +1,12 @@
1
1
  <?xml version="1.0" encoding="utf-8"?>
2
2
  <feed xmlns="http://www.w3.org/2005/Atom">
3
3
  <title>Xommelier nest elements</title>
4
+ <updated>2012-04-04T04:04:00Z</updated>
4
5
  <subtitle>Xommelier is able to build complex objects from very nested hash</subtitle>
5
6
  <author>
6
7
  <name>Alexander</name>
7
8
  <email>al@semyonov.us</email>
8
9
  </author>
9
- <updated>2012-04-04T04:04:00Z</updated>
10
10
  <contributor>
11
11
  <name>Artyom</name>
12
12
  <email>sevenov@gmail.com</email>
@@ -2,14 +2,14 @@
2
2
  <feed xmlns="http://www.w3.org/2005/Atom">
3
3
  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
4
4
  <title>Example Feed</title>
5
- <link href="http://example.org/"/>
6
5
  <updated>2003-12-13T18:30:02Z</updated>
7
6
  <author>
8
7
  <name>John Doe</name>
9
8
  </author>
9
+ <link href="http://example.org/"/>
10
10
  <entry>
11
- <title>Atom-Powered Robots Run Amok</title>
12
11
  <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
12
+ <title>Atom-Powered Robots Run Amok</title>
13
13
  <updated>2003-12-13T18:30:02Z</updated>
14
14
  <summary>Some text.</summary>
15
15
  <link href="http://example.org/2003/12/13/atom03"/>
@@ -29,6 +29,11 @@ describe 'Atom feed building' do
29
29
  it { feed.should be_valid }
30
30
 
31
31
  it { should == load_xml_file('simple_feed.atom').read }
32
+ it do
33
+ rng.validate(parsed_xml).each do |error|
34
+ pp error
35
+ end
36
+ end
32
37
  it('should conform to RelaxNG schema') { rng.valid?(parsed_xml).should == true }
33
38
  it('should conform to XML Schema') { xsd.valid?(parsed_xml).should == true }
34
39
  end
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Xommelier do
4
- it { Xommelier::VERSION.should == '0.1.11' }
4
+ it { Xommelier::VERSION.should == '0.1.12' }
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.11
4
+ version: 0.1.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-02 00:00:00.000000000 Z
12
+ date: 2012-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri