attributor 2.1.0

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 (57) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +4 -0
  5. data/CHANGELOG.md +52 -0
  6. data/Gemfile +3 -0
  7. data/Guardfile +12 -0
  8. data/LICENSE +22 -0
  9. data/README.md +62 -0
  10. data/Rakefile +28 -0
  11. data/attributor.gemspec +40 -0
  12. data/lib/attributor.rb +89 -0
  13. data/lib/attributor/attribute.rb +271 -0
  14. data/lib/attributor/attribute_resolver.rb +116 -0
  15. data/lib/attributor/dsl_compiler.rb +106 -0
  16. data/lib/attributor/exceptions.rb +38 -0
  17. data/lib/attributor/extensions/randexp.rb +10 -0
  18. data/lib/attributor/type.rb +117 -0
  19. data/lib/attributor/types/boolean.rb +26 -0
  20. data/lib/attributor/types/collection.rb +135 -0
  21. data/lib/attributor/types/container.rb +42 -0
  22. data/lib/attributor/types/csv.rb +10 -0
  23. data/lib/attributor/types/date_time.rb +36 -0
  24. data/lib/attributor/types/file_upload.rb +11 -0
  25. data/lib/attributor/types/float.rb +27 -0
  26. data/lib/attributor/types/hash.rb +337 -0
  27. data/lib/attributor/types/ids.rb +26 -0
  28. data/lib/attributor/types/integer.rb +63 -0
  29. data/lib/attributor/types/model.rb +316 -0
  30. data/lib/attributor/types/object.rb +19 -0
  31. data/lib/attributor/types/string.rb +25 -0
  32. data/lib/attributor/types/struct.rb +50 -0
  33. data/lib/attributor/types/tempfile.rb +36 -0
  34. data/lib/attributor/version.rb +3 -0
  35. data/spec/attribute_resolver_spec.rb +227 -0
  36. data/spec/attribute_spec.rb +597 -0
  37. data/spec/attributor_spec.rb +25 -0
  38. data/spec/dsl_compiler_spec.rb +130 -0
  39. data/spec/spec_helper.rb +30 -0
  40. data/spec/support/models.rb +81 -0
  41. data/spec/support/types.rb +21 -0
  42. data/spec/type_spec.rb +134 -0
  43. data/spec/types/boolean_spec.rb +85 -0
  44. data/spec/types/collection_spec.rb +286 -0
  45. data/spec/types/container_spec.rb +49 -0
  46. data/spec/types/csv_spec.rb +17 -0
  47. data/spec/types/date_time_spec.rb +90 -0
  48. data/spec/types/file_upload_spec.rb +6 -0
  49. data/spec/types/float_spec.rb +78 -0
  50. data/spec/types/hash_spec.rb +372 -0
  51. data/spec/types/ids_spec.rb +32 -0
  52. data/spec/types/integer_spec.rb +151 -0
  53. data/spec/types/model_spec.rb +401 -0
  54. data/spec/types/string_spec.rb +55 -0
  55. data/spec/types/struct_spec.rb +189 -0
  56. data/spec/types/tempfile_spec.rb +6 -0
  57. metadata +348 -0
