openxml-package 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/openxml-package/version.rb +1 -1
- data/lib/openxml/builder.rb +2 -23
- data/lib/openxml/builder/element.rb +7 -17
- data/test/element_test.rb +38 -0
- metadata +4 -3
- data/lib/openxml/builder/namespace.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4ac578cfd771b07653d4fdc0fec13bf8880369e
|
4
|
+
data.tar.gz: 03a116e7818d9fcc01c9a1384bc678227a4b2f1e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be40b47eda1c018675dd49cf3f7979fbd32bf9623a5528ab18697dbb25cce9c4c3c544a57071fca663749bfe015a9fdc8b07f00709c60670fb6b2e978ec1ec81
|
7
|
+
data.tar.gz: 1a3a835d1deb48d3dd0e376552abda27754a07762fcda8c09236582f01ef800b2349f995e97406fe13699a76aac5b5dcae1b1a6d1b8a28051daed313888413a1
|
data/lib/openxml/builder.rb
CHANGED
@@ -5,7 +5,6 @@
|
|
5
5
|
# This class mimics the XML Builder DSL.
|
6
6
|
require "ox"
|
7
7
|
require "openxml/builder/element"
|
8
|
-
require "openxml/builder/namespace"
|
9
8
|
|
10
9
|
module OpenXml
|
11
10
|
class Builder
|
@@ -28,39 +27,19 @@ module OpenXml
|
|
28
27
|
end
|
29
28
|
alias :to_xml :to_s
|
30
29
|
|
31
|
-
# Adapted from Nokogiri's builder.rb
|
32
30
|
def [](ns)
|
33
|
-
|
34
|
-
@ns = @parent.namespace_definitions.find { |x| x.prefix == ns.to_s }
|
35
|
-
end
|
36
|
-
return self if @ns
|
37
|
-
|
38
|
-
@parent.ancestors.each do |a|
|
39
|
-
next if a == @document
|
40
|
-
@ns = a.namespace_definitions.find { |x| x.prefix == ns.to_s }
|
41
|
-
return self if @ns
|
42
|
-
end
|
43
|
-
|
44
|
-
@ns = { :pending => ns.to_s }
|
31
|
+
@ns = ns.to_sym if ns
|
45
32
|
return self
|
46
33
|
end
|
47
34
|
|
48
35
|
def method_missing(tag_name, *args)
|
49
36
|
new_element = OpenXml::Builder::Element.new(tag_name)
|
50
|
-
new_element.parent = @parent
|
51
37
|
attributes = extract_options!(args)
|
52
38
|
attributes.each do |key, value|
|
53
39
|
new_element[key] = value
|
54
40
|
end
|
55
41
|
|
56
|
-
|
57
|
-
if @ns.is_a? OpenXml::Builder::Namespace
|
58
|
-
new_element.namespace = @ns
|
59
|
-
elsif @ns.is_a? Hash
|
60
|
-
new_element.namespace = new_element.namespace_definitions.find { |x| x.prefix == @ns[:pending] }
|
61
|
-
raise ArgumentError, "Namespace #{@ns[:pending]} has not been defined" if new_element.namespace.nil?
|
62
|
-
end
|
63
|
-
|
42
|
+
new_element.namespace = @ns
|
64
43
|
@ns = nil
|
65
44
|
|
66
45
|
if block_given?
|
@@ -2,7 +2,6 @@ module OpenXml
|
|
2
2
|
class Builder
|
3
3
|
class Element < SimpleDelegator
|
4
4
|
attr_reader :namespace
|
5
|
-
attr_accessor :parent
|
6
5
|
|
7
6
|
def initialize(*args)
|
8
7
|
super Ox::Element.new(*args)
|
@@ -10,15 +9,18 @@ module OpenXml
|
|
10
9
|
|
11
10
|
def []=(attribute, value)
|
12
11
|
namespace_def = attribute.downcase.to_s.match /^xmlns(?:\:(?<prefix>.*))?$/
|
13
|
-
|
12
|
+
namespaces << namespace_def[:prefix].to_sym if namespace_def && namespace_def[:prefix]
|
14
13
|
super
|
15
14
|
end
|
16
15
|
|
17
16
|
def namespace=(ns)
|
18
|
-
@namespace = ns
|
17
|
+
@namespace = ns.to_sym if ns
|
19
18
|
tag = name.match(/^(?:\w*?\:)?(?<tag>\w*)$/i)[:tag]
|
20
|
-
self.value = "#{namespace
|
21
|
-
|
19
|
+
self.value = "#{namespace}:#{tag}" if namespace
|
20
|
+
end
|
21
|
+
|
22
|
+
def namespaces=(ns)
|
23
|
+
@namespaces = Array(ns)
|
22
24
|
end
|
23
25
|
|
24
26
|
def namespaces
|
@@ -26,18 +28,6 @@ module OpenXml
|
|
26
28
|
end
|
27
29
|
alias :namespace_definitions :namespaces
|
28
30
|
|
29
|
-
def ancestors
|
30
|
-
parents = [self]
|
31
|
-
parents << parent.ancestors unless parent.nil? || !parent.respond_to?(:ancestors)
|
32
|
-
parents.flatten
|
33
|
-
end
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
def add_namespace(prefix, uri)
|
38
|
-
namespaces << OpenXml::Builder::Namespace.new(prefix, uri)
|
39
|
-
end
|
40
|
-
|
41
31
|
end
|
42
32
|
end
|
43
33
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ElementTest < ActiveSupport::TestCase
|
4
|
+
attr_reader :element
|
5
|
+
|
6
|
+
setup do
|
7
|
+
@element = OpenXml::Builder::Element.new("tag")
|
8
|
+
end
|
9
|
+
|
10
|
+
context "The element" do
|
11
|
+
should "correctly assign attributes" do
|
12
|
+
element[:attribute] = "value"
|
13
|
+
element["namespaced:attribute"] = "value"
|
14
|
+
dump = Ox.dump(element.__getobj__)
|
15
|
+
assert_equal "\n<tag attribute=\"value\" namespaced:attribute=\"value\"/>\n", dump
|
16
|
+
end
|
17
|
+
|
18
|
+
should "parse out available namespace prefixes from namespace definition attributes" do
|
19
|
+
nsdef_prefix = "xmlns:namespace"
|
20
|
+
nsdef_uri = "http://schema.somenamespace.org/"
|
21
|
+
element[nsdef_prefix] = nsdef_uri
|
22
|
+
assert_equal [:namespace], element.namespaces
|
23
|
+
end
|
24
|
+
|
25
|
+
should "only parse namespace definition attributes with a prefix" do
|
26
|
+
element["xmlns"] = "http://schema.somenamespace.org/"
|
27
|
+
assert element.namespaces.empty?
|
28
|
+
end
|
29
|
+
|
30
|
+
should "correctly use a namespace prefix" do
|
31
|
+
element.namespace = "namespace"
|
32
|
+
dump = Ox.dump(element.__getobj__)
|
33
|
+
assert_equal "\n<namespace:tag/>\n", dump
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openxml-package
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bob Lail
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubyzip
|
@@ -214,7 +214,6 @@ files:
|
|
214
214
|
- lib/openxml-package/version.rb
|
215
215
|
- lib/openxml/builder.rb
|
216
216
|
- lib/openxml/builder/element.rb
|
217
|
-
- lib/openxml/builder/namespace.rb
|
218
217
|
- lib/openxml/content_types_presets.rb
|
219
218
|
- lib/openxml/errors.rb
|
220
219
|
- lib/openxml/package.rb
|
@@ -227,6 +226,7 @@ files:
|
|
227
226
|
- lib/openxml/types.rb
|
228
227
|
- openxml-package.gemspec
|
229
228
|
- test/content_types_test.rb
|
229
|
+
- test/element_test.rb
|
230
230
|
- test/package_test.rb
|
231
231
|
- test/part_test.rb
|
232
232
|
- test/support/sample.docx
|
@@ -257,6 +257,7 @@ specification_version: 4
|
|
257
257
|
summary: A Ruby implementation of OpenXmlPackage
|
258
258
|
test_files:
|
259
259
|
- test/content_types_test.rb
|
260
|
+
- test/element_test.rb
|
260
261
|
- test/package_test.rb
|
261
262
|
- test/part_test.rb
|
262
263
|
- test/support/sample.docx
|