minimapper 0.5.3 → 0.6.0
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 +6 -6
- data/lib/minimapper/entity/convert.rb +4 -0
- data/lib/minimapper/mapper/ar.rb +11 -4
- data/lib/minimapper/version.rb +1 -1
- data/unit/entity/convert_spec.rb +7 -3
- metadata +10 -4
data/README.md
CHANGED
@@ -174,9 +174,7 @@ end
|
|
174
174
|
module AR
|
175
175
|
class ProjectMapper < Minimapper::AR
|
176
176
|
def waiting_for_review
|
177
|
-
record_class.where(waiting_for_review: true).order("id DESC")
|
178
|
-
entity_for(record)
|
179
|
-
end
|
177
|
+
entities_for record_class.where(waiting_for_review: true).order("id DESC")
|
180
178
|
end
|
181
179
|
end
|
182
180
|
end
|
@@ -250,7 +248,9 @@ Minimapper only calls #convert on non-empty strings. When the value is blank or
|
|
250
248
|
|
251
249
|
### Associations
|
252
250
|
|
253
|
-
There is no
|
251
|
+
There is no core support for associations, but we're implementing them in [minimapper-extras](https://github.com/barsoom/minimapper-extras) as we need them.
|
252
|
+
|
253
|
+
For some discussion, [see this issue](https://github.com/joakimk/minimapper/issues/3).
|
254
254
|
|
255
255
|
### Lifecycle hooks
|
256
256
|
|
@@ -272,11 +272,11 @@ end
|
|
272
272
|
|
273
273
|
### Custom entity class
|
274
274
|
|
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
|
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)).
|
276
276
|
|
277
277
|
### Adding a new mapper
|
278
278
|
|
279
|
-
If you
|
279
|
+
If you were to add a [Mongoid](http://mongoid.org/en/mongoid/index.html) mapper:
|
280
280
|
|
281
281
|
1. Start by copying *spec/ar_spec.rb* to *spec/mongoid_spec.rb* and adapt it for Mongoid.
|
282
282
|
2. Add any setup code needed in *spec/support/database_setup.rb*.
|
@@ -26,6 +26,10 @@ module Minimapper
|
|
26
26
|
private
|
27
27
|
|
28
28
|
def converter_for(type)
|
29
|
+
if type && !@@converters[type]
|
30
|
+
raise "Unknown attribute type: #{type}. Perhaps you've spelled it wrong or not registered the converter for it."
|
31
|
+
end
|
32
|
+
|
29
33
|
@@converters.fetch(type, NoOpConverter.new)
|
30
34
|
end
|
31
35
|
|
data/lib/minimapper/mapper/ar.rb
CHANGED
@@ -9,6 +9,8 @@ module Minimapper
|
|
9
9
|
|
10
10
|
def create(entity)
|
11
11
|
record = record_class.new
|
12
|
+
|
13
|
+
copy_attributes_to_record(record, entity)
|
12
14
|
validate_record_and_copy_errors_to_entity(record, entity)
|
13
15
|
|
14
16
|
if entity.valid?
|
@@ -50,6 +52,8 @@ module Minimapper
|
|
50
52
|
|
51
53
|
def update(entity)
|
52
54
|
record = record_for(entity)
|
55
|
+
|
56
|
+
copy_attributes_to_record(record, entity)
|
53
57
|
validate_record_and_copy_errors_to_entity(record, entity)
|
54
58
|
|
55
59
|
if entity.valid?
|
@@ -80,13 +84,13 @@ module Minimapper
|
|
80
84
|
# NOTE: Don't memoize the classes or code reloading will break in rails apps.
|
81
85
|
|
82
86
|
# Will attempt to use AR:Project as the record class
|
83
|
-
# when the mapper class name is AR::ProjectMapper
|
87
|
+
# when the mapper class name is AR::ProjectMapper.
|
84
88
|
def record_class
|
85
89
|
self.class.name.sub(/Mapper$/, '').constantize
|
86
90
|
end
|
87
91
|
|
88
|
-
# Will attempt to use Project as the
|
89
|
-
# the mapper class name is AR::ProjectMapper
|
92
|
+
# Will attempt to use Project as the entity class when
|
93
|
+
# the mapper class name is AR::ProjectMapper.
|
90
94
|
def entity_class
|
91
95
|
("::" + self.class.name.split('::').last.sub(/Mapper$/, '')).constantize
|
92
96
|
end
|
@@ -99,8 +103,11 @@ module Minimapper
|
|
99
103
|
record_class.protected_attributes
|
100
104
|
end
|
101
105
|
|
102
|
-
def
|
106
|
+
def copy_attributes_to_record(record, entity)
|
103
107
|
record.attributes = accessible_attributes(entity)
|
108
|
+
end
|
109
|
+
|
110
|
+
def validate_record_and_copy_errors_to_entity(record, entity)
|
104
111
|
record.valid?
|
105
112
|
entity.mapper_errors = record.errors.map { |k, v| [k, v] }
|
106
113
|
end
|
data/lib/minimapper/version.rb
CHANGED
data/unit/entity/convert_spec.rb
CHANGED
@@ -18,11 +18,15 @@ describe Minimapper::Entity::Convert do
|
|
18
18
|
described_class.new('garbage').to(:date_time).should be_nil
|
19
19
|
end
|
20
20
|
|
21
|
-
it "returns the value as-is when
|
22
|
-
described_class.new('foobar').to(
|
21
|
+
it "returns the value as-is when the type isn't specified" do
|
22
|
+
described_class.new('foobar').to(nil).should == 'foobar'
|
23
23
|
end
|
24
24
|
|
25
25
|
it "does not make false nil" do
|
26
|
-
described_class.new(false).to(
|
26
|
+
described_class.new(false).to(nil).should eq(false)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "raises when the type isn't known" do
|
30
|
+
lambda { described_class.new('foobar').to(:unknown) }.should raise_error(/Unknown attribute type/)
|
27
31
|
end
|
28
32
|
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.
|
4
|
+
version: 0.6.0
|
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-05-
|
12
|
+
date: 2013-05-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70151297215480 !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: *70151297215480
|
25
25
|
description: A minimalistic way of separating your models from ORMs like ActiveRecord.
|
26
26
|
email:
|
27
27
|
- joakim.kolsjo@gmail.com
|
@@ -75,12 +75,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
75
|
- - ! '>='
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
hash: -4467739529293173565
|
78
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
82
|
none: false
|
80
83
|
requirements:
|
81
84
|
- - ! '>='
|
82
85
|
- !ruby/object:Gem::Version
|
83
86
|
version: '0'
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
hash: -4467739529293173565
|
84
90
|
requirements: []
|
85
91
|
rubyforge_project:
|
86
92
|
rubygems_version: 1.8.5
|