@@ -0,0 +1,55 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe Attributor::String do
4
+
5
+ subject(:type) { Attributor::String }
6
+
7
+ context '.native_type' do
8
+ it "returns String" do
9
+ type.native_type.should be(::String)
10
+ end
11
+ end
12
+
13
+ context '.example' do
14
+ it "should return a valid String" do
15
+ type.example(options:{regexp: /\w\d{2,3}/}).should be_a(::String)
16
+ end
17
+
18
+ it "should return a valid String" do
19
+ type.example.should be_a(::String)
20
+ end
21
+
22
+ it 'handles regexps that Randexp can not' do
23
+ pending 'resolution of #72' do
24
+ regex = /\w+(,\w+)*/
25
+ expect {
26
+ val = Attributor::String.example(options:{regexp: regex})
27
+ val.should be_a(::String)
28
+ }.to_not raise_error
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ context '.load' do
35
+ let(:value) { nil }
36
+
37
+ context 'for incoming String values' do
38
+
39
+ it 'returns the incoming value' do
40
+ ['', 'foo', '0.0', '-1.0', '1.0', '1e-10', 1].each do |value|
41
+ type.load(value).should eq(String(value))
42
+ end
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ context 'for incoming Symbol values' do
49
+ let(:value) { :something }
50
+ it 'returns the stringified-value' do
51
+ type.load(value).should == value.to_s
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,189 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe Attributor::Struct do
4
+
5
+ context '.definition for a Struct with no sub-attributes' do
6
+ subject { Attributor::Struct }
7
+ it 'raises an error' do
8
+ expect {
9
+ subject.definition
10
+ }.to raise_error(Attributor::AttributorException,"Can not use a pure Struct without defining sub-attributes")
11
+ end
12
+
13
+ end
14
+ context '.construct' do
15
+
16
+ context 'empty struct' do
17
+ let(:attribute_definition) do
18
+ Proc.new {}
19
+ end
20
+
21
+ subject(:empty_struct) { Attributor::Struct.construct(attribute_definition) }
22
+
23
+ it 'constructs a struct with no attributes' do
24
+ empty_struct.should < Attributor::Struct
25
+
26
+ attributes = empty_struct.attributes
27
+ attributes.should be_empty
28
+ end
29
+ end
30
+
31
+ context 'simple struct' do
32
+ let(:attribute_definition) do
33
+ Proc.new do
34
+ attribute :age, Attributor::Integer
35
+ end
36
+ end
37
+
38
+ subject(:simple_struct) { Attributor::Struct.construct(attribute_definition) }
39
+
40
+ it 'constructs a struct with one attribute' do
41
+ simple_struct.should < Attributor::Struct
42
+
43
+ attributes = simple_struct.attributes
44
+ attributes.should have_key :age
45
+ end
46
+ end
47
+
48
+ context 'less simple struct' do
49
+ let(:attribute_definition) do
50
+ Proc.new do
51
+ attribute :age, Attributor::Integer
52
+ attribute :name, Attributor::String
53
+ attribute :employed?, Attributor::Boolean
54
+ attribute :salary, Attributor::Float
55
+ attribute :hired_at, Attributor::DateTime
56
+ end
57
+ end
58
+
59
+ subject(:large_struct) { Attributor::Struct.construct(attribute_definition) }
60
+
61
+ it 'constructs a struct with five attributes' do
62
+ large_struct.should < Attributor::Struct
63
+
64
+ attributes = large_struct.attributes
65
+ attributes.should have_key :age
66
+ attributes.should have_key :name
67
+ attributes.should have_key :employed?
68
+ attributes.should have_key :salary
69
+ attributes.should have_key :hired_at
70
+ end
71
+ end
72
+
73
+ context 'complex struct containing model' do
74
+ let(:attribute_definition) do
75
+ Proc.new do
76
+ attribute :pet, ::Chicken
77
+ end
78
+ end
79
+
80
+ subject(:struct_of_models) { Attributor::Struct.construct(attribute_definition) }
81
+
82
+ it 'constructs a struct with a model attribute' do
83
+ struct_of_models.should < Attributor::Struct
84
+
85
+ attributes = struct_of_models.attributes
86
+ attributes.should have_key :pet
87
+ end
88
+ end
89
+
90
+ context 'complex struct containing named struct' do
91
+ let(:attribute_definition) do
92
+ Proc.new do
93
+ attribute :stats, Attributor::Struct do
94
+ attribute :months, Attributor::Integer
95
+ attribute :days, Attributor::Integer
96
+ end
97
+ end
98
+ end
99
+
100
+ subject(:struct_of_structs) { Attributor::Struct.construct(attribute_definition) }
101
+
102
+ it 'constructs a struct with a named struct attribute' do
103
+ struct_of_structs.should < Attributor::Struct
104
+
105
+ attributes = struct_of_structs.attributes
106
+ attributes.should have_key :stats
107
+
108
+ stats = attributes[:stats].attributes
109
+ stats.should have_key :months
110
+ stats.should have_key :days
111
+ end
112
+ end
113
+
114
+ context 'complex struct containing multi-level recursive structs' do
115
+ let(:attribute_definition) do
116
+ Proc.new do
117
+ attribute :arthropods, Attributor::Struct do
118
+ attribute :insects, Attributor::Struct do
119
+ attribute :ants, Attributor::Struct do
120
+ attribute :name, Attributor::String
121
+ attribute :age, Attributor::Integer
122
+ attribute :weight, Attributor::Float
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ subject(:multi_level_struct_of_structs) { Attributor::Struct.construct(attribute_definition) }
130
+
131
+ it 'constructs a struct with multiple levels of named struct attributes' do
132
+ multi_level_struct_of_structs.should < Attributor::Struct
133
+
134
+ root = multi_level_struct_of_structs.attributes
135
+ root.should have_key :arthropods
136
+
137
+ arthropods = root[:arthropods].attributes
138
+ arthropods.should have_key :insects
139
+
140
+ insects = arthropods[:insects].attributes
141
+ insects.should have_key :ants
142
+
143
+ ants = insects[:ants].attributes
144
+ ants.should have_key :name
145
+ ants.should have_key :age
146
+ ants.should have_key :weight
147
+ end
148
+ end
149
+
150
+ context 'with a reference' do
151
+ let(:reference) { Chicken }
152
+ let(:attribute_definition) do
153
+ proc do
154
+ end
155
+ end
156
+ subject(:struct) { Attributor::Struct.construct(attribute_definition, options)}
157
+
158
+ context 'with new type-level options' do
159
+ let(:options) { {reference: reference} }
160
+ its(:options) { should have_key(:identity) }
161
+ it 'inherits from the reference' do
162
+ struct.options[:identity].should eq(reference.options[:identity])
163
+ end
164
+ it 'does not raise an error when used in an attribute' do
165
+ expect {
166
+ Attributor::Attribute.new(struct)
167
+ }.to_not raise_error
168
+ end
169
+ end
170
+
171
+ context 'with existing type-level options' do
172
+ let(:options) { {reference: reference, identity: :name} }
173
+ its(:options) { should have_key(:identity) }
174
+ it 'does not override from the reference' do
175
+ struct.options[:identity].should eq(:name)
176
+ end
177
+ it 'does not raise an error when used in an attribute' do
178
+ expect {
179
+ Attributor::Attribute.new(struct)
180
+ }.to_not raise_error
181
+ end
182
+
183
+ end
184
+ end
185
+
186
+
187
+ end
188
+
189
+ end
@@ -0,0 +1,6 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+
4
+ describe Attributor::Tempfile do
5
+ it 'has specs'
6
+ end
metadata ADDED
@@ -0,0 +1,348 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attributor
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josep M. Blanquer
8
+ - Dane Jensen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-08-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hashie
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: randexp
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "<"
47
+ - !ruby/object:Gem::Version
48
+ version: '2.99'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "<"
54
+ - !ruby/object:Gem::Version
55
+ version: '2.99'
56
+ - !ruby/object:Gem::Dependency
57
+ name: yard
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 0.8.7
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 0.8.7
70
+ - !ruby/object:Gem::Dependency
71
+ name: backports
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '3'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3'
84
+ - !ruby/object:Gem::Dependency
85
+ name: yardstick
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: redcarpet
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "<"
103
+ - !ruby/object:Gem::Version
104
+ version: '3.0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "<"
110
+ - !ruby/object:Gem::Version
111
+ version: '3.0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: bundler
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rake-notes
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - "~>"
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ - !ruby/object:Gem::Dependency
141
+ name: simplecov
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - "~>"
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - "~>"
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ - !ruby/object:Gem::Dependency
155
+ name: guard
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - "~>"
159
+ - !ruby/object:Gem::Version
160
+ version: '2'
161
+ type: :development
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - "~>"
166
+ - !ruby/object:Gem::Version
167
+ version: '2'
168
+ - !ruby/object:Gem::Dependency
169
+ name: guard-rspec
170
+ requirement: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - "~>"
173
+ - !ruby/object:Gem::Version
174
+ version: '4'
175
+ type: :development
176
+ prerelease: false
177
+ version_requirements: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - "~>"
180
+ - !ruby/object:Gem::Version
181
+ version: '4'
182
+ - !ruby/object:Gem::Dependency
183
+ name: pry
184
+ requirement: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - "~>"
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ type: :development
190
+ prerelease: false
191
+ version_requirements: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - "~>"
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ - !ruby/object:Gem::Dependency
197
+ name: pry-byebug
198
+ requirement: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - "~>"
201
+ - !ruby/object:Gem::Version
202
+ version: '1'
203
+ type: :development
204
+ prerelease: false
205
+ version_requirements: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - "~>"
208
+ - !ruby/object:Gem::Version
209
+ version: '1'
210
+ - !ruby/object:Gem::Dependency
211
+ name: pry-stack_explorer
212
+ requirement: !ruby/object:Gem::Requirement
213
+ requirements:
214
+ - - "~>"
215
+ - !ruby/object:Gem::Version
216
+ version: '0'
217
+ type: :development
218
+ prerelease: false
219
+ version_requirements: !ruby/object:Gem::Requirement
220
+ requirements:
221
+ - - "~>"
222
+ - !ruby/object:Gem::Version
223
+ version: '0'
224
+ - !ruby/object:Gem::Dependency
225
+ name: fuubar
226
+ requirement: !ruby/object:Gem::Requirement
227
+ requirements:
228
+ - - "~>"
229
+ - !ruby/object:Gem::Version
230
+ version: '1'
231
+ type: :development
232
+ prerelease: false
233
+ version_requirements: !ruby/object:Gem::Requirement
234
+ requirements:
235
+ - - "~>"
236
+ - !ruby/object:Gem::Version
237
+ version: '1'
238
+ description:
239
+ email:
240
+ - blanquer@gmail.com
241
+ - dane.jensen@gmail.com
242
+ executables: []
243
+ extensions: []
244
+ extra_rdoc_files: []
245
+ files:
246
+ - ".gitignore"
247
+ - ".rspec"
248
+ - ".travis.yml"
249
+ - CHANGELOG.md
250
+ - Gemfile
251
+ - Guardfile
252
+ - LICENSE
253
+ - README.md
254
+ - Rakefile
255
+ - attributor.gemspec
256
+ - lib/attributor.rb
257
+ - lib/attributor/attribute.rb
258
+ - lib/attributor/attribute_resolver.rb
259
+ - lib/attributor/dsl_compiler.rb
260
+ - lib/attributor/exceptions.rb
261
+ - lib/attributor/extensions/randexp.rb
262
+ - lib/attributor/type.rb
263
+ - lib/attributor/types/boolean.rb
264
+ - lib/attributor/types/collection.rb
265
+ - lib/attributor/types/container.rb
266
+ - lib/attributor/types/csv.rb
267
+ - lib/attributor/types/date_time.rb
268
+ - lib/attributor/types/file_upload.rb
269
+ - lib/attributor/types/float.rb
270
+ - lib/attributor/types/hash.rb
271
+ - lib/attributor/types/ids.rb
272
+ - lib/attributor/types/integer.rb
273
+ - lib/attributor/types/model.rb
274
+ - lib/attributor/types/object.rb
275
+ - lib/attributor/types/string.rb
276
+ - lib/attributor/types/struct.rb
277
+ - lib/attributor/types/tempfile.rb
278
+ - lib/attributor/version.rb
279
+ - spec/attribute_resolver_spec.rb
280
+ - spec/attribute_spec.rb
281
+ - spec/attributor_spec.rb
282
+ - spec/dsl_compiler_spec.rb
283
+ - spec/spec_helper.rb
284
+ - spec/support/models.rb
285
+ - spec/support/types.rb
286
+ - spec/type_spec.rb
287
+ - spec/types/boolean_spec.rb
288
+ - spec/types/collection_spec.rb
289
+ - spec/types/container_spec.rb
290
+ - spec/types/csv_spec.rb
291
+ - spec/types/date_time_spec.rb
292
+ - spec/types/file_upload_spec.rb
293
+ - spec/types/float_spec.rb
294
+ - spec/types/hash_spec.rb
295
+ - spec/types/ids_spec.rb
296
+ - spec/types/integer_spec.rb
297
+ - spec/types/model_spec.rb
298
+ - spec/types/string_spec.rb
299
+ - spec/types/struct_spec.rb
300
+ - spec/types/tempfile_spec.rb
301
+ homepage: https://github.com/rightscale/attributor
302
+ licenses:
303
+ - MIT
304
+ metadata: {}
305
+ post_install_message:
306
+ rdoc_options: []
307
+ require_paths:
308
+ - lib
309
+ required_ruby_version: !ruby/object:Gem::Requirement
310
+ requirements:
311
+ - - ">="
312
+ - !ruby/object:Gem::Version
313
+ version: '2.1'
314
+ required_rubygems_version: !ruby/object:Gem::Requirement
315
+ requirements:
316
+ - - ">="
317
+ - !ruby/object:Gem::Version
318
+ version: '0'
319
+ requirements: []
320
+ rubyforge_project:
321
+ rubygems_version: 2.2.1
322
+ signing_key:
323
+ specification_version: 4
324
+ summary: A powerful attribute and type management library for Ruby
325
+ test_files:
326
+ - spec/attribute_resolver_spec.rb
327
+ - spec/attribute_spec.rb
328
+ - spec/attributor_spec.rb
329
+ - spec/dsl_compiler_spec.rb
330
+ - spec/spec_helper.rb
331
+ - spec/support/models.rb
332
+ - spec/support/types.rb
333
+ - spec/type_spec.rb
334
+ - spec/types/boolean_spec.rb
335
+ - spec/types/collection_spec.rb
336
+ - spec/types/container_spec.rb
337
+ - spec/types/csv_spec.rb
338
+ - spec/types/date_time_spec.rb
339
+ - spec/types/file_upload_spec.rb
340
+ - spec/types/float_spec.rb
341
+ - spec/types/hash_spec.rb
342
+ - spec/types/ids_spec.rb
343
+ - spec/types/integer_spec.rb
344
+ - spec/types/model_spec.rb
345
+ - spec/types/string_spec.rb
346
+ - spec/types/struct_spec.rb
347
+ - spec/types/tempfile_spec.rb
348
+ has_rdoc: