atdis 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/atdis/person.rb CHANGED
@@ -1,10 +1,12 @@
1
1
  module ATDIS
2
2
  class Person < Model
3
- field_mappings :name => [:name, String],
4
- :role => [:role, String],
5
- :contact => [:contact, String]
3
+ set_field_mappings [
4
+ [:name, [:name, String, {:level => 2}]],
5
+ [:role, [:role, String, {:level => 2}]],
6
+ [:contact, [:contact, String, {:level => 2}]]
7
+ ]
6
8
 
7
9
  # Mandatory parameters
8
- validates :name, :role, :presence_before_type_cast => true
10
+ validates :name, :role, :presence_before_type_cast => {:spec_section => "4.3.6"}
9
11
  end
10
12
  end
@@ -6,7 +6,9 @@ module ATDIS
6
6
  def validate_each(record, attribute, value)
7
7
  raw_value = record.send("#{attribute}_before_type_cast")
8
8
  if raw_value.present? && value.nil?
9
- record.errors.add(attribute, "is not valid GeoJSON")
9
+ message = "is not valid GeoJSON"
10
+ message = ErrorMessage[message, options[:spec_section]] if options[:spec_section]
11
+ record.errors.add(attribute, message)
10
12
  end
11
13
  end
12
14
  end
@@ -15,7 +17,18 @@ module ATDIS
15
17
  def validate_each(record, attribute, value)
16
18
  raw_value = record.send("#{attribute}_before_type_cast")
17
19
  if raw_value.present? && !value.kind_of?(DateTime)
18
- record.errors.add(attribute, "is not a valid date")
20
+ record.errors.add(attribute, ErrorMessage["is not a valid date", options[:spec_section]])
21
+ end
22
+ end
23
+ end
24
+
25
+ class DateTimeOrNoneValidator < ActiveModel::EachValidator
26
+ def validate_each(record, attribute, value)
27
+ raw_value = record.send("#{attribute}_before_type_cast")
28
+ if raw_value.present? && raw_value != "none" && !value.kind_of?(DateTime)
29
+ message = "is not a valid date or none"
30
+ message = ErrorMessage[message, options[:spec_section]] if options[:spec_section]
31
+ record.errors.add(attribute, message)
19
32
  end
20
33
  end
21
34
  end
@@ -24,7 +37,19 @@ module ATDIS
24
37
  def validate_each(record, attribute, value)
25
38
  raw_value = record.send("#{attribute}_before_type_cast")
26
39
  if raw_value.present? && !value.kind_of?(URI::HTTP) && !value.kind_of?(URI::HTTPS)
27
- record.errors.add(attribute, "is not a valid URL")
40
+ message = "is not a valid URL"
41
+ message = ErrorMessage[message, options[:spec_section]] if options[:spec_section]
42
+ record.errors.add(attribute, message)
43
+ end
44
+ end
45
+ end
46
+
47
+ class ArrayValidator < ActiveModel::EachValidator
48
+ def validate_each(record, attribute, value)
49
+ if value && !value.kind_of?(Array)
50
+ message = "should be an array"
51
+ message = ErrorMessage[message, options[:spec_section]] if options[:spec_section]
52
+ record.errors.add(attribute, message)
28
53
  end
29
54
  end
30
55
  end
@@ -33,8 +58,10 @@ module ATDIS
33
58
  class PresenceBeforeTypeCastValidator < ActiveModel::EachValidator
34
59
  def validate_each(record, attribute, value)
35
60
  raw_value = record.send("#{attribute}_before_type_cast")
36
- unless raw_value.present?
37
- record.errors.add(attribute, "can't be blank")
61
+ if !raw_value.kind_of?(Array) && !raw_value.present?
62
+ message = "can't be blank"
63
+ message = ErrorMessage[message, options[:spec_section]] if options[:spec_section]
64
+ record.errors.add(attribute, message)
38
65
  end
39
66
  end
40
67
  end
@@ -43,7 +70,7 @@ module ATDIS
43
70
  class ValidValidator < ActiveModel::EachValidator
