representative 0.2.1 → 0.2.3

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.
@@ -1,3 +1,3 @@
1
1
  module Representative
2
- VERSION = "0.2.1".freeze
2
+ VERSION = "0.2.3".freeze
3
3
  end
@@ -26,19 +26,16 @@ module Representative
26
26
  # This object will provide element values where they haven't been
27
27
  # explicitly provided.
28
28
  #
29
- def subject
29
+ def current_subject
30
30
  @subjects.last
31
31
  end
32
32
 
33
+ alias :subject :current_subject
34
+
33
35
  # Evaluate a block with a specified object as #subject.
34
36
  #
35
- def representing(subject)
36
- @subjects.push(subject)
37
- begin
38
- yield subject
39
- ensure
40
- @subjects.pop
41
- end
37
+ def representing(new_subject, &block)
38
+ with_subject(resolve_value(new_subject), &block)
42
39
  end
43
40
 
44
41
  # Generate an element.
@@ -73,12 +70,12 @@ module Representative
73
70
  #
74
71
  def element(name, *args, &block)
75
72
 
76
- attributes = args.extract_options!
77
- attributes = attributes.merge(@inspector.get_metadata(subject, name))
73
+ metadata = @inspector.get_metadata(current_subject, name)
74
+ attributes = args.extract_options!.merge(metadata)
78
75
 
79
- value_generator = if args.empty?
76
+ subject_of_element = if args.empty?
80
77
  lambda do |subject|
81
- @inspector.get_value(subject, name)
78
+ @inspector.get_value(current_subject, name)
82
79
  end
83
80
  else
84
81
  args.shift
@@ -86,18 +83,22 @@ module Representative
86
83
 
87
84
  raise ArgumentError, "too many arguments" unless args.empty?
88
85
 
89
- value = resolve_value(value_generator)
90
- return @xml.tag!(name) if value.nil?
91
-
92
- representing(value) do
93
-
94
- content_string = subject.to_s unless block
95
- content_block = unless block.nil? || block == Representative::EMPTY
96
- Proc.new do
97
- block.call(subject)
86
+ representing(subject_of_element) do
87
+
88
+ content_string = content_block = nil
89
+
90
+ unless current_subject.nil?
91
+ if block
92
+ unless block == Representative::EMPTY
93
+ content_block = Proc.new do
94
+ block.call(current_subject)
95
+ end
96
+ end
97
+ else
98
+ content_string = current_subject.to_s
98
99
  end
99
100
  end
100
-
101
+
101
102
  resolved_attributes = resolve_attributes(attributes)
102
103
  tag_args = [content_string, resolved_attributes].compact
103
104
 
@@ -124,15 +125,15 @@ module Representative
124
125
  def list_of(name, *args, &block)
125
126
 
126
127
  options = args.extract_options!
127
- value_generator = args.empty? ? name : args.shift
128
+ list_subject = args.empty? ? name : args.shift
128
129
  raise ArgumentError, "too many arguments" unless args.empty?
129
130
 
130
131
  list_attributes = options[:list_attributes] || {}
131
132
  item_name = options[:item_name] || name.to_s.singularize
132
133
  item_attributes = options[:item_attributes] || {}
133
134
 
134
- items = resolve_value(value_generator)
135
- element(name, items, list_attributes.merge(:type => "array")) do
135
+ items = resolve_value(list_subject)
136
+ element(name, items, list_attributes.merge(:type => lambda{"array"})) do
136
137
  items.each do |item|
137
138
  element(item_name, item, item_attributes, &block)
138
139
  end
@@ -150,13 +151,27 @@ module Representative
150
151
  Representative::EMPTY
151
152
  end
152
153
 
154
+ # Generate a comment
155
+ def comment(text)
156
+ @xml.comment!(text)
157
+ end
158
+
153
159
  private
154
160
 
155
- def resolve_value(value_generator, subject = subject)
161
+ def with_subject(subject)
162
+ @subjects.push(subject)
163
+ begin
164
+ yield subject
165
+ ensure
166
+ @subjects.pop
167
+ end
168
+ end
169
+
170
+ def resolve_value(value_generator)
156
171
  if value_generator == :self
157
- subject
172
+ current_subject
158
173
  elsif value_generator.respond_to?(:to_proc)
159
- value_generator.to_proc.call(subject) if subject
174
+ value_generator.to_proc.call(current_subject) unless current_subject.nil?
160
175
  else
161
176
  value_generator
162
177
  end
@@ -165,7 +180,7 @@ module Representative
165
180
  def resolve_attributes(attributes)
166
181
  if attributes
167
182
  attributes.inject({}) do |resolved, (name, value_generator)|
168
- resolved_value = resolve_value(value_generator, subject)
183
+ resolved_value = resolve_value(value_generator)
169
184
  resolved[name.to_s.dasherize] = resolved_value unless resolved_value.nil?
170
185
  resolved
171
186
  end
@@ -116,11 +116,16 @@ describe Representative::Xml do
116
116
 
117
117
  describe "and attributes" do
118
118
 
119
- it "omits the attributes" do
119
+ it "omits attributes derived from the subject" do
120
120
  r.element :name, nil, :size => :size
121
121
  resulting_xml.should == %(<name/>)
122
122
  end
123
123
 
124
+ it "retains attributes with explicit values" do
125
+ r.element :name, nil, :lang => "en"
126
+ resulting_xml.should == %(<name lang="en"/>)
127
+ end
128
+
124
129
  end
125
130
 
126
131
  describe "and a block" do
@@ -209,8 +214,10 @@ describe Representative::Xml do
209
214
  describe "with an argument that resolves to nil" do
210
215
 
211
216
  it "omits the attribute" do
212
- r.list_of(:flags)
213
- resulting_xml.should == %(<flags/>)
217
+ r.list_of(:services) do
218
+ r.date
219
+ end
220
+ resulting_xml.should == %(<services/>)
214
221
  end
215
222
 
216
223
  end
@@ -248,6 +255,30 @@ describe Representative::Xml do
248
255
 
249
256
  end
250
257
 
258
+ describe "#representing" do
259
+
260
+ it "selects a new subject without generating an element" do
261
+ r.representing :vehicle do
262
+ r.element :make
263
+ end
264
+ resulting_xml.should == %(<make>Chevrolet</make>)
265
+ end
266
+
267
+ end
268
+
269
+ describe "#comment" do
270
+
271
+ it "inserts a comment" do
272
+ r.element :vehicle do
273
+ r.comment "Year of manufacture"
274
+ r.element :year
275
+ end
276
+ resulting_xml.should ==
277
+ %(<vehicle><!-- Year of manufacture --><year>1959</year></vehicle>)
278
+ end
279
+
280
+ end
281
+
251
282
  end
252
283
 
253
284
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 1
9
- version: 0.2.1
8
+ - 3
9
+ version: 0.2.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Mike Williams
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-04 00:00:00 +10:00
17
+ date: 2010-08-11 00:00:00 +10:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency