dm-maker 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -11,6 +11,9 @@ API
11
11
  when using references (see below), order of occurrence matters; instances must
12
12
  be defined before they can be referenced
13
13
 
14
+ if any errors occur, the respective instance is stored in an "_errors" member
15
+ of the returned hash (unless `raise_on_save_failure` is used)
16
+
14
17
 
15
18
  Examples
16
19
  ========
@@ -39,7 +42,7 @@ associations:
39
42
  manufacturer:
40
43
  name: Knight Industries
41
44
 
42
- (note that associated instances can include a `$class` attribute, which might be
45
+ (note that associated instances may include a `$class` attribute, which might be
43
46
  required due to Single Table Inheritance)
44
47
 
45
48
  ERB expansion:
data/README.md CHANGED
@@ -11,6 +11,9 @@ API
11
11
  when using references (see below), order of occurrence matters; instances must
12
12
  be defined before they can be referenced
13
13
 
14
+ if any errors occur, the respective instance is stored in an "_errors" member
15
+ of the returned hash (unless `raise_on_save_failure` is used)
16
+
14
17
 
15
18
  Examples
16
19
  ========
@@ -39,7 +42,7 @@ associations:
39
42
  manufacturer:
40
43
  name: Knight Industries
41
44
 
42
- (note that associated instances can include a `$class` attribute, which might be
45
+ (note that associated instances may include a `$class` attribute, which might be
43
46
  required due to Single Table Inheritance)
44
47
 
45
48
  ERB expansion:
@@ -18,14 +18,20 @@ module DataMapper
18
18
  DataMapper::Associations::OneToMany::Relationship]
19
19
  }
20
20
 
21
+ # returns a hash of instances by class
22
+ # if any errors occur, the respective instances are stored in a special
23
+ # "_errors" member
21
24
  def self.make(data)
22
25
  data = load_yaml(data) if data.class == String
23
26
 
24
27
  cache = {}
25
- return data.each_with_object({}) do |(class_name, instances), hsh|
28
+ res = { "_errors" => [] }
29
+ return data.each_with_object(res) do |(class_name, instances), hsh|
26
30
  klass = class_name.constantize
27
31
  hsh[class_name] = instances.map { |instance_data|
28
- create_instance(klass, instance_data, cache)
32
+ instance = create_instance(klass, instance_data, cache)
33
+ hsh["_errors"] << instance unless instance.save
34
+ instance
29
35
  }
30
36
  end
31
37
  end
@@ -63,11 +69,6 @@ module DataMapper
63
69
  }
64
70
 
65
71
  instance = klass.new(data)
66
- begin
67
- instance.save
68
- rescue DataMapper::SaveFailureError
69
- # rely on parent saving -- FIXME: hacky!?
70
- end
71
72
 
72
73
  cache[id] = instance if id
73
74
  return instance
@@ -2,6 +2,6 @@
2
2
 
3
3
  module DataMapper
4
4
  module Maker
5
- VERSION = "1.1.1"
5
+ VERSION = "1.2.0"
6
6
  end
7
7
  end
@@ -4,7 +4,7 @@ class Person
4
4
  include DataMapper::Resource
5
5
 
6
6
  property :id, Serial
7
- property :name, String
7
+ property :name, String, :required => true
8
8
  property :age, Integer
9
9
 
10
10
  belongs_to :family, :required => false
@@ -63,7 +63,8 @@ class BasicsTest < Test::Unit::TestCase
63
63
  EOF
64
64
  data = DataMapper::Maker.make(yaml)
65
65
 
66
- assert_equal 2, data.length
66
+ assert_equal 3, data.length # includes "_errors"
67
+ assert_equal 0, data["_errors"].length
67
68
  assert_equal 2, data["Person"].length
68
69
  assert_equal "John", data["Person"][0].name
69
70
  assert_equal 11, data["Person"][0].age
@@ -0,0 +1,53 @@
1
+ # encoding: UTF-8
2
+
3
+ require "helper"
4
+
5
+ class ErrorsTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ reset_database
9
+ @raise_setting = DataMapper::Model.raise_on_save_failure
10
+
11
+ @yaml = <<-EOF
12
+ Person:
13
+ -
14
+ name: John Doe
15
+ age: 11
16
+ -
17
+ age: 17
18
+ -
19
+ name: Jane Doe
20
+ age: 13
21
+ EOF
22
+ end
23
+
24
+ def teardown
25
+ DataMapper::Model.raise_on_save_failure = @raise_setting
26
+ end
27
+
28
+ def test_reports_silent_errors
29
+ DataMapper::Model.raise_on_save_failure = false
30
+
31
+ data = DataMapper::Maker.make(@yaml)
32
+
33
+ assert_equal 2, Person.count
34
+ assert_equal "John Doe", Person.first.name
35
+ assert_equal "Jane Doe", Person.last.name
36
+
37
+ assert_equal 1, data["_errors"].length
38
+ assert_equal "Person", data["_errors"].first.class.name
39
+ assert (not data["_errors"].first.saved?)
40
+ end
41
+
42
+ def test_does_not_swallow_exceptions
43
+ DataMapper::Model.raise_on_save_failure = true
44
+
45
+ assert_raise DataMapper::SaveFailureError do
46
+ data = DataMapper::Maker.make(@yaml)
47
+ end
48
+ assert_equal 1, Person.count
49
+ assert_equal "John Doe", Person.first.name
50
+ # Jane Doe was never processed
51
+ end
52
+
53
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-maker
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 1
9
- - 1
10
- version: 1.1.1
8
+ - 2
9
+ - 0
10
+ version: 1.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - FND
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-16 00:00:00 Z
18
+ date: 2011-12-07 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: dm-core
@@ -158,6 +158,7 @@ files:
158
158
  - test/models.rb
159
159
  - test/test_associations.rb
160
160
  - test/test_basics.rb
161
+ - test/test_errors.rb
161
162
  - test/test_references.rb
162
163
  homepage:
163
164
  licenses: []
@@ -195,4 +196,5 @@ summary: DataMapper extension to generate instances from YAML
195
196
  test_files:
196
197
  - test/test_associations.rb
197
198
  - test/test_basics.rb
199
+ - test/test_errors.rb
198
200
  - test/test_references.rb