representative 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -44,17 +44,23 @@ module Representative
44
44
  end
45
45
 
46
46
  def list_of(name, *args, &block)
47
+ options = args.extract_options!
47
48
  list_subject = args.empty? ? name : args.shift
49
+ raise ArgumentError, "too many arguments" unless args.empty?
50
+ list_attributes = options[:list_attributes]
51
+ raise ArgumentError, "list_attributes #{list_attributes} not supported for json representation" if list_attributes
52
+ item_attributes = options[:item_attributes] || {}
53
+
48
54
  items = resolve_value(list_subject)
49
55
  label(name)
50
56
  inside "[", "]" do
51
57
  items.each do |item|
52
58
  new_item
53
- value(item, &block)
59
+ value(item, item_attributes, &block)
54
60
  end
55
61
  end
56
62
  end
57
-
63
+
58
64
  def value(subject, attributes = {})
59
65
  representing(subject) do
60
66
  if block_given? && !current_subject.nil?
@@ -1,3 +1,3 @@
1
1
  module Representative
2
- VERSION = "0.3.1".freeze
2
+ VERSION = "0.3.2".freeze
3
3
  end
@@ -178,6 +178,55 @@ describe Representative::Json do
178
178
  end
179
179
 
180
180
  end
181
+
182
+ describe "with item attributes" do
183
+ it "adds the attributes with an @ sign to child elements" do
184
+ @authors = [
185
+ OpenStruct.new(:name => "Hewey", :age => 3),
186
+ OpenStruct.new(:name => "Dewey", :age => 4),
187
+ OpenStruct.new(:name => "Louie", :age => 5)
188
+ ]
189
+ r.list_of :authors, @authors, :item_attributes => {:about => lambda{|obj| "#{obj.name} is #{obj.age} years old"}} do
190
+ r.element :name
191
+ r.element :age
192
+ end
193
+ resulting_json.should == undent(<<-JSON)
194
+ [
195
+ {
196
+ "@about": "Hewey is 3 years old",
197
+ "name": "Hewey",
198
+ "age": 3
199
+ },
200
+ {
201
+ "@about": "Dewey is 4 years old",
202
+ "name": "Dewey",
203
+ "age": 4
204
+ },
205
+ {
206
+ "@about": "Louie is 5 years old",
207
+ "name": "Louie",
208
+ "age": 5
209
+ }
210
+ ]
211
+ JSON
212
+ end
213
+ end
214
+
215
+ describe "with list attributes" do
216
+ it "raises an ArgumentError" do
217
+ @authors = []
218
+ lambda{ r.list_of(:authors, @authors, :list_attributes => {}) {} }.should raise_exception(ArgumentError)
219
+ end
220
+ end
221
+
222
+ describe "with unnecessary arguments" do
223
+ it "raises an ArgumentError" do
224
+ @authors = []
225
+ lambda{
226
+ r.list_of(:authors, @authors, :unecessary_arg_should_cause_failure, :item_attributes => {}){}
227
+ }.should raise_exception(ArgumentError)
228
+ end
229
+ end
181
230
 
182
231
  end
183
232
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: representative
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
5
- prerelease: false
4
+ hash: 23
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 1
10
- version: 0.3.1
9
+ - 2
10
+ version: 0.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mike Williams
@@ -15,13 +15,12 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-13 00:00:00 +11:00
18
+ date: 2011-01-17 00:00:00 +11:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: activesupport
23
22
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
24
  none: false
26
25
  requirements:
27
26
  - - ">="
@@ -33,11 +32,11 @@ dependencies:
33
32
  - 2
34
33
  version: 2.2.2
35
34
  type: :runtime
36
- version_requirements: *id001
35
+ requirement: *id001
36
+ name: activesupport
37
37
  - !ruby/object:Gem::Dependency
38
- name: i18n
39
38
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
41
40
  none: false
42
41
  requirements:
43
42
  - - ">="
@@ -49,11 +48,11 @@ dependencies:
49
48
  - 1
50
49
  version: 0.4.1
51
50
  type: :runtime
52
- version_requirements: *id002
51
+ requirement: *id002
52
+ name: i18n
53
53
  - !ruby/object:Gem::Dependency
