minimapper 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -63,17 +63,20 @@ mapper.create(user)
63
63
 
64
64
  # Finding
65
65
  user = mapper.find(user.id)
66
- puts user.name # -> Joe
67
- puts mapper.first.name # -> Joe
66
+ puts user.name # -> Joe
67
+ puts mapper.first.name # -> Joe
68
68
 
69
69
  # Updating
70
70
  user.name = "Joey"
71
71
  mapper.update(user)
72
- puts mapper.first.name # -> Joey
72
+ puts mapper.first.name # -> Joey
73
73
 
74
74
  # Deleting
75
+ old_id = user.id
75
76
  mapper.delete(user)
76
- mapper.find(user) # raises Minimapper::Common::CanNotFindEntity
77
+ user.id # -> nil
78
+ # mapper.find(old_id) # raises Minimapper::Common::CanNotFindEntity
79
+ mapper.find_by_id(old_id) # -> nil
77
80
 
78
81
  # Deleting all
79
82
  mapper.delete_all
@@ -85,11 +88,13 @@ Or though a repository:
85
88
  require "minimapper/repository"
86
89
 
87
90
  repository = Minimapper::Repository.build({
88
- :users => UserMapper.new,
89
- :projects => ProjectMapper.new
91
+ :users => UserMapper.new
92
+ # :projects => ProjectMapper.new
90
93
  })
91
94
 
92
- repository.users.find(1)
95
+ user = User.new(:name => "Joe")
96
+ repository.users.create(user)
97
+ puts repository.users.find(user.id).name # -> Joe
93
98
  ```
94
99
 
95
100
  ## Using the ActiveRecord mapper
@@ -111,6 +116,18 @@ mapper = AR::UserMapper.new
111
116
  mapper.create(user)
112
117
  ```
113
118
 
119
+ ## Using ActiveModel validation
120
+
121
+ ``` ruby
122
+ user = User.new
123
+
124
+ mapper = UserMapper.new
125
+ mapper.create(user)
126
+
127
+ mapper.count # -> 0
128
+ user.errors.full_messages # Name can't be blank
129
+ ```
130
+
114
131
  ## Implementing custom queries
115
132
 
116
133
  *todo* show how, talk about shared examples.
data/lib/minimapper/ar.rb CHANGED
@@ -13,6 +13,10 @@ module Minimapper
13
13
 
14
14
  # Read
15
15
  def find(id)
16
+ entity_for(find_record_safely(id))
17
+ end
18
+
19
+ def find_by_id(id)
16
20
  entity_for(find_record(id))
17
21
  end
18
22
 
@@ -45,10 +49,11 @@ module Minimapper
45
49
  # Delete
46
50
  def delete(entity)
47
51
  delete_by_id(entity.id)
52
+ entity.id = nil
48
53
  end
49
54
 
50
55
  def delete_by_id(id)
51
- find_record(id).delete
56
+ find_record_safely(id).delete
52
57
  end
53
58
 
54
59
  def delete_all
@@ -74,11 +79,15 @@ module Minimapper
74
79
  @entity_klass
75
80
  end
76
81
 
77
- def find_record(id)
78
- (id && record_klass.find_by_id(id)) ||
82
+ def find_record_safely(id)
83
+ find_record(id) ||
79
84
  raise(Common::CanNotFindEntity, :id => id)
80
85
  end
81
86
 
87
+ def find_record(id)
88
+ id && record_klass.find_by_id(id)
89
+ end
90
+
82
91
  def record_for(entity)
83
92
  (entity.id && record_klass.find_by_id(entity.id)) ||
84
93
  raise(Common::CanNotFindEntity, entity.inspect)
@@ -20,7 +20,12 @@ module Minimapper
20
20
 
21
21
  # Read
22
22
  def find(id)
23
- find_internal(id).dup
23
+ find_internal_safely(id).dup
24
+ end
25
+
26
+ def find_by_id(id)
27
+ entity = find_internal(id)
28
+ entity && entity.dup
24
29
  end
25
30
 
26
31
  def all
@@ -42,7 +47,7 @@ module Minimapper
42
47
  # Update
43
48
  def update(entity)
44
49
  if entity.valid?
45
- known_entity = find_internal(entity.id)
50
+ known_entity = find_internal_safely(entity.id)
46
51
  known_entity.attributes = entity.attributes
47
52
  true
48
53
  else
@@ -53,10 +58,11 @@ module Minimapper
53
58
  # Delete
54
59
  def delete(entity)
55
60
  delete_by_id(entity.id)
61
+ entity.id = nil
56
62
  end
57
63
 
58
64
  def delete_by_id(id)
59
- entity = find_internal(id)
65
+ entity = find_internal_safely(id)
60
66
  store.delete(entity)
61
67
  end
62
68
 
@@ -66,11 +72,15 @@ module Minimapper
66
72
 
67
73
  private
68
74
 
69
- def find_internal(id)
70
- (id && store.find { |e| e.id == id.to_i }) ||
75
+ def find_internal_safely(id)
76
+ find_internal(id) ||
71
77
  raise(Common::CanNotFindEntity, :id => id)
72
78
  end
73
79
 
80
+ def find_internal(id)
81
+ id && store.find { |e| e.id == id.to_i }
82
+ end
83
+
74
84
  def next_id
75
85
  @last_id += 1
76
86
  end
@@ -1,3 +1,3 @@
1
1
  module Minimapper
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -60,6 +60,34 @@ shared_examples :mapper do
60
60
  end
61
61
  end
62
62
 
63
+ describe "find_by_id" do
64
+ it "returns an entity matching the id" do
65
+ entity = build_valid_entity
66
+ repository.create(entity)
67
+ found_entity = repository.find_by_id(entity.id)
68
+ found_entity.name.should == "test"
69
+ found_entity.id.should == entity.id
70
+ found_entity.should be_kind_of(Minimapper::Entity)
71
+ end
72
+
73
+ it "supports string ids" do
74
+ entity = build_valid_entity
75
+ repository.create(entity)
76
+ repository.find_by_id(entity.id.to_s)
77
+ end
78
+
79
+ it "does not return the same instance" do
80
+ entity = build_valid_entity
81
+ repository.create(entity)
82
+ repository.find_by_id(entity.id).object_id.should_not == entity.object_id
83
+ repository.find_by_id(entity.id).object_id.should_not == repository.find_by_id(entity.id).object_id
84
+ end
85
+
86
+ it "returns nil when the an entity can not be found" do
87
+ repository.find_by_id(-1).should be_nil
88
+ end
89
+ end
90
+
63
91
  describe "all" do
64
92
  it "returns all entities in undefined order" do
65
93
  first_created_entity = build_valid_entity
@@ -174,11 +202,20 @@ shared_examples :mapper do
174
202
  describe "delete" do
175
203
  it "removes the entity" do
176
204
  entity = build_valid_entity
205
+ removed_entity_id = entity.id
177
206
  repository.create(entity)
178
207
  repository.create(build_valid_entity)
179
208
  repository.delete(entity)
180
209
  repository.all.size.should == 1
181
- repository.first.id.should_not == entity.id
210
+ repository.first.id.should_not == removed_entity_id
211
+ end
212
+
213
+ it "clears the entity id" do
214
+ entity = build_valid_entity
215
+ repository.create(entity)
216
+ entity.id.should_not be_nil
217
+ repository.delete(entity)
218
+ entity.id.should be_nil
182
219
  end
183
220
 
184
221
  it "fails when the entity does not have an id" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minimapper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Joakim Kolsj\xC3\xB6"