44
71
  def validate_each(record, attribute, value)
45
72
  if (value.respond_to?(:valid?) && !value.valid?) || (value && !value.respond_to?(:valid?) && !value.all?{|v| v.valid?})
46
- record.errors.add(attribute, "is not valid")
73
+ record.errors.add(attribute, ErrorMessage["is not valid", nil])
47
74
  end
48
75
  end
49
76
  end
data/lib/atdis/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Atdis
2
- VERSION = "0.1"
2
+ VERSION = "0.2"
3
3
  end
@@ -2,6 +2,18 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe ATDIS::Application do
4
4
 
5
+ describe ".level_used?" do
6
+ it "level 3" do
7
+ a = ATDIS::Application.new(:extended => {:some_info => "here"})
8
+ a.level_used?(3).should be_true
9
+ end
10
+
11
+ it "level 2" do
12
+ a = ATDIS::Application.new(:people => [])
13
+ a.level_used?(2).should be_true
14
+ end
15
+ end
16
+
5
17
  context "extra parameter in json" do
6
18
  it "should not be valid" do
7
19
  ATDIS::Location.should_receive(:interpret).with(:foo => "Some location data").and_return(double(:valid? => true))
@@ -20,10 +32,12 @@ describe ATDIS::Application do
20
32
  :foo => "bar",
21
33
  :more_info_url => "http://foo.com/bar"
22
34
  },
23
- :location => {:foo => "Some location data"}
35
+ :location => {:foo => "Some location data"},
36
+ :events => [],
37
+ :documents => []
24
38
  })
25
39
  a.should_not be_valid
26
- a.errors.messages.should == {:json => ['Unexpected parameters in json data: {"application":{"reference":{"foo":"bar"}}}']}
40
+ a.errors.messages.should == {:json => [ATDIS::ErrorMessage['Unexpected parameters in json data: {"application":{"reference":{"foo":"bar"}}}', "4"]]}
27
41
  end
28
42
  end
29
43
 
@@ -84,7 +98,10 @@ describe ATDIS::Application do
84
98
 
85
99
  it "should create a nil valued application when there is no information in the json" do
86
100
  application = double
87
- ATDIS::Application.should_receive(:new).with({:json_left_overs => {}}).and_return(application)
101
+ ATDIS::Application.should_receive(:new).with({:json_left_overs => {}, :status=>nil, :determination_date=>nil, :estimated_cost=>nil,
102
+ :comments_url=>nil, :description=>nil, :more_info_url=>nil, :dat_id=>nil, :notification_start_date=>nil, :location=>nil,
103
+ :extended=>nil, :events=>nil, :last_modified_date=>nil, :notification_end_date=>nil, :documents=>nil, :authority=>nil,
104
+ :lodgement_date=>nil, :officer=>nil, :people=>nil}).and_return(application)
88
105
 
89
106
  ATDIS::Application.interpret(:application => {:info => {}, :reference => {}}).should == application
90
107
  end
@@ -251,10 +268,58 @@ describe ATDIS::Application do
251
268
  # TODO This should really be a test on the Model base class
252
269
  describe "#attribute_names" do
253
270
  it do
