acdc 0.7.1 → 0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/acdc.rb +1 -1
- data/lib/acdc/item.rb +26 -19
- data/lib/acdc/parse.rb +6 -18
- metadata +2 -2
data/lib/acdc.rb
CHANGED
data/lib/acdc/item.rb
CHANGED
@@ -7,7 +7,9 @@ module AcDc
|
|
7
7
|
def initialize(name, type, options={})
|
8
8
|
@name = name.to_s
|
9
9
|
@type = type
|
10
|
-
@tag = options.delete(:tag) ||
|
10
|
+
@tag = options.delete(:tag) ||
|
11
|
+
@type.tag_name rescue nil ||
|
12
|
+
name.to_s
|
11
13
|
@options = options
|
12
14
|
@xml_type = self.class.to_s.split('::').last.downcase
|
13
15
|
end
|
@@ -29,13 +31,16 @@ module AcDc
|
|
29
31
|
end
|
30
32
|
|
31
33
|
def value_from_xml(node, namespace)
|
32
|
-
|
33
|
-
|
34
|
+
find(node,namespace) do |n|
|
35
|
+
if primitive?
|
34
36
|
value = n.respond_to?(:content) ? n.content : n.to_s
|
35
37
|
typecast(value)
|
38
|
+
else
|
39
|
+
# it is important to "detach" the node from the document
|
40
|
+
# by passing it in as a string. this forces the root xpath
|
41
|
+
# lookup to work properly.
|
42
|
+
constant.acdc(n.to_s)
|
36
43
|
end
|
37
|
-
else
|
38
|
-
constant.parse(node, options)
|
39
44
|
end
|
40
45
|
end
|
41
46
|
|
@@ -72,14 +77,28 @@ module AcDc
|
|
72
77
|
elsif self.namespace
|
73
78
|
namespace = "#{DEFAULT_NAMESPACE}:#{self.namespace}"
|
74
79
|
end
|
75
|
-
|
76
80
|
if element?
|
77
81
|
if(options[:single].nil? || options[:single])
|
82
|
+
|
78
83
|
result = node.find_first(xpath(namespace), namespace)
|
84
|
+
|
85
|
+
if result.nil? and AcDc::parseable_constants.include?(@type)
|
86
|
+
# This makes sure it is found if in the event the Ruby type name
|
87
|
+
# doesn't match the Xml node name, but the type has a tag_name.
|
88
|
+
# Not sure why this would be good ---- edge case fix.
|
89
|
+
result = node.find_first(@type.tag_name, namespace)
|
90
|
+
end
|
91
|
+
|
92
|
+
if result.nil?
|
93
|
+
# This makes sure it is found in the event the Ruby method name
|
94
|
+
# doesn't match the Xml node name.
|
95
|
+
# Not sure why this would be good ---- edge case fix.
|
96
|
+
result = node.find_first(@type.to_s.split('::').last, namespace)
|
97
|
+
end
|
98
|
+
|
79
99
|
else
|
80
100
|
result = node.find(xpath(namespace))
|
81
101
|
end
|
82
|
-
|
83
102
|
if result
|
84
103
|
if(options[:single].nil? || options[:single])
|
85
104
|
value = yield(result)
|
@@ -89,18 +108,6 @@ module AcDc
|
|
89
108
|
value << yield(res)
|
90
109
|
end
|
91
110
|
end
|
92
|
-
if options[:attributes].is_a?(Hash)
|
93
|
-
result.attributes.each do |xml_attribute|
|
94
|
-
if attribute_options = options[:attributes][xml_attribute.name.to_sym]
|
95
|
-
attribute_value = Attribute.new(xml_attribute.name.to_sym, *attribute_options).from_xml_node(result, namespace)
|
96
|
-
result.instance_eval <<-EOV
|
97
|
-
def value.#{xml_attribute.name}
|
98
|
-
#{attribute_value.inspect}
|
99
|
-
end
|
100
|
-
EOV
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
111
|
value
|
105
112
|
else
|
106
113
|
nil
|
data/lib/acdc/parse.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module AcDc
|
2
2
|
module Parsing
|
3
3
|
|
4
|
-
def acdc(xml
|
4
|
+
def acdc(xml)
|
5
5
|
if xml.is_a?(XML::Node)
|
6
6
|
node = xml
|
7
7
|
else
|
@@ -11,14 +11,13 @@ module AcDc
|
|
11
11
|
node = XML::Parser.string(xml).parse.root
|
12
12
|
end
|
13
13
|
end
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
klass = constantize(node.name)
|
14
|
+
klass = AcDc.parseable_constants.detect{ |const|
|
15
|
+
const.name.downcase =~ /#{node.name.downcase}/ || const.tag_name == node.name
|
16
|
+
}
|
18
17
|
if klass.nil?
|
19
18
|
raise Exception.new("Uh Oh ... Live Wire! Couldn't parse #{node.name}.")
|
20
19
|
end
|
21
|
-
root = node.name.downcase == klass.tag_name
|
20
|
+
root = node.name.downcase == klass.tag_name.downcase
|
22
21
|
namespace = node.namespaces.default
|
23
22
|
namespace = "#{DEFAULT_NAMESPACE}:#{namespace}" if namespace
|
24
23
|
xpath = root ? '/' : './/'
|
@@ -36,23 +35,12 @@ module AcDc
|
|
36
35
|
obj
|
37
36
|
end
|
38
37
|
nodes = nil
|
39
|
-
if
|
38
|
+
if root
|
40
39
|
collection.first
|
41
40
|
else
|
42
41
|
collection
|
43
42
|
end
|
44
43
|
end
|
45
|
-
|
46
|
-
private
|
47
|
-
def constantize(type)
|
48
|
-
# I'm using a separate store for constants because of the fact at
|
49
|
-
# runtime during parsing we will simply get a tag name to start with.
|
50
|
-
# It won't be in a form we could use to detect a constant normally.
|
51
|
-
# Also - this way provides a cleaner check for the tag_name feature.
|
52
|
-
AcDc.parseable_constants.detect{ |const|
|
53
|
-
const.name.downcase =~ /#{type.downcase}/ || const.tag_name == type
|
54
|
-
}
|
55
|
-
end
|
56
44
|
|
57
45
|
end
|
58
46
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acdc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Clint Hill
|
@@ -78,6 +78,6 @@ rubyforge_project:
|
|
78
78
|
rubygems_version: 1.3.5
|
79
79
|
signing_key:
|
80
80
|
specification_version: 3
|
81
|
-
summary: acdc 0.7.
|
81
|
+
summary: acdc 0.7.2
|
82
82
|
test_files: []
|
83
83
|
|