minimapper 0.10.0 → 0.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -1
- data/README.md +6 -2
- data/lib/minimapper/entity_invalid.rb +14 -0
- data/lib/minimapper/mapper.rb +39 -20
- data/lib/minimapper/version.rb +1 -1
- data/spec/mapper_spec.rb +35 -0
- metadata +8 -7
data/.rvmrc
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm ruby-1.9.3-
|
1
|
+
rvm ruby-1.9.3-p194-falcon@minimapper --create
|
data/README.md
CHANGED
@@ -150,9 +150,9 @@ So an entity that wouldn't be unique in the database will be `valid?` before you
|
|
150
150
|
You can write custom queries like this:
|
151
151
|
|
152
152
|
``` ruby
|
153
|
-
class ProjectMapper < Minimapper::
|
153
|
+
class ProjectMapper < Minimapper::Mapper
|
154
154
|
def waiting_for_review
|
155
|
-
entities_for
|
155
|
+
entities_for query_scope.where(waiting_for_review: true).order("id DESC")
|
156
156
|
end
|
157
157
|
end
|
158
158
|
```
|
@@ -166,6 +166,10 @@ Repository.projects.waiting_for_review.each do |project|
|
|
166
166
|
end
|
167
167
|
```
|
168
168
|
|
169
|
+
`record_class` is shorthand for ProjectMapper::Record.
|
170
|
+
|
171
|
+
`query_scope` is a wrapper around `record_class` to allow you to add custom scopes to all queries.
|
172
|
+
|
169
173
|
`entity_for` returns nil for nil.
|
170
174
|
|
171
175
|
`entity_for` and `entities_for` take an optional second argument if you want a different entity class than the mapper's:
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Minimapper
|
2
|
+
class EntityInvalid < StandardError
|
3
|
+
def initialize(entity)
|
4
|
+
@entity = entity
|
5
|
+
end
|
6
|
+
|
7
|
+
def message
|
8
|
+
# As long as the mappers support Minimapper::Entity::Core, we can't expect .errors to exist. I'm thinking of removing core as I don't use it myself and don't know anyone who does.
|
9
|
+
return super unless @entity.respond_to?(:errors)
|
10
|
+
|
11
|
+
@entity.errors.full_messages.join(', ')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/minimapper/mapper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require "minimapper/entity_invalid"
|
2
|
+
|
1
3
|
module Minimapper
|
2
4
|
class Mapper
|
3
5
|
attr_accessor :repository
|
@@ -22,6 +24,10 @@ module Minimapper
|
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
27
|
+
def create!(entity)
|
28
|
+
create(entity) || raise(Minimapper::EntityInvalid.new(entity))
|
29
|
+
end
|
30
|
+
|
25
31
|
# Read
|
26
32
|
|
27
33
|
def find(id)
|
@@ -33,15 +39,15 @@ module Minimapper
|
|
33
39
|
end
|
34
40
|
|
35
41
|
def all
|
36
|
-
entities_for
|
42
|
+
entities_for query_scope.all
|
37
43
|
end
|
38
44
|
|
39
45
|
def first
|
40
|
-
entity_for(
|
46
|
+
entity_for(query_scope.order("id ASC").first)
|
41
47
|
end
|
42
48
|
|
43
49
|
def last
|
44
|
-
entity_for(
|
50
|
+
entity_for(query_scope.order("id ASC").last)
|
45
51
|
end
|
46
52
|
|
47
53
|
def reload(entity)
|
@@ -49,7 +55,7 @@ module Minimapper
|
|
49
55
|
end
|
50
56
|
|
51
57
|
def count
|
52
|
-
|
58
|
+
query_scope.count
|
53
59
|
end
|
54
60
|
|
55
61
|
# Update
|
@@ -70,6 +76,10 @@ module Minimapper
|
|
70
76
|
end
|
71
77
|
end
|
72
78
|
|
79
|
+
def update!(entity)
|
80
|
+
update(entity) || raise(Minimapper::EntityInvalid.new(entity))
|
81
|
+
end
|
82
|
+
|
73
83
|
# Delete
|
74
84
|
|
75
85
|
def delete(entity)
|
@@ -82,22 +92,11 @@ module Minimapper
|
|
82
92
|
end
|
83
93
|
|
84
94
|
def delete_all
|
85
|
-
|
95
|
+
query_scope.delete_all
|
86
96
|
end
|
87
97
|
|
88
98
|
private
|
89
99
|
|
90
|
-
# NOTE: Don't memoize the record_class or code reloading will break in rails apps.
|
91
|
-
def record_class
|
92
|
-
"#{self.class.name}::Record".constantize
|
93
|
-
end
|
94
|
-
|
95
|
-
# Will attempt to use Project as the entity class when
|
96
|
-
# the mapper class name is ProjectMapper.
|
97
|
-
def entity_class
|
98
|
-
self.class.name.sub(/Mapper$/, '').constantize
|
99
|
-
end
|
100
|
-
|
101
100
|
def accessible_attributes(entity)
|
102
101
|
entity.attributes.reject { |k, v| protected_attributes.include?(k.to_s) }
|
103
102
|
end
|
@@ -110,21 +109,25 @@ module Minimapper
|
|
110
109
|
record.attributes = accessible_attributes(entity)
|
111
110
|
end
|
112
111
|
|
112
|
+
def copy_attributes_to_entity(record, entity)
|
113
|
+
entity.attributes = record.attributes.symbolize_keys
|
114
|
+
end
|
115
|
+
|
113
116
|
def validate_record_and_copy_errors_to_entity(record, entity)
|
114
117
|
record.valid?
|
115
118
|
entity.mapper_errors = record.errors.map { |k, v| [k, v] }
|
116
119
|
end
|
117
120
|
|
118
121
|
def find_record_safely(id)
|
119
|
-
|
122
|
+
query_scope.find(id)
|
120
123
|
end
|
121
124
|
|
122
125
|
def find_record(id)
|
123
|
-
id &&
|
126
|
+
id && query_scope.find_by_id(id)
|
124
127
|
end
|
125
128
|
|
126
129
|
def record_for(entity)
|
127
|
-
|
130
|
+
query_scope.find(entity.id)
|
128
131
|
end
|
129
132
|
|
130
133
|
def entities_for(records, klass = entity_class)
|
@@ -136,7 +139,7 @@ module Minimapper
|
|
136
139
|
entity = klass.new
|
137
140
|
entity.id = record.id
|
138
141
|
entity.mark_as_persisted
|
139
|
-
entity
|
142
|
+
copy_attributes_to_entity(record, entity)
|
140
143
|
|
141
144
|
if klass == entity_class
|
142
145
|
after_find(entity, record)
|
@@ -164,6 +167,22 @@ module Minimapper
|
|
164
167
|
result
|
165
168
|
end
|
166
169
|
|
170
|
+
# Override to customize default includes etc that apply to all queries
|
171
|
+
def query_scope
|
172
|
+
record_class
|
173
|
+
end
|
174
|
+
|
175
|
+
# NOTE: Don't memoize the record_class or code reloading will break in rails apps.
|
176
|
+
def record_class
|
177
|
+
"#{self.class.name}::Record".constantize
|
178
|
+
end
|
179
|
+
|
180
|
+
# Will attempt to use Project as the entity class when
|
181
|
+
# the mapper class name is ProjectMapper.
|
182
|
+
def entity_class
|
183
|
+
self.class.name.sub(/Mapper$/, '').constantize
|
184
|
+
end
|
185
|
+
|
167
186
|
# Hooks
|
168
187
|
|
169
188
|
def after_find(entity, record)
|
data/lib/minimapper/version.rb
CHANGED
data/spec/mapper_spec.rb
CHANGED
@@ -137,6 +137,22 @@ describe Minimapper::Mapper do
|
|
137
137
|
end
|
138
138
|
end
|
139
139
|
|
140
|
+
describe "#create!" do
|
141
|
+
it "can create records" do
|
142
|
+
entity = build_valid_entity
|
143
|
+
mapper.create!(entity)
|
144
|
+
entity.should be_persisted
|
145
|
+
end
|
146
|
+
|
147
|
+
it "raises Minimapper::EntityInvalid when the entity is invalid" do
|
148
|
+
entity = entity_class.new
|
149
|
+
def entity.valid?
|
150
|
+
false
|
151
|
+
end
|
152
|
+
lambda { mapper.create!(entity) }.should raise_error(Minimapper::EntityInvalid)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
140
156
|
describe "#find" do
|
141
157
|
it "returns an entity matching the id" do
|
142
158
|
entity = build_valid_entity
|
@@ -400,6 +416,25 @@ describe Minimapper::Mapper do
|
|
400
416
|
end
|
401
417
|
end
|
402
418
|
|
419
|
+
describe "#update!" do
|
420
|
+
it "can update records" do
|
421
|
+
entity = build_valid_entity
|
422
|
+
mapper.create(entity)
|
423
|
+
entity.attributes[:email] = "updated@example.com"
|
424
|
+
mapper.update!(entity)
|
425
|
+
mapper.reload(entity).attributes[:email].should == "updated@example.com"
|
426
|
+
end
|
427
|
+
|
428
|
+
it "raises Minimapper::EntityInvalid when the entity is invalid" do
|
429
|
+
entity = build_valid_entity
|
430
|
+
mapper.create(entity)
|
431
|
+
def entity.valid?
|
432
|
+
false
|
433
|
+
end
|
434
|
+
lambda { mapper.update!(entity) }.should raise_error(Minimapper::EntityInvalid)
|
435
|
+
end
|
436
|
+
end
|
437
|
+
|
403
438
|
describe "#delete" do
|
404
439
|
it "removes the entity" do
|
405
440
|
entity = build_valid_entity
|
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.10.
|
4
|
+
version: 0.10.1
|
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-10-
|
12
|
+
date: 2013-10-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70105129258360 !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: *70105129258360
|
25
25
|
description: A minimalistic way of separating your models from ActiveRecord.
|
26
26
|
email:
|
27
27
|
- joakim.kolsjo@gmail.com
|
@@ -45,6 +45,7 @@ files:
|
|
45
45
|
- lib/minimapper/entity/core.rb
|
46
46
|
- lib/minimapper/entity/rails.rb
|
47
47
|
- lib/minimapper/entity/validation.rb
|
48
|
+
- lib/minimapper/entity_invalid.rb
|
48
49
|
- lib/minimapper/mapper.rb
|
49
50
|
- lib/minimapper/repository.rb
|
50
51
|
- lib/minimapper/version.rb
|
@@ -76,7 +77,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
76
77
|
version: '0'
|
77
78
|
segments:
|
78
79
|
- 0
|
79
|
-
hash:
|
80
|
+
hash: 3246960416648791491
|
80
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
82
|
none: false
|
82
83
|
requirements:
|
@@ -85,10 +86,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
86
|
version: '0'
|
86
87
|
segments:
|
87
88
|
- 0
|
88
|
-
hash:
|
89
|
+
hash: 3246960416648791491
|
89
90
|
requirements: []
|
90
91
|
rubyforge_project:
|
91
|
-
rubygems_version: 1.8.
|
92
|
+
rubygems_version: 1.8.5
|
92
93
|
signing_key:
|
93
94
|
specification_version: 3
|
94
95
|
summary: A minimalistic way of separating your models from ActiveRecord.
|