acdc 0.7.1 → 0.7.2

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.
Files changed (4) hide show
  1. data/lib/acdc.rb +1 -1
  2. data/lib/acdc/item.rb +26 -19
  3. data/lib/acdc/parse.rb +6 -18
  4. metadata +2 -2
data/lib/acdc.rb CHANGED
@@ -11,7 +11,7 @@ class Boolean; end
11
11
  module AcDc
12
12
 
13
13
  DEFAULT_NAMESPACE = "acdc"
14
- VERSION = [0,7,1]
14
+ VERSION = [0,7,2]
15
15
 
16
16
  def self.parseable_constants
17
17
  @parseables ||= []
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) || name.to_s
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
- if primitive?
33
- find(node, namespace) do |n|
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, options={})
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
- # This constantize is a global scope constantize.
15
- # It will use AcDc#parseable_constants to detect which class
16
- # is being parsed.
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 options[:single] || root
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.1
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.1
81
+ summary: acdc 0.7.2
82
82
  test_files: []
83
83