active_mappers 1.4.0 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -18
- data/active_mappers.gemspec +1 -1
- data/lib/active_mappers.rb +6 -2
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6645f47b539aaf463969e77a0124e6f175e1d416062a1b20b3cfb844a70d7f1
|
4
|
+
data.tar.gz: e9281f76b54f95f5435aefd6d546456ad2e8370f747603360cc6623727c42a85
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d1b60f6a7a67fd6f258427c202955cc4a60fcf143d57b4ccd6b10aa1b9d88e95ffeb3874cd72dcf997a5836dd7a59914be75bf7358d426bbabfb97a9195b4df
|
7
|
+
data.tar.gz: 0a921774c1c38eee7d0ec1adbf436676190bbdb2b2b3e396da146aec2bc024acd278ee49cd469017e32d26db03ea17e0f0a28888afade69c297417b2954ba7e6
|
data/README.md
CHANGED
@@ -32,7 +32,7 @@ Then, depending on your usage you may want to create an `app/mappers` folder in
|
|
32
32
|
|
33
33
|
You will put all your mappers inside of it.
|
34
34
|
|
35
|
-
##
|
35
|
+
## Basic usage
|
36
36
|
|
37
37
|
```ruby
|
38
38
|
UserMapper.with(user)
|
@@ -49,7 +49,7 @@ UserMapper.with(user)
|
|
49
49
|
# }
|
50
50
|
```
|
51
51
|
|
52
|
-
|
52
|
+
## Setup (optional)
|
53
53
|
|
54
54
|
You may want to customize some parts of ActiveMappers behavior.
|
55
55
|
|
@@ -71,7 +71,7 @@ Here is the list of configurable options
|
|
71
71
|
| `ignored_namespaces` | `Array` | `[]` | Namespaces to ignore when generating json root key name |
|
72
72
|
| `root_keys_transformer` | `Proc` | See below | Custom way to change a mapper class name into a JSON root key |
|
73
73
|
|
74
|
-
|
74
|
+
### Root Keys Transformer
|
75
75
|
|
76
76
|
A root key transform is used to transform the mapper class name into a JSON root key name.
|
77
77
|
|
@@ -101,9 +101,9 @@ config.root_keys_transformer = proc do |key|
|
|
101
101
|
end
|
102
102
|
```
|
103
103
|
|
104
|
-
|
104
|
+
## API
|
105
105
|
|
106
|
-
|
106
|
+
### Declaring your attributes
|
107
107
|
|
108
108
|
Most basic usage, just declare the attributes you want to display.
|
109
109
|
|
@@ -113,7 +113,7 @@ class UserMapper < ActiveMappers::Base # You must extend ActiveMappers::Base in
|
|
113
113
|
end
|
114
114
|
```
|
115
115
|
|
116
|
-
|
116
|
+
### Delegating your attributes
|
117
117
|
|
118
118
|
Say you have a model with the following structure :
|
119
119
|
|
@@ -144,7 +144,7 @@ class UserMapper < ActiveMappers::Base
|
|
144
144
|
end
|
145
145
|
```
|
146
146
|
|
147
|
-
|
147
|
+
### Declaring relationship
|
148
148
|
|
149
149
|
You can declare any type of relationship (`has_one`, `belongs_to`, `has_many`, etc) and the mapper that matches it will automatically be fetched and used.
|
150
150
|
|
@@ -179,14 +179,13 @@ It also works with namespaced resources.
|
|
179
179
|
|
180
180
|
If you need you can specify more options :
|
181
181
|
|
182
|
-
|
182
|
+
```ruby
|
183
183
|
class UserMapper < ActiveMappers::Base
|
184
184
|
relation :account, AccountMapper, scope: :admin
|
185
185
|
end
|
186
|
+
```
|
186
187
|
|
187
|
-
|
188
|
-
|
189
|
-
**Declaring polymorphic relationships**
|
188
|
+
### Declaring polymorphic relationships
|
190
189
|
|
191
190
|
Consider the following polymorphic relation :
|
192
191
|
|
@@ -202,7 +201,7 @@ end
|
|
202
201
|
class NormalUser
|
203
202
|
has_many :posts, class_name: 'Post', as: :author
|
204
203
|
end
|
205
|
-
|
204
|
+
```
|
206
205
|
|
207
206
|
In order to use the `author` polymorphic attribute in your `PostMapper` you need to declare the following :
|
208
207
|
|
@@ -226,7 +225,7 @@ end
|
|
226
225
|
|
227
226
|
Then, based of the `XXX_type` column, the mapper will automatically resolve to either `AdminUserMapper` or `NormalUserMapper`
|
228
227
|
|
229
|
-
|
228
|
+
### Rendering a collection of different classes
|
230
229
|
|
231
230
|
Say you want to render many resources with a single Mapper
|
232
231
|
|
@@ -266,7 +265,7 @@ Will generate the following :
|
|
266
265
|
|
267
266
|
Again, just like the above polymorphic declaration, the mapper will automatically resolve to the corresponding one.
|
268
267
|
|
269
|
-
|
268
|
+
### Custom Attributes
|
270
269
|
|
271
270
|
If you need to implement custom attributes you can always use the `each` statement.
|
272
271
|
|
@@ -299,7 +298,7 @@ Will generate the following:
|
|
299
298
|
You can declare any number of `each` in a single mapper.
|
300
299
|
Actually, `each` is used to implement every above features.
|
301
300
|
|
302
|
-
|
301
|
+
### Context
|
303
302
|
|
304
303
|
In most cases declaring your mapper with your resource should be enough. However, sometimes you may need to give your mapper a context in order to allow it to resolve a method or something else.
|
305
304
|
|
@@ -317,7 +316,7 @@ end
|
|
317
316
|
RewardMapper.with(reward, context: user)
|
318
317
|
```
|
319
318
|
|
320
|
-
|
319
|
+
### Scope
|
321
320
|
|
322
321
|
ActiveMappers does not yet support inheritance. However we provide an even better alternative named `scope`.
|
323
322
|
|
@@ -385,7 +384,7 @@ By default, root will be enabled, meaning a UserMapper, will generate a JSON pre
|
|
385
384
|
}
|
386
385
|
```
|
387
386
|
|
388
|
-
|
387
|
+
### Custom Root
|
389
388
|
|
390
389
|
If you want to customize the root name, you can use
|
391
390
|
|
@@ -401,7 +400,7 @@ which will generate :
|
|
401
400
|
}
|
402
401
|
```
|
403
402
|
|
404
|
-
|
403
|
+
### Rootless
|
405
404
|
|
406
405
|
If you do not want to set any root, use :
|
407
406
|
|
data/active_mappers.gemspec
CHANGED
data/lib/active_mappers.rb
CHANGED
@@ -56,8 +56,12 @@ module ActiveMappers
|
|
56
56
|
|
57
57
|
def self.polymorphic(key)
|
58
58
|
each do |resource|
|
59
|
-
|
60
|
-
|
59
|
+
if polymorphic_resource = resource.send("#{key}_type")
|
60
|
+
resource_mapper = "#{KeyTransformer.base_namespace(self)}::#{polymorphic_resource}Mapper".constantize
|
61
|
+
{ key => resource_mapper.with(resource.send(key), rootless: true) }
|
62
|
+
else
|
63
|
+
{}
|
64
|
+
end
|
61
65
|
end
|
62
66
|
end
|
63
67
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_mappers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michaël Villeneuve
|
@@ -116,8 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: '0'
|
118
118
|
requirements: []
|
119
|
-
|
120
|
-
rubygems_version: 2.7.3
|
119
|
+
rubygems_version: 3.0.1
|
121
120
|
signing_key:
|
122
121
|
specification_version: 4
|
123
122
|
summary: Slick, fast view layer for you Rails API.
|