representative 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +1 -1
- data/lib/representative/version.rb +1 -1
- data/lib/representative/xml.rb +21 -10
- data/representative.gemspec +2 -2
- data/spec/representative/xml_spec.rb +19 -10
- metadata +2 -2
data/README.markdown
CHANGED
@@ -96,7 +96,7 @@ Installation
|
|
96
96
|
|
97
97
|
Representative is packaged as a Gem, hosted on [gemcutter](http://gemcutter.org/gems/representative). Install with:
|
98
98
|
|
99
|
-
sudo gem install representative
|
99
|
+
sudo gem install representative
|
100
100
|
|
101
101
|
Copyright
|
102
102
|
---------
|
data/lib/representative/xml.rb
CHANGED
@@ -8,7 +8,7 @@ module Representative
|
|
8
8
|
|
9
9
|
def initialize(xml_builder, subject = nil)
|
10
10
|
@xml = xml_builder
|
11
|
-
@
|
11
|
+
@subjects = [subject]
|
12
12
|
yield self if block_given?
|
13
13
|
end
|
14
14
|
|
@@ -21,7 +21,7 @@ module Representative
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def subject!
|
24
|
-
@
|
24
|
+
@subjects.last
|
25
25
|
end
|
26
26
|
|
27
27
|
def property!(property_name, *args, &block)
|
@@ -39,18 +39,23 @@ module Representative
|
|
39
39
|
|
40
40
|
end
|
41
41
|
|
42
|
-
def element!(element_name,
|
43
|
-
|
44
|
-
if block &&
|
42
|
+
def element!(element_name, subject, options, &block)
|
43
|
+
content = content_generator = nil
|
44
|
+
if block && subject
|
45
45
|
unless block == Representative::EMPTY
|
46
46
|
content_generator = Proc.new do
|
47
|
-
|
47
|
+
@subjects.push(subject)
|
48
|
+
begin
|
49
|
+
block.call(self)
|
50
|
+
ensure
|
51
|
+
@subjects.pop
|
52
|
+
end
|
48
53
|
end
|
49
54
|
end
|
50
55
|
else
|
51
|
-
|
56
|
+
content = subject
|
52
57
|
end
|
53
|
-
tag_args = [
|
58
|
+
tag_args = [content, options].compact
|
54
59
|
@xml.tag!(element_name, *tag_args, &content_generator)
|
55
60
|
end
|
56
61
|
|
@@ -77,10 +82,16 @@ module Representative
|
|
77
82
|
|
78
83
|
end
|
79
84
|
|
85
|
+
def empty!(property_name, *args)
|
86
|
+
property!(property_name, *args, &Representative::EMPTY)
|
87
|
+
end
|
88
|
+
|
80
89
|
private
|
81
90
|
|
82
|
-
def resolve(value_generator, subject =
|
83
|
-
if value_generator
|
91
|
+
def resolve(value_generator, subject = subject!)
|
92
|
+
if value_generator == :self
|
93
|
+
subject
|
94
|
+
elsif value_generator.respond_to?(:to_proc)
|
84
95
|
value_generator.to_proc.call(subject) if subject
|
85
96
|
else
|
86
97
|
value_generator
|
data/representative.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{representative}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Mike Williams"]
|
12
|
-
s.date = %q{2009-12-
|
12
|
+
s.date = %q{2009-12-03}
|
13
13
|
s.email = %q{mdub@dogbiscuit.org}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE",
|
@@ -21,7 +21,7 @@ describe Representative::Xml do
|
|
21
21
|
describe "for some 'subject'" do
|
22
22
|
|
23
23
|
before do
|
24
|
-
@subject = OpenStruct.new(:name => "Fred", :width => 200)
|
24
|
+
@subject = OpenStruct.new(:name => "Fred", :width => 200, :vehicle => OpenStruct.new(:year => "1959", :make => "Chevrolet"))
|
25
25
|
@xml_representative = Representative::Xml.new(@xml, @subject)
|
26
26
|
end
|
27
27
|
|
@@ -97,6 +97,17 @@ describe Representative::Xml do
|
|
97
97
|
|
98
98
|
end
|
99
99
|
|
100
|
+
describe "with argument :self" do
|
101
|
+
|
102
|
+
it "doesn't alter the subject" do
|
103
|
+
represent.info(:self) do |info|
|
104
|
+
info.name
|
105
|
+
end
|
106
|
+
resulting_xml.should == %(<info><name>Fred</name></info>)
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
100
111
|
describe "with a nil argument" do
|
101
112
|
|
102
113
|
it "builds an empty element" do
|
@@ -129,7 +140,6 @@ describe Representative::Xml do
|
|
129
140
|
describe "with a block" do
|
130
141
|
|
131
142
|
it "generates nested elements" do
|
132
|
-
@subject.vehicle = OpenStruct.new(:year => "1959", :make => "Chevrolet")
|
133
143
|
represent.vehicle do |vehicle|
|
134
144
|
vehicle.year
|
135
145
|
vehicle.make
|
@@ -139,18 +149,17 @@ describe Representative::Xml do
|
|
139
149
|
|
140
150
|
end
|
141
151
|
|
142
|
-
|
152
|
+
end
|
143
153
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
154
|
+
describe "#empty!" do
|
155
|
+
|
156
|
+
it "generates an empty element" do
|
157
|
+
represent.empty!(:vehicle, :year => :year)
|
158
|
+
resulting_xml.should == %(<vehicle year="1959"/>)
|
150
159
|
end
|
151
160
|
|
152
161
|
end
|
153
|
-
|
162
|
+
|
154
163
|
describe "#list_of!" do
|
155
164
|
|
156
165
|
before do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: representative
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Williams
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-03 00:00:00 +11:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|