acdc 0.7.4 → 0.7.5

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.
@@ -11,7 +11,7 @@ class Boolean; end
11
11
  module AcDc
12
12
 
13
13
  DEFAULT_NAMESPACE = "acdc"
14
- VERSION = [0,7,4]
14
+ VERSION = [0,7,5]
15
15
 
16
16
  def self.parseable_constants
17
17
  @parseables ||= []
@@ -1,6 +1,8 @@
1
1
  module AcDc
2
2
  class Body
3
3
 
4
+ attr_accessor :value
5
+
4
6
  include Mapping
5
7
  include Building
6
8
 
@@ -29,7 +29,7 @@ module AcDc
29
29
  end
30
30
 
31
31
  def single?
32
- options[:single].nil? || options[:single]
32
+ @single ||= options[:single].nil? || options[:single]
33
33
  end
34
34
 
35
35
  def element?
@@ -41,16 +41,12 @@ module AcDc
41
41
  end
42
42
 
43
43
  def value_from_xml(node, namespace)
44
- find(node,namespace) do |n|
45
- if primitive?
46
- value = n.respond_to?(:content) ? n.content : n.to_s
47
- typecast(value)
48
- else
49
- # it is important to "detach" the node from the document
50
- # by passing it in as a string. this forces the root xpath
51
- # lookup to work properly.
52
- constant.acdc(n.to_s)
53
- end
44
+ n = find(node,namespace)
45
+ if primitive?
46
+ value = n.respond_to?(:content) ? n.content : n.to_s
47
+ typecast(value)
48
+ else
49
+ constant.acdc(n)
54
50
  end
55
51
  end
56
52
 
@@ -79,51 +75,15 @@ module AcDc
79
75
  end
80
76
  end
81
77
 
82
- def find(node, namespace, &block)
83
- if options[:namespace] == false
84
- namespace = nil
85
- elsif options[:namespace]
86
- namespace = "#{DEFAULT_NAMESPACE}:#{options[:namespace]}"
87
- elsif self.namespace
88
- namespace = "#{DEFAULT_NAMESPACE}:#{self.namespace}"
89
- end
78
+ def find(node, namespace)
90
79
  if element?
91
- if(single?)
92
-
93
- result = node.find_first(xpath(namespace), namespace)
94
-
95
- if result.nil? and AcDc::parseable_constants.include?(@type)
96
- # This makes sure it is found if in the event the Ruby type name
97
- # doesn't match the Xml node name, but the type has a tag_name.
98
- # Not sure why this would be good ---- edge case fix.
99
- result = node.find_first(@type.tag_name, namespace)
100
- end
101
-
102
- if result.nil?
103
- # This makes sure it is found in the event the Ruby method name
104
- # doesn't match the Xml node name.
105
- # Not sure why this would be good ---- edge case fix.
106
- result = node.find_first(@type.to_s.split('::').last, namespace)
107
- end
108
-
109
- else
110
- result = node.find(xpath(namespace))
111
- end
112
- if result
113
- if(single?)
114
- value = yield(result)
115
- else
116
- value = []
117
- result.each do |res|
118
- value << yield(res)
119
- end
120
- end
121
- value
122
- else
123
- nil
80
+ result = single? ? node.find_first(xpath(namespace), namespace) : node.find(xpath(namespace))
81
+ unless result.nil?
82
+ value = single? ? result : result.each { |res| (value ||= []) << res }
124
83
  end
84
+ value
125
85
  else
126
- yield(node[tag])
86
+ node[tag]
127
87
  end
128
88
  end
129
89
 
@@ -45,18 +45,17 @@ module AcDc
45
45
  def namespace(namespace = nil)
46
46
  @namespace = namespace if namespace
47
47
  @namespace
48
- end
49
-
50
- private
51
- def make_accessor(item)
52
- in_elems = elements.find{|e| e.name == item.name and e != item}
53
- in_attrs = attributes.find{|a| a.name == item.name and a != item}
54
- if in_elems or in_attrs
55
- name = "#{item.element? ? "element_" : "attribute_"}#{item.name}"
56
- item.name = name
57
- end
58
- attr_accessor item.method_name.intern
48
+ end
49
+
50
+ def make_accessor(item)
51
+ in_elems = elements.find{|e| e.name == item.name and e != item}
52
+ in_attrs = attributes.find{|a| a.name == item.name and a != item}
53
+ if in_elems or in_attrs
54
+ name = "#{item.element? ? "element_" : "attribute_"}#{item.name}"
55
+ item.name = name
59
56
  end
57
+ attr_accessor item.method_name.intern
58
+ end
60
59
 
61
60
  end
62
61
 
@@ -2,6 +2,8 @@ module AcDc
2
2
  module Parsing
3
3
 
4
4
  def acdc(xml)
5
+ return nil if xml.nil?
6
+
5
7
  if xml.is_a?(XML::Node)
6
8
  node = xml
7
9
  else
@@ -11,35 +13,33 @@ module AcDc
11
13
  node = XML::Parser.string(xml).parse.root
12
14
  end
13
15
  end
16
+
14
17
  klass = AcDc.parseable_constants.find{ |const|
15
18
  const.name.downcase =~ /#{node.name.downcase}/ || const.tag_name == node.name
16
19
  }
20
+
17
21
  if klass.nil?
18
22
  raise Exception.new("Uh Oh ... Live Wire! Couldn't parse #{node.name}.")
19
23
  end
20
- root = node.name.downcase == klass.tag_name.downcase
24
+
21
25
  namespace = node.namespaces.default
22
26
  namespace = "#{DEFAULT_NAMESPACE}:#{namespace}" if namespace
23
- xpath = root ? '/' : './/'
24
- xpath += "#{DEFAULT_NAMESPACE}:" if namespace
25
- xpath += node.name
26
- nodes = node.find(xpath, Array(namespace))
27
- collection = nodes.collect do |n|
28
- obj = klass.new
29
- klass.attributes.each do |attr|
30
- obj.send("#{attr.method_name}=", attr.value_from_xml(n, namespace))
31
- end
27
+
28
+ obj = klass.new
29
+
30
+ klass.attributes.each do |attr|
31
+ obj.send("#{attr.method_name}=", attr.value_from_xml(node, namespace))
32
+ end
33
+
34
+ if klass.elements.size > 0
32
35
  klass.elements.each do |elem|
33
- obj.send("#{elem.method_name}=", elem.value_from_xml(n, namespace))
36
+ obj.send("#{elem.method_name}=", elem.value_from_xml(node, namespace))
34
37
  end
35
- obj
36
- end
37
- nodes = nil
38
- if root
39
- collection.first
40
38
  else
41
- collection
39
+ obj.value = node.respond_to?(:content) ? node.content : node.to_s
42
40
  end
41
+
42
+ obj
43
43
  end
44
44
 
45
45
  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
4
+ version: 0.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clint Hill
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-01 00:00:00 -07:00
12
+ date: 2010-01-16 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -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.4
81
+ summary: acdc 0.7.5
82
82
  test_files: []
83
83