active_mappers 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +29 -11
- data/active_mappers.gemspec +1 -1
- data/lib/active_mappers.rb +7 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aeca810a9999765703a5f64cc394c4b30cbb2d0891f6648c5402efa1d336ffdd
|
4
|
+
data.tar.gz: 8e77fda2149230f5bab01d50083bd8254b84bd34d28306abc86f829a7741d151
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7cc3c0b2848d31fc0edf7448fcacf68cbce32c9919255363199d9fbfdc86ce97e8d779effb4187ddb3d93157042fc69e7037d5ebf418ebcc702830e6301906f0
|
7
|
+
data.tar.gz: 337e71b30d908254f7e394d7767888fcd90bcd7a70b1a88796a315d68de7bcda105986f3b0d5746b1d292a7a4a8af35a8eee17e730fc765ed84a533430ee4918
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -7,12 +7,12 @@ If you have ever done Rails API development, you must have considered using a la
|
|
7
7
|
|
8
8
|
There are multiple solutions out on the market, here is a quick overview of each :
|
9
9
|
|
10
|
-
| Solution | Pros | Cons
|
11
|
-
| ------------------------ | ---------------------------------------------------------------------------------- |
|
12
|
-
| JBuilder | Simple, easy, integrates with the default View layer | Very slow, dedicated to JSON
|
13
|
-
| Active Model Serializers | Simple, easy to declare | Can be hard to customize, slow, project is
|
14
|
-
| fast_json_api | As simple as AMS, fast | Hard to customize, JSONAPI standard is required
|
15
|
-
| ActiveMappers | Blazing fast, Easy to declare/customize, works with any format output (JSON, Hash) | Limited number of options (for now)
|
10
|
+
| Solution | Pros | Cons |
|
11
|
+
| ------------------------ | ---------------------------------------------------------------------------------- | ---------------------------------------------------- |
|
12
|
+
| JBuilder | Simple, easy, integrates with the default View layer | Very slow, dedicated to JSON |
|
13
|
+
| Active Model Serializers | Simple, easy to declare | Can be hard to customize, slow, project is abandoned |
|
14
|
+
| fast_json_api | As simple as AMS, fast | Hard to customize, JSONAPI standard is required |
|
15
|
+
| ActiveMappers | Blazing fast, Easy to declare/customize, works with any format output (JSON, Hash) | Limited number of options (for now) |
|
16
16
|
|
17
17
|
## Installation
|
18
18
|
|
@@ -179,9 +179,9 @@ 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
|
-
relation :account, AccountMapper, scope: :admin
|
184
|
+
relation :account, AccountMapper, scope: :admin
|
185
185
|
end
|
186
186
|
|
187
187
|
|
@@ -202,7 +202,7 @@ end
|
|
202
202
|
class NormalUser
|
203
203
|
has_many :posts, class_name: 'Post', as: :author
|
204
204
|
end
|
205
|
-
|
205
|
+
````
|
206
206
|
|
207
207
|
In order to use the `author` polymorphic attribute in your `PostMapper` you need to declare the following :
|
208
208
|
|
@@ -299,6 +299,24 @@ Will generate the following:
|
|
299
299
|
You can declare any number of `each` in a single mapper.
|
300
300
|
Actually, `each` is used to implement every above features.
|
301
301
|
|
302
|
+
**Context**
|
303
|
+
|
304
|
+
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
|
+
|
306
|
+
At fidme we found a use case where we need to send an instance of a user along with a mapper in order to call a model method with user as params.
|
307
|
+
|
308
|
+
To solve this use case we use the context option, for instance :
|
309
|
+
|
310
|
+
```ruby
|
311
|
+
class RewardMapper < ActiveMappers::Base
|
312
|
+
each do |reward, context|
|
313
|
+
{ user_reward: reward.of_user(context) }
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
RewardMapper.with(reward, context: user)
|
318
|
+
```
|
319
|
+
|
302
320
|
**Scope**
|
303
321
|
|
304
322
|
ActiveMappers does not yet support inheritance. However we provide an even better alternative named `scope`.
|
@@ -336,7 +354,6 @@ UserMapper.with(User.first, scope: :owner)
|
|
336
354
|
# => { pseudo: 'michael33', email: 'mvilleneuve@fidme.com' }
|
337
355
|
```
|
338
356
|
|
339
|
-
|
340
357
|
## Using a mapper
|
341
358
|
|
342
359
|
Even though there are many ways to declare a mapper, there is only one way to use it
|
@@ -418,7 +435,8 @@ module ActiveMappers
|
|
418
435
|
end
|
419
436
|
end
|
420
437
|
```
|
421
|
-
|
438
|
+
|
439
|
+
and then:
|
422
440
|
|
423
441
|
```ruby
|
424
442
|
class UserMapper < ActiveMappers::Base
|
data/active_mappers.gemspec
CHANGED
data/lib/active_mappers.rb
CHANGED
@@ -78,7 +78,7 @@ module ActiveMappers
|
|
78
78
|
return evaluate_scopes(args, options) unless options[:scope].nil?
|
79
79
|
|
80
80
|
response = if options[:rootless]
|
81
|
-
args.respond_to?(:each) ? all(args) : one(args)
|
81
|
+
args.respond_to?(:each) ? all(args, options[:context]) : one(args, options[:context])
|
82
82
|
else
|
83
83
|
render_with_root(args, options)
|
84
84
|
end
|
@@ -105,21 +105,21 @@ module ActiveMappers
|
|
105
105
|
resource_name ||= KeyTransformer.apply_on(self.name)
|
106
106
|
|
107
107
|
if args.respond_to?(:each)
|
108
|
-
{ resource_name.to_s.pluralize.to_sym => all(args) }
|
108
|
+
{ resource_name.to_s.pluralize.to_sym => all(args, options[:context]) }
|
109
109
|
else
|
110
|
-
{ resource_name.to_s.singularize.to_sym => one(args) }
|
110
|
+
{ resource_name.to_s.singularize.to_sym => one(args, options[:context]) }
|
111
111
|
end
|
112
112
|
end
|
113
113
|
|
114
|
-
def self.all(collection)
|
115
|
-
collection.map { |el| one(el) }.compact
|
114
|
+
def self.all(collection, context = nil)
|
115
|
+
collection.map { |el| one(el, context) }.compact
|
116
116
|
end
|
117
117
|
|
118
|
-
def self.one(resource)
|
118
|
+
def self.one(resource, context = nil)
|
119
119
|
return nil unless resource
|
120
120
|
return {} if @@renderers[name].nil? # Mapper is empty
|
121
121
|
renderers = @@renderers[name].map do |renderer|
|
122
|
-
renderer.call(resource)
|
122
|
+
renderer.call(resource, context)
|
123
123
|
end.reduce(&:merge)
|
124
124
|
|
125
125
|
KeyTransformer.format_keys(renderers)
|