54
- name: builder
55
54
  prerelease: false
56
- requirement: &id003 !ruby/object:Gem::Requirement
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
57
56
  none: false
58
57
  requirements:
59
58
  - - ">="
@@ -65,11 +64,11 @@ dependencies:
65
64
  - 2
66
65
  version: 2.1.2
67
66
  type: :runtime
68
- version_requirements: *id003
67
+ requirement: *id003
68
+ name: builder
69
69
  - !ruby/object:Gem::Dependency
70
- name: json
71
70
  prerelease: false
72
- requirement: &id004 !ruby/object:Gem::Requirement
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
73
72
  none: false
74
73
  requirements:
75
74
  - - ">="
@@ -81,11 +80,11 @@ dependencies:
81
80
  - 5
82
81
  version: 1.4.5
83
82
  type: :runtime
84
- version_requirements: *id004
83
+ requirement: *id004
84
+ name: json
85
85
  - !ruby/object:Gem::Dependency
86
- name: rake
87
86
  prerelease: false
88
- requirement: &id005 !ruby/object:Gem::Requirement
87
+ version_requirements: &id005 !ruby/object:Gem::Requirement
89
88
  none: false
90
89
  requirements:
91
90
  - - ~>
@@ -97,11 +96,11 @@ dependencies:
97
96
  - 7
98
97
  version: 0.8.7
99
98
  type: :development
100
- version_requirements: *id005
99
+ requirement: *id005
100
+ name: rake
101
101
  - !ruby/object:Gem::Dependency
102
- name: rspec
103
102
  prerelease: false
104
- requirement: &id006 !ruby/object:Gem::Requirement
103
+ version_requirements: &id006 !ruby/object:Gem::Requirement
105
104
  none: false
106
105
  requirements:
107
106
  - - ~>
@@ -113,11 +112,11 @@ dependencies:
113
112
  - 0
114
113
  version: 1.3.0
115
114
  type: :development
116
- version_requirements: *id006
115
+ requirement: *id006
116
+ name: rspec
117
117
  - !ruby/object:Gem::Dependency
118
- name: tilt
119
118
  prerelease: false
120
- requirement: &id007 !ruby/object:Gem::Requirement
119
+ version_requirements: &id007 !ruby/object:Gem::Requirement
121
120
  none: false
122
121
  requirements:
123
122
  - - ">="
@@ -128,11 +127,11 @@ dependencies:
128
127
  - 9
129
128
  version: "0.9"
130
129
  type: :development
131
- version_requirements: *id007
130
+ requirement: *id007
131
+ name: tilt
132
132
  - !ruby/object:Gem::Dependency
133
- name: nokogiri
134
133
  prerelease: false
135
- requirement: &id008 !ruby/object:Gem::Requirement
134
+ version_requirements: &id008 !ruby/object:Gem::Requirement
136
135
  none: false
137
136
  requirements:
138
137
  - - ~>
@@ -144,11 +143,11 @@ dependencies:
144
143
  - 2
145
144
  version: 1.4.2
146
145
  type: :development
147
- version_requirements: *id008
146
+ requirement: *id008
147
+ name: nokogiri
148
148
  - !ruby/object:Gem::Dependency
149
- name: diff-lcs
150
149
  prerelease: false
151
- requirement: &id009 !ruby/object:Gem::Requirement
150
+ version_requirements: &id009 !ruby/object:Gem::Requirement
152
151
  none: false
153
152
  requirements:
154
153
  - - ">="
@@ -158,7 +157,8 @@ dependencies:
158
157
  - 0
159
158
  version: "0"
160
159
  type: :development
161
- version_requirements: *id009
160
+ requirement: *id009
161
+ name: diff-lcs
162
162
  description:
163
163
  email: mdub@dogbiscuit.org
164
164
  executables: []
@@ -219,7 +219,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
219
  requirements: []
220
220
 
221
221
  rubyforge_project:
222
- rubygems_version: 1.3.7
222
+ rubygems_version: 1.4.2
223
223
  signing_key:
224
224
  specification_version: 3
225
225
  summary: Builds XML and JSON representations of your Ruby objects