254
- ATDIS::Application.attribute_names.sort.should == ["authority", "comments_url", "dat_id", "description",
255
- "determination_date", "documents", "estimated_cost", "events", "extended", "last_modified_date",
256
- "location", "lodgement_date", "more_info_url", "notification_end_date", "notification_start_date", "officer",
257
- "people", "status"]
271
+ # These are also ordered in a way that corresponds to the specification. Makes for easy reading by humans.
272
+ ATDIS::Application.attribute_names.should == [
273
+ "dat_id",
274
+ "last_modified_date",
275
+ "description",
276
+ "authority",
277
+ "lodgement_date",
278
+ "determination_date",
279
+ "status",
280
+ "notification_start_date",
281
+ "notification_end_date",
282
+ "officer",
283
+ "estimated_cost",
284
+ "more_info_url",
285
+ "comments_url",
286
+ "location",
287
+ "events",
288
+ "documents",
289
+ "people",
290
+ "extended"
291
+ ]
292
+ end
293
+ end
294
+
295
+ describe ".level_attribute_names" do
296
+ it "L1" do
297
+ ATDIS::Application.level_attribute_names(1).should == [
298
+ "dat_id",
299
+ "last_modified_date",
300
+ "description",
301
+ "authority",
302
+ "lodgement_date",
303
+ "determination_date",
304
+ "status",
305
+ "notification_start_date",
306
+ "notification_end_date",
307
+ "officer",
308
+ "estimated_cost",
309
+ "more_info_url",
310
+ "comments_url",
311
+ "location",
312
+ "events",
313
+ "documents"
314
+ ]
315
+ end
316
+
317
+ it "L2" do
318
+ ATDIS::Application.level_attribute_names(2).should == ["people"]
319
+ end
320
+
321
+ it "L3" do
322
+ ATDIS::Application.level_attribute_names(3).should == ["extended"]
258
323
  end
259
324
  end
260
325
 
@@ -273,7 +338,9 @@ describe ATDIS::Application do
273
338
  :determination_date => DateTime.new(2013,6,20),
274
339
  :status => "OPEN",
275
340
  :more_info_url => URI.parse("http://foo.com/bar"),
276
- :location => {:address => "123 Fourfivesix Street Neutral Bay NSW 2089"}
341
+ :location => {:address => "123 Fourfivesix Street Neutral Bay NSW 2089"},
342
+ :events => [],
343
+ :documents => []
277
344
  ) }
278
345
 
279
346
  it { a.should be_valid }
@@ -290,19 +357,19 @@ describe ATDIS::Application do
290
357
  it ".dat_id" do
291
358
  a.dat_id = nil
292
359
  a.should_not be_valid
293
- a.errors.messages.should == {:dat_id => ["can't be blank"]}
360
+ a.errors.messages.should == {:dat_id => [ATDIS::ErrorMessage["can't be blank", "4.3.1"]]}
294
361
  end
295
362
 
296
363
  describe ".last_modified_date" do
297
364
  it do
298
365
  a.last_modified_date = nil
299
366
  a.should_not be_valid
300
- a.errors.messages.should == {:last_modified_date => ["can't be blank"]}
367
+ a.errors.messages.should == {:last_modified_date => [ATDIS::ErrorMessage["can't be blank", "4.3.1"]]}
301
368
  end
302
369
  it do
303
370
  a.last_modified_date = "18 January 2013"
304
371
  a.should_not be_valid
305
- a.errors.messages.should == {:last_modified_date => ["is not a valid date"]}
372
+ a.errors.messages.should == {:last_modified_date => [ATDIS::ErrorMessage["is not a valid date", "4.3.8"]]}
306
373
  end
307
374
  end
308
375
 
@@ -310,7 +377,7 @@ describe ATDIS::Application do
310
377
  it do
311
378
  a.description = ""
312
379
  a.should_not be_valid
313
- a.errors.messages.should == {:description => ["can't be blank"]}
380
+ a.errors.messages.should == {:description => [ATDIS::ErrorMessage["can't be blank", "4.3.1"]]}
314
381
  end
315
382
  end
316
383
 
@@ -318,7 +385,7 @@ describe ATDIS::Application do
318
385
  it do
319
386
  a.authority = nil
320
387
  a.should_not be_valid
321
- a.errors.messages.should == {:authority => ["can't be blank"]}
388
+ a.errors.messages.should == {:authority => [ATDIS::ErrorMessage["can't be blank", "4.3.1"]]}
322
389
  end
323
390
  end
324
391
 
@@ -326,12 +393,12 @@ describe ATDIS::Application do
326
393
  it do
327
394
  a.lodgement_date = nil
328
395
  a.should_not be_valid
329
- a.errors.messages.should == {:lodgement_date => ["can't be blank"]}
396
+ a.errors.messages.should == {:lodgement_date => [ATDIS::ErrorMessage["can't be blank", "4.3.1"]]}
330
397
  end
