acdc 0.7.3 → 0.7.4
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.
- data/README +89 -10
- data/lib/acdc.rb +1 -1
- data/lib/acdc/build.rb +14 -4
- data/lib/acdc/item.rb +6 -2
- data/lib/acdc/mapping.rb +4 -7
- data/lib/acdc/parse.rb +1 -1
- metadata +3 -3
data/README
CHANGED
@@ -17,20 +17,99 @@ This is a little XML-to-object-to-XML library that gets Dirty Deeds Done Dirt Ch
|
|
17
17
|
AcDc::Body assists you with declaring XML objects with ease. And #acdc makes
|
18
18
|
marshaling those objects from XML a breeze.
|
19
19
|
|
20
|
+
==== Simple Data Model
|
21
|
+
|
22
|
+
This example will go over a simple Address data model and all of the ways you could use it.
|
23
|
+
|
24
|
+
require 'rubygems'
|
20
25
|
require 'acdc'
|
21
|
-
|
22
|
-
|
23
|
-
|
26
|
+
|
27
|
+
class Address < AcDc::Body
|
28
|
+
attribute :type, String, :tag => "Type"
|
24
29
|
end
|
25
30
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
puts Address.new.acdc
|
32
|
+
#=> <?xml version="1.0" encoding="UTF-8"?><address Type=""></address>
|
33
|
+
|
34
|
+
First thing to point out is the #attribute class method that allows you to specify
|
35
|
+
the name of the method (:type) the type of the data (String) and the XML tag ("Type").
|
36
|
+
|
37
|
+
You can also do this with #element.
|
38
|
+
|
39
|
+
class Address < AcDc::Body
|
40
|
+
attribute :type, String, :tag => "Type"
|
41
|
+
element :street, String, :tag => "Street"
|
42
|
+
end
|
43
|
+
|
44
|
+
puts Address.new.acdc
|
45
|
+
#=> <?xml version="1.0" encoding="UTF-8"?><address Type=""><Street/></address>
|
46
|
+
|
47
|
+
The :tag parameter is optional. The reason it's demonstrated here is simply to capitalize
|
48
|
+
the attribute/element tag. You could :tag any element/attribute with another name for rendering.
|
49
|
+
By default AcDc will output XML in lowercase.
|
50
|
+
|
51
|
+
class Address < AcDc::Body
|
52
|
+
attribute :type, String
|
53
|
+
element :street, String
|
54
|
+
end
|
55
|
+
|
56
|
+
puts Address.new.acdc
|
57
|
+
#=> <?xml version="1.0" encoding="UTF-8"?><address type=""><street/></address>
|
58
|
+
|
59
|
+
You could also specify a custom type for the second parameter. In the following example the
|
60
|
+
Street element will be created as a custom type.
|
61
|
+
|
62
|
+
class Street < AcDc::Body
|
63
|
+
element :line_1, String
|
64
|
+
end
|
65
|
+
|
66
|
+
class Address < AcDc::Body
|
67
|
+
attribute :type, String
|
68
|
+
element :street, Street
|
69
|
+
end
|
70
|
+
|
71
|
+
add = Address.new
|
72
|
+
add.street = Street.new
|
73
|
+
add.street.line_1 = "1234 Somewhere"
|
74
|
+
|
75
|
+
puts add.acdc
|
76
|
+
#=> <?xml version="1.0" encoding="UTF-8"?><address type=""><street><line_1>1234 Somewhere</line_1></street></address>
|
77
|
+
|
78
|
+
AcDc will also recognize collections of elements. You can do this with the :single
|
79
|
+
parameter. Here is an example:
|
80
|
+
|
81
|
+
class Street < AcDc::Body
|
82
|
+
element :line_1, String
|
83
|
+
end
|
84
|
+
|
85
|
+
class Address < AcDc::Body
|
86
|
+
attribute :type, String
|
87
|
+
element :streets, Street, :single => false
|
88
|
+
end
|
89
|
+
|
90
|
+
add = Address.new
|
91
|
+
street1 = Street.new
|
92
|
+
street2 = Street.new
|
93
|
+
street1.line_1 = "1234 Somewhere"
|
94
|
+
street2.line_1 = "5678 Somwhere Else"
|
95
|
+
add.streets = [street1,street2]
|
96
|
+
puts add.acdc
|
97
|
+
#=> <?xml version="1.0" encoding="UTF-8"?><address type=""><street><line_1>1234 Somewhere</line_1></street><street><line_1>5678 Somwhere Else</line_1></street></address>
|
98
|
+
|
99
|
+
The final example is the Dc part - Xml to Object. The following example uses the Street and Address
|
100
|
+
classes above and the #acdc method to derive the objects from the XML string.
|
33
101
|
|
102
|
+
addy = acdc <<EOF
|
103
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
104
|
+
<address type="">
|
105
|
+
<street><line_1>1234 Somewhere</line_1></street>
|
106
|
+
<street><line_1>5678 Somwhere Else</line_1></street>
|
107
|
+
</address>
|
108
|
+
EOF
|
109
|
+
|
110
|
+
puts addy.inspect
|
111
|
+
#<Address:0x3342f8 @type="", @streets=[#<Street:0x32abb8 @line_1="1234 Somewhere">, #<Street:0x329290 @line_1="5678 Somwhere Else">]>
|
112
|
+
|
34
113
|
== Contact
|
35
114
|
|
36
115
|
- Author:: Clint Hill clint.hill@h3osoftware.com
|
data/lib/acdc.rb
CHANGED
data/lib/acdc/build.rb
CHANGED
@@ -6,7 +6,7 @@ module AcDc
|
|
6
6
|
def acdc(root = true)
|
7
7
|
xml = Builder::XmlMarkup.new
|
8
8
|
attrs = self.class.attributes.inject({}){ |acc,attr|
|
9
|
-
acc.update(attr.
|
9
|
+
acc.update(attr.tag => send(attr.method_name.to_sym))
|
10
10
|
}
|
11
11
|
attrs.update(:xmlns => self.class.namespace) if self.class.namespace
|
12
12
|
xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8" if root
|
@@ -14,10 +14,20 @@ module AcDc
|
|
14
14
|
self.class.elements.each do |elem|
|
15
15
|
value = send(elem.method_name.to_sym)
|
16
16
|
if value
|
17
|
-
if elem.
|
18
|
-
|
17
|
+
if elem.single?
|
18
|
+
if elem.primitive?
|
19
|
+
body.tag! elem.tag, value
|
20
|
+
else
|
21
|
+
body << value.acdc(false)
|
22
|
+
end
|
19
23
|
else
|
20
|
-
|
24
|
+
value.each { |v|
|
25
|
+
if v.kind_of?(AcDc::Body)
|
26
|
+
body << v.acdc(false)
|
27
|
+
else
|
28
|
+
body.tag! elem.tag, v
|
29
|
+
end
|
30
|
+
}
|
21
31
|
end
|
22
32
|
else
|
23
33
|
body.tag! elem.tag if elem.renderable?
|
data/lib/acdc/item.rb
CHANGED
@@ -28,6 +28,10 @@ module AcDc
|
|
28
28
|
@constant ||= constantize(type)
|
29
29
|
end
|
30
30
|
|
31
|
+
def single?
|
32
|
+
options[:single].nil? || options[:single]
|
33
|
+
end
|
34
|
+
|
31
35
|
def element?
|
32
36
|
@xml_type == 'element'
|
33
37
|
end
|
@@ -84,7 +88,7 @@ module AcDc
|
|
84
88
|
namespace = "#{DEFAULT_NAMESPACE}:#{self.namespace}"
|
85
89
|
end
|
86
90
|
if element?
|
87
|
-
if(
|
91
|
+
if(single?)
|
88
92
|
|
89
93
|
result = node.find_first(xpath(namespace), namespace)
|
90
94
|
|
@@ -106,7 +110,7 @@ module AcDc
|
|
106
110
|
result = node.find(xpath(namespace))
|
107
111
|
end
|
108
112
|
if result
|
109
|
-
if(
|
113
|
+
if(single?)
|
110
114
|
value = yield(result)
|
111
115
|
else
|
112
116
|
value = []
|
data/lib/acdc/mapping.rb
CHANGED
@@ -49,13 +49,10 @@ module AcDc
|
|
49
49
|
|
50
50
|
private
|
51
51
|
def make_accessor(item)
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
# from our own @elements/@attributes
|
57
|
-
if instance_methods.include?(safe_name)
|
58
|
-
name = "#{item.element? ? "element_" : "attribute_"}#{safe_name}"
|
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}"
|
59
56
|
item.name = name
|
60
57
|
end
|
61
58
|
attr_accessor item.method_name.intern
|
data/lib/acdc/parse.rb
CHANGED
@@ -11,7 +11,7 @@ module AcDc
|
|
11
11
|
node = XML::Parser.string(xml).parse.root
|
12
12
|
end
|
13
13
|
end
|
14
|
-
klass = AcDc.parseable_constants.
|
14
|
+
klass = AcDc.parseable_constants.find{ |const|
|
15
15
|
const.name.downcase =~ /#{node.name.downcase}/ || const.tag_name == node.name
|
16
16
|
}
|
17
17
|
if klass.nil?
|
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.4
|
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:
|
12
|
+
date: 2010-01-01 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.
|
81
|
+
summary: acdc 0.7.4
|
82
82
|
test_files: []
|
83
83
|
|