attributor 2.6.1 → 3.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.
- checksums.yaml +4 -4
- data/.travis.yml +2 -1
- data/CHANGELOG.md +38 -26
- data/lib/attributor.rb +8 -7
- data/lib/attributor/attribute.rb +41 -25
- data/lib/attributor/dsl_compiler.rb +7 -1
- data/lib/attributor/type.rb +10 -8
- data/lib/attributor/types/collection.rb +49 -10
- data/lib/attributor/types/csv.rb +8 -1
- data/lib/attributor/types/hash.rb +45 -11
- data/lib/attributor/types/model.rb +13 -10
- data/lib/attributor/types/string.rb +4 -4
- data/lib/attributor/types/uri.rb +57 -0
- data/lib/attributor/version.rb +1 -1
- data/spec/attribute_spec.rb +136 -64
- data/spec/support/models.rb +7 -2
- data/spec/type_spec.rb +13 -3
- data/spec/types/collection_spec.rb +66 -19
- data/spec/types/csv_spec.rb +14 -3
- data/spec/types/hash_spec.rb +134 -10
- data/spec/types/ids_spec.rb +1 -1
- data/spec/types/model_spec.rb +78 -18
- data/spec/types/string_spec.rb +6 -8
- data/spec/types/uri_spec.rb +12 -0
- metadata +6 -3
data/spec/types/ids_spec.rb
CHANGED
data/spec/types/model_spec.rb
CHANGED
@@ -1,9 +1,48 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
|
2
2
|
|
3
3
|
describe Attributor::Model do
|
4
|
+
subject(:chicken) { Chicken }
|
5
|
+
|
6
|
+
# TODO: should move most of these specs to hash spec
|
7
|
+
|
8
|
+
context 'attributes' do
|
9
|
+
context 'with an exception from the definition block' do
|
10
|
+
subject(:broken_model) do
|
11
|
+
Class.new(Attributor::Model) do
|
12
|
+
attributes do
|
13
|
+
raise 'sorry :('
|
14
|
+
attribute :name, String
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'throws original exception upon first run' do
|
20
|
+
lambda {
|
21
|
+
broken_model.attributes
|
22
|
+
}.should raise_error(RuntimeError, 'sorry :(')
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'throws InvalidDefinition for subsequent access' do
|
26
|
+
broken_model.attributes rescue nil
|
27
|
+
|
28
|
+
lambda {
|
29
|
+
broken_model.attributes
|
30
|
+
}.should raise_error(Attributor::InvalidDefinition)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'throws for any attempts at using of an instance of it' do
|
34
|
+
broken_model.attributes rescue nil
|
35
|
+
|
36
|
+
instance = broken_model.new
|
37
|
+
lambda {
|
38
|
+
instance.name
|
39
|
+
}.should raise_error(Attributor::InvalidDefinition)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
4
44
|
|
5
45
|
context 'class methods' do
|
6
|
-
subject(:chicken) { Chicken }
|
7
46
|
let(:context){ ["root","subattr"] }
|
8
47
|
|
9
48
|
its(:native_type) { should eq(Chicken) }
|
@@ -315,13 +354,12 @@ describe Attributor::Model do
|
|
315
354
|
its(:validate) { should be_empty}
|
316
355
|
end
|
317
356
|
|
318
|
-
context 'that are invalid' do
|
319
|
-
subject(:person)
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
obj
|
357
|
+
context 'that are both invalid' do
|
358
|
+
subject(:person){ Person.load( name: 'Joe', title: 'dude', okay: true )}
|
359
|
+
let(:address){ Address.load( name: '1 Main St', state: 'ME' )}
|
360
|
+
before do
|
361
|
+
person.address = address
|
362
|
+
address.person = person
|
325
363
|
end
|
326
364
|
|
327
365
|
its(:validate) { should have(2).items }
|
@@ -331,10 +369,8 @@ describe Attributor::Model do
|
|
331
369
|
title_error.should =~ /^Attribute person\.title:/
|
332
370
|
state_error.should =~ /^Attribute person\.address\.state:/
|
333
371
|
end
|
334
|
-
|
335
372
|
end
|
336
373
|
|
337
|
-
|
338
374
|
end
|
339
375
|
end
|
340
376
|
|
@@ -400,18 +436,42 @@ describe Attributor::Model do
|
|
400
436
|
end
|
401
437
|
end
|
402
438
|
|
439
|
+
context 'for collections of models' do
|
440
|
+
let(:attributes_block) do
|
441
|
+
proc do
|
442
|
+
attribute :neighbors, required: true do
|
443
|
+
attribute :name, required: true
|
444
|
+
attribute :age, Integer
|
445
|
+
end
|
446
|
+
end
|
447
|
+
end
|
448
|
+
subject(:struct) { Attributor::Struct.construct(attributes_block, reference: Cormorant) }
|
449
|
+
|
450
|
+
it 'supports defining sub-attributes using the proper reference' do
|
451
|
+
struct.attributes[:neighbors].options[:required].should be true
|
452
|
+
struct.attributes[:neighbors].type.member_attribute.type.attributes.keys.should =~ [:name, :age]
|
453
|
+
|
454
|
+
name_options = struct.attributes[:neighbors].type.member_attribute.type.attributes[:name].options
|
455
|
+
name_options[:required].should be true
|
456
|
+
name_options[:description].should eq 'Name of the Cormorant'
|
457
|
+
end
|
458
|
+
end
|
403
459
|
|
404
460
|
context 'redefining an attribute' do
|
405
|
-
|
406
|
-
|
407
|
-
|
461
|
+
context 'for simple types' do
|
462
|
+
before do
|
463
|
+
model.attributes do
|
464
|
+
attribute :id, String
|
465
|
+
end
|
466
|
+
end
|
467
|
+
|
468
|
+
it 'updates the type properly' do
|
469
|
+
model.attributes[:id].type.should be(Attributor::String)
|
408
470
|
end
|
409
|
-
end
|
410
471
|
|
411
|
-
it 'works' do
|
412
|
-
model.attributes[:id].type.should be(Attributor::String)
|
413
472
|
end
|
414
473
|
|
474
|
+
|
415
475
|
end
|
416
476
|
|
417
477
|
end
|
@@ -427,8 +487,8 @@ describe Attributor::Model do
|
|
427
487
|
subject(:example) { model_class.example }
|
428
488
|
|
429
489
|
its(:attributes) { should be_empty }
|
430
|
-
|
431
|
-
it 'dumps as an empty hash' do
|
490
|
+
|
491
|
+
it 'dumps as an empty hash' do
|
432
492
|
example.dump.should eq({})
|
433
493
|
end
|
434
494
|
|
data/spec/types/string_spec.rb
CHANGED
@@ -19,14 +19,12 @@ describe Attributor::String do
|
|
19
19
|
type.example.should be_a(::String)
|
20
20
|
end
|
21
21
|
|
22
|
-
it 'handles regexps that Randexp can not' do
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
}.to_not raise_error
|
29
|
-
end
|
22
|
+
it 'handles regexps that Randexp can not (#72)' do
|
23
|
+
regex = /\w+(,\w+)*/
|
24
|
+
expect {
|
25
|
+
val = Attributor::String.example(options:{regexp: regex})
|
26
|
+
val.should be_a(::String)
|
27
|
+
}.to_not raise_error
|
30
28
|
end
|
31
29
|
|
32
30
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attributor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: '3.0'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josep M. Blanquer
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-06-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: hashie
|
@@ -282,6 +282,7 @@ files:
|
|
282
282
|
- lib/attributor/types/symbol.rb
|
283
283
|
- lib/attributor/types/tempfile.rb
|
284
284
|
- lib/attributor/types/time.rb
|
285
|
+
- lib/attributor/types/uri.rb
|
285
286
|
- lib/attributor/version.rb
|
286
287
|
- spec/attribute_resolver_spec.rb
|
287
288
|
- spec/attribute_spec.rb
|
@@ -309,6 +310,7 @@ files:
|
|
309
310
|
- spec/types/struct_spec.rb
|
310
311
|
- spec/types/tempfile_spec.rb
|
311
312
|
- spec/types/time_spec.rb
|
313
|
+
- spec/types/uri_spec.rb
|
312
314
|
homepage: https://github.com/rightscale/attributor
|
313
315
|
licenses:
|
314
316
|
- MIT
|
@@ -329,7 +331,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
329
331
|
version: '0'
|
330
332
|
requirements: []
|
331
333
|
rubyforge_project:
|
332
|
-
rubygems_version: 2.
|
334
|
+
rubygems_version: 2.4.5
|
333
335
|
signing_key:
|
334
336
|
specification_version: 4
|
335
337
|
summary: A powerful attribute and type management library for Ruby
|
@@ -360,4 +362,5 @@ test_files:
|
|
360
362
|
- spec/types/struct_spec.rb
|
361
363
|
- spec/types/tempfile_spec.rb
|
362
364
|
- spec/types/time_spec.rb
|
365
|
+
- spec/types/uri_spec.rb
|
363
366
|
has_rdoc:
|