331
398
  it do
332
399
  a.lodgement_date = "18 January 2013"
333
400
  a.should_not be_valid
334
- a.errors.messages.should == {:lodgement_date => ["is not a valid date"]}
401
+ a.errors.messages.should == {:lodgement_date => [ATDIS::ErrorMessage["is not a valid date", "4.3.8"]]}
335
402
  end
336
403
  end
337
404
 
@@ -339,12 +406,17 @@ describe ATDIS::Application do
339
406
  it do
340
407
  a.determination_date = nil
341
408
  a.should_not be_valid
342
- a.errors.messages.should == {:determination_date => ["can't be blank"]}
409
+ a.errors.messages.should == {:determination_date => [ATDIS::ErrorMessage["can't be blank", "4.3.1"]]}
343
410
  end
344
411
  it do
345
412
  a.determination_date = "18 January 2013"
346
413
  a.should_not be_valid
347
- a.errors.messages.should == {:determination_date => ["is not a valid date"]}
414
+ a.errors.messages.should == {:determination_date => [ATDIS::ErrorMessage["is not a valid date or none", "4.3.1"]]}
415
+ end
416
+ it "none should be allowed if the application is not yet determined" do
417
+ a.determination_date = "none"
418
+ a.determination_date.should be_nil
419
+ a.should be_valid
348
420
  end
349
421
  end
350
422
 
@@ -352,31 +424,70 @@ describe ATDIS::Application do
352
424
  it do
353
425
  a.status = nil
354
426
  a.should_not be_valid
355
- a.errors.messages.should == {:status => ["can't be blank"]}
427
+ a.errors.messages.should == {:status => [ATDIS::ErrorMessage["can't be blank", "4.3.1"]]}
356
428
  end
357
429
  end
358
430
 
359
- describe ".notification_start_date" do
360
- it do
431
+ describe "notification_date" do
432
+ it "both valid start and end dates" do
361
433
  a.notification_start_date = DateTime.new(2013,4,20,2,1,7)
434
+ a.notification_end_date = DateTime.new(2013,5,20,0,0,0)
362
435
  a.should be_valid
363
436
  end
364
- it do
437
+
438
+ it "invalid start date" do
365
439
  a.notification_start_date = "18 January 2013"
440
+ a.notification_end_date = DateTime.new(2013,2,1,0,0,0)
366
441
  a.should_not be_valid
367
- a.errors.messages.should == {:notification_start_date => ["is not a valid date"]}
442
+ a.errors.messages.should == {:notification_start_date => [ATDIS::ErrorMessage["is not a valid date or none", "4.3.1"]]}
368
443
  end
369
- end
370
444
 
371
- describe ".notification_end_date" do
372
- it do
373
- a.notification_end_date = DateTime.new(2013,5,20,2,1,7)
445
+ it "invalid end date" do
446
+ a.notification_start_date = DateTime.new(2013,1,10,0,0,0)
447
+ a.notification_end_date = "18 January 2013"
448
+ a.should_not be_valid
449
+ a.errors.messages.should == {:notification_end_date => [ATDIS::ErrorMessage["is not a valid date or none", "4.3.1"]]}
450
+ end
451
+
452
+ it "only start date set" do
453
+ a.notification_start_date = DateTime.new(2013,4,20,2,1,7)
454
+ a.should_not be_valid
455
+ a.errors.messages.should == {:notification_end_date => [ATDIS::ErrorMessage["can not be blank if notification_start_date is set", "4.3.1"]]}
456
+ end
457
+
458
+ it "only end date set" do
459
+ a.notification_end_date = DateTime.new(2013,4,20,2,1,7)
460
+ a.should_not be_valid
461
+ a.errors.messages.should == {:notification_start_date => [ATDIS::ErrorMessage["can not be blank if notification_end_date is set", "4.3.1"]]}
462
+ end
463
+
464
+ it "end date is before start date" do
465
+ a.notification_start_date = DateTime.new(2013,5,20,0,0,0)
466
+ a.notification_end_date = DateTime.new(2013,4,20,2,1,7)
467
+ a.should_not be_valid
468
+ a.errors.messages.should == {:notification_end_date => [ATDIS::ErrorMessage["can not be earlier than notification_start_date", "4.3.1"]]}
469
+ end
470
+
471
+ it "both dates set to none" do
472
+ a.notification_start_date = "none"
473
+ a.notification_end_date = "none"
474
+ a.notification_start_date.should be_nil
475
+ a.notification_end_date.should be_nil
374
476
  a.should be_valid
