minimapper 0.5.2 → 0.5.3
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.
- data/README.md +18 -0
- data/lib/minimapper/mapper/ar.rb +10 -0
- data/lib/minimapper/mapper/memory.rb +8 -1
- data/lib/minimapper/version.rb +1 -1
- data/spec/support/shared_examples/mapper.rb +7 -0
- metadata +4 -4
data/README.md
CHANGED
@@ -252,6 +252,24 @@ Minimapper only calls #convert on non-empty strings. When the value is blank or
|
|
252
252
|
|
253
253
|
There is no built in support for associations yet, but you can handle them manually (see https://github.com/joakimk/minimapper/issues/3).
|
254
254
|
|
255
|
+
### Lifecycle hooks
|
256
|
+
|
257
|
+
#### after_find
|
258
|
+
|
259
|
+
This is called after any kind of find and can be used for things like loading associated data.
|
260
|
+
|
261
|
+
The parameters to the after_find hook differs between mappers, for example the ActiveRecord mapper provides a record as the second argument. In practice this should not be a problem as each mapper class in an app only implements one type of mapper.
|
262
|
+
|
263
|
+
``` ruby
|
264
|
+
class ProjectMapper < Minimapper::AR
|
265
|
+
private
|
266
|
+
|
267
|
+
def after_find(entity, record)
|
268
|
+
entity.owner = User.new(record.owner.attributes)
|
269
|
+
end
|
270
|
+
end
|
271
|
+
```
|
272
|
+
|
255
273
|
### Custom entity class
|
256
274
|
|
257
275
|
[Minimapper::Entity](https://github.com/joakimk/minimapper/blob/master/lib/minimapper/entity.rb) adds some convenience methods for when a model is used within a rails application. If you don't need that you can just include the core API from the [Minimapper::Entity::Core](https://github.com/joakimk/minimapper/blob/master/lib/minimapper/entity/core.rb) module (or implement your own version that behaves like [Minimapper::Entity::Core](https://github.com/joakimk/minimapper/blob/master/lib/minimapper/entity/core.rb)).
|
data/lib/minimapper/mapper/ar.rb
CHANGED
@@ -128,11 +128,21 @@ module Minimapper
|
|
128
128
|
entity = klass.new
|
129
129
|
entity.id = record.id
|
130
130
|
entity.attributes = record.attributes.symbolize_keys
|
131
|
+
|
132
|
+
if klass == entity_class
|
133
|
+
after_find(entity, record)
|
134
|
+
end
|
135
|
+
|
131
136
|
entity
|
132
137
|
else
|
133
138
|
nil
|
134
139
|
end
|
135
140
|
end
|
141
|
+
|
142
|
+
# Hooks
|
143
|
+
|
144
|
+
def after_find(entity, record)
|
145
|
+
end
|
136
146
|
end
|
137
147
|
end
|
138
148
|
end
|
@@ -81,7 +81,9 @@ module Minimapper
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def find_internal(id)
|
84
|
-
id && store.find { |e| e.id == id.to_i }
|
84
|
+
entity = id && store.find { |e| e.id == id.to_i }
|
85
|
+
after_find(entity)
|
86
|
+
entity
|
85
87
|
end
|
86
88
|
|
87
89
|
def next_id
|
@@ -89,6 +91,11 @@ module Minimapper
|
|
89
91
|
end
|
90
92
|
|
91
93
|
attr_reader :store, :last_id
|
94
|
+
|
95
|
+
# Hooks
|
96
|
+
|
97
|
+
def after_find(entity)
|
98
|
+
end
|
92
99
|
end
|
93
100
|
end
|
94
101
|
end
|
data/lib/minimapper/version.rb
CHANGED
@@ -63,6 +63,13 @@ shared_examples :mapper do
|
|
63
63
|
mapper.find(entity.id).object_id.should_not == mapper.find(entity.id).object_id
|
64
64
|
end
|
65
65
|
|
66
|
+
it "calls after_find on the mapper" do
|
67
|
+
entity = build_valid_entity
|
68
|
+
mapper.create(entity)
|
69
|
+
mapper.should_receive(:after_find)
|
70
|
+
found_entity = mapper.find(entity.id)
|
71
|
+
end
|
72
|
+
|
66
73
|
it "fails when an entity can not be found" do
|
67
74
|
lambda { mapper.find(-1) }.should raise_error(Minimapper::EntityNotFound)
|
68
75
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minimapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-05-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70136082132800 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70136082132800
|
25
25
|
description: A minimalistic way of separating your models from ORMs like ActiveRecord.
|
26
26
|
email:
|
27
27
|
- joakim.kolsjo@gmail.com
|