375
477
  end
376
- it do
377
- a.notification_end_date = "18 January 2013"
478
+
479
+ it "only start date set to none" do
480
+ a.notification_start_date = "none"
481
+ a.notification_end_date = DateTime.new(2013,2,1,0,0,0)
482
+ a.should_not be_valid
483
+ a.errors.messages.should == {:notification_start_date => [ATDIS::ErrorMessage["can't be none unless notification_end_date is none as well", "4.3.1"]]}
484
+ end
485
+
486
+ it "only end date set to none" do
487
+ a.notification_start_date = DateTime.new(2013,2,1,0,0,0)
488
+ a.notification_end_date = "none"
378
489
  a.should_not be_valid
379
- a.errors.messages.should == {:notification_end_date => ["is not a valid date"]}
490
+ a.errors.messages.should == {:notification_end_date => [ATDIS::ErrorMessage["can't be none unless notification_start_date is none as well", "4.3.1"]]}
380
491
  end
381
492
  end
382
493
 
@@ -384,22 +495,44 @@ describe ATDIS::Application do
384
495
  it do
385
496
  a.more_info_url = nil
386
497
  a.should_not be_valid
387
- a.errors.messages.should == {:more_info_url => ["can't be blank"]}
498
+ a.errors.messages.should == {:more_info_url => [ATDIS::ErrorMessage["can't be blank", "4.3.2"]]}
388
499
  end
389
500
  it do
390
501
  a.more_info_url = "This is not a valid url"
391
502
  a.should_not be_valid
392
- a.errors.messages.should == {:more_info_url => ["is not a valid URL"]}
503
+ a.errors.messages.should == {:more_info_url => [ATDIS::ErrorMessage["is not a valid URL", "4.3.2"]]}
393
504
  end
394
505
  it do
395
506
  a.more_info_url = "foo.com"
396
507
  a.should_not be_valid
397
- a.errors.messages.should == {:more_info_url => ["is not a valid URL"]}
508
+ a.errors.messages.should == {:more_info_url => [ATDIS::ErrorMessage["is not a valid URL", "4.3.2"]]}
398
509
  end
399
510
  it do
400
511
  a.more_info_url = "httpss://foo.com"
401
512
  a.should_not be_valid
402
- a.errors.messages.should == {:more_info_url => ["is not a valid URL"]}
513
+ a.errors.messages.should == {:more_info_url => [ATDIS::ErrorMessage["is not a valid URL", "4.3.2"]]}
514
+ end
515
+ end
516
+
517
+ describe "events" do
518
+ it "has to be an array" do
519
+ ATDIS::Event.should_receive(:interpret).with(:foo => "bar").and_return(double(:valid? => true))
520
+ a.events = {:foo => "bar"}
521
+ #a.events.should be_nil
522
+ a.should_not be_valid
523
+ a.errors.messages.should == {:events => [ATDIS::ErrorMessage["should be an array", "4.3.4"]]}
524
+ end
525
+
526
+ it "can be an empty array" do
527
+ a.events = []
528
+ a.events.should == []
529
+ a.should be_valid
530
+ end
531
+
532
+ it "can not be empty" do
533
+ a.events = nil
534
+ a.should_not be_valid
535
+ a.errors.messages.should == {:events => [ATDIS::ErrorMessage["can't be blank", "4.3.4"]]}
403
536
  end
404
537
  end
405
538
  end
@@ -1,6 +1,10 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe ATDIS::Document do
4
+ it ".attribute_names" do
5
+ ATDIS::Document.attribute_names.should == ["ref", "title", "document_url"]
6
+ end
7
+
4
8
  it ".ref" do
5
9
  ATDIS::Document.interpret(:ref => "27B/6").ref.should == "27B/6"
6
10
  end
@@ -1,6 +1,10 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe ATDIS::Event do
4
+ it ".attribute_names" do
5
+ ATDIS::Event.attribute_names.should == ["id", "date", "description", "event_type", "status"]
6
+ end
7
+
4
8
  it ".id" do
5
9
  ATDIS::Event.interpret(:id => "27B/6").id.should == "27B/6"
6
10
  end
@@ -1,6 +1,16 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe ATDIS::Location do
4
+ it ".attribute_names" do
5
+ ATDIS::Location.attribute_names.should == [
6
+ "address",
7
+ "lot",
8
+ "section",
9
+ "dpsp_id",
10
+ "geometry"
11
+ ]
12
+ end
13
+
4
14
  describe "validation" do
5
15
  context "valid location" do
6
16
  let(:l) { ATDIS::Location.new(
@@ -19,7 +29,7 @@ describe ATDIS::Location do
19
29
  it "address" do
20
30
  l.address = ""
21
31
  l.should_not be_valid
22
- l.errors.messages.should == {:address => ["can't be blank"]}
32
+ l.errors.messages.should == {:address => [ATDIS::ErrorMessage["can't be blank", "4.3.3"]]}
23
33
  end
24
34
 
25
35
  it "geometry" do
@@ -27,6 +37,48 @@ describe ATDIS::Location do
27
37
  l.geometry.should be_nil
28
38
  l.should_not be_valid
29
39
  end
40
+
41
+ describe "lot" do
42
+ it "can not be blank" do
43
+ l.lot = ""
44
+ l.should_not be_valid
45
+ l.errors.messages.should == {:lot => [ATDIS::ErrorMessage["can't be blank", "4.3.3"]]}
46
+ end
47
+
48
+ it "can be none but is not interpreted in any special way" do
49
+ l.lot = "none"
50
+ l.lot.should == "none"
51
+ l.should be_valid
52
+ end
53
+ end
54
+
55
+ describe "section" do
56
+ it "can not be blank" do
57
+ l.section = ""
58
+ l.should_not be_valid
59
+ l.errors.messages.should == {:section => [ATDIS::ErrorMessage["can't be blank", "4.3.3"]]}
60
+ end
61
+
62
+ it "can be none" do
63
+ l.section = "none"
64
+ l.section.should be_nil
65
+ l.should be_valid
66
+ end
67
+ end
68
+
69
+ describe "dpsp_id" do
70
+ it "can not be blank" do
71
+ l.dpsp_id = ""
72
+ l.should_not be_valid
73
+ l.errors.messages.should == {:dpsp_id => [ATDIS::ErrorMessage["can't be blank", "4.3.3"]]}
74
+ end
75
+
76
+ it "can be none but is not interpreted in any special way" do
77
+ l.dpsp_id = "none"
78
+ l.dpsp_id.should == "none"
79
+ l.should be_valid
80
+ end
81
+ end
30
82
  end
31
83
  end
32
84
 
@@ -48,11 +100,10 @@ describe ATDIS::Location do
48
100
 
49
101
  it "should gracefully handle the land_title_ref block being missing" do
50
102
  l = ATDIS::Location.interpret(:address => "123 Fourfivesix Street Neutral Bay NSW 2089")
51
-
52
103
  l.address.should == "123 Fourfivesix Street Neutral Bay NSW 2089"
53
- l.lot.should be_nil
54
- l.section.should be_nil
55
- l.dpsp_id.should be_nil
104
+ l.lot.should == ""
105
+ l.section.should == ""
106
+ l.dpsp_id.should == ""
56
107
  end
57
108
 
58
109
  it "should pass on the responsibility for parsing the geometry section" do