active_mappers 1.4.0 → 1.4.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aeca810a9999765703a5f64cc394c4b30cbb2d0891f6648c5402efa1d336ffdd
4
- data.tar.gz: 8e77fda2149230f5bab01d50083bd8254b84bd34d28306abc86f829a7741d151
3
+ metadata.gz: 74222f3c7d4f7ba70429c27aefde7a52c5fb34c811f904ed6608e22c4687df80
4
+ data.tar.gz: 97c3d7bc5d23f0b73f59ffe9e9edb4f32c4fbb5007ae1d9767c0625f1cb52563
5
5
  SHA512:
6
- metadata.gz: 7cc3c0b2848d31fc0edf7448fcacf68cbce32c9919255363199d9fbfdc86ce97e8d779effb4187ddb3d93157042fc69e7037d5ebf418ebcc702830e6301906f0
7
- data.tar.gz: 337e71b30d908254f7e394d7767888fcd90bcd7a70b1a88796a315d68de7bcda105986f3b0d5746b1d292a7a4a8af35a8eee17e730fc765ed84a533430ee4918
6
+ metadata.gz: 10575096699c2016d0e0fae9e4eb815daa01e5c6b607441acb11b482538c941fc3680b50270c9c549e023774e2e2d266682da59628085fd1b187082df0c63440
7
+ data.tar.gz: c4c854c19f6e51fef45fde92c7b178aef063718d98fce57a7fb856626621a612a448ba9fa84eafecdd6ecb7506def98e5009078a7279666d774dcdb37b1d792f
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_mappers (1.4.0)
4
+ active_mappers (1.4.4)
5
5
  activesupport (>= 4.2)
6
6
  method_source (~> 0.9.2)
7
7
  mocha (>= 1.8.0)
@@ -11,29 +11,31 @@ PATH
11
11
  GEM
12
12
  remote: https://rubygems.org/
13
13
  specs:
14
- activesupport (5.2.2)
14
+ activesupport (6.0.1)
15
15
  concurrent-ruby (~> 1.0, >= 1.0.2)
16
16
  i18n (>= 0.7, < 2)
17
17
  minitest (~> 5.1)
18
18
  tzinfo (~> 1.1)
19
+ zeitwerk (~> 2.2)
19
20
  concurrent-ruby (1.1.5)
20
- i18n (1.6.0)
21
+ i18n (1.7.0)
21
22
  concurrent-ruby (~> 1.0)
22
23
  metaclass (0.0.4)
23
24
  method_source (0.9.2)
24
- minitest (5.11.3)
25
- mocha (1.8.0)
25
+ minitest (5.13.0)
26
+ mocha (1.9.0)
26
27
  metaclass (~> 0.0.1)
27
28
  rake (12.3.1)
28
- ruby2ruby (2.4.2)
29
+ ruby2ruby (2.4.4)
29
30
  ruby_parser (~> 3.1)
30
31
  sexp_processor (~> 4.6)
31
- ruby_parser (3.13.0)
32
+ ruby_parser (3.14.1)
32
33
  sexp_processor (~> 4.9)
33
- sexp_processor (4.12.0)
34
+ sexp_processor (4.13.0)
34
35
  thread_safe (0.3.6)
35
36
  tzinfo (1.2.5)
36
37
  thread_safe (~> 0.1)
38
+ zeitwerk (2.2.1)
37
39
 
38
40
  PLATFORMS
39
41
  ruby
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
- ## Usage
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
- ### Setup (optional)
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
- **Root Keys Transformer**
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
- ### Creating a mapper
104
+ ## API
105
105
 
106
- **Declaring your attributes**
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
- **Delegating your attributes**
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
- **Declaring relationship**
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
- ````ruby
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
- **Rendering a collection of different classes**
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
- **Custom Attributes**
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
- **Context**
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
- **Scope**
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
- **Custom Root**
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
- **Rootless**
403
+ ### Rootless
405
404
 
406
405
  If you do not want to set any root, use :
407
406
 
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'active_mappers'
3
- s.version = '1.4.0'
4
- s.date = '2019-05-10'
3
+ s.version = '1.4.5'
4
+ s.date = '2020-08-27'
5
5
  s.summary = 'Slick, fast view layer for you Rails API.'
6
6
  s.description = 'Fast, simple, declarative way to design your API\'s view layer'
7
7
  s.authors = ['Michaël Villeneuve']
@@ -44,27 +44,36 @@ module ActiveMappers
44
44
  def self.relation(key, mapper = nil, **options)
45
45
  path = options[:optional_path] || key
46
46
  each do |resource|
47
- relation_class_name = resource.class&.reflect_on_association(options[:optional_path] || key)&.class_name
48
- raise "undefined relation : #{key.to_s}" if (mapper.nil? && relation_class_name.nil?)
49
-
50
- mapper_to_use = mapper || KeyTransformer.resource_class_to_mapper(relation_class_name, self)
51
- raise "'#{mapper_to_use.name}' should be a mapper" unless mapper_to_use.ancestors.map(&:to_s).include?("ActiveMappers::Base")
47
+ mapper_to_use = if mapper
48
+ mapper
49
+ else
50
+ relation_class_name = resource.class&.reflect_on_association(options[:optional_path] || key)&.class_name
51
+ raise "undefined relation : #{key.to_s}" if (mapper.nil? && relation_class_name.nil?)
52
+ KeyTransformer.resource_class_to_mapper(relation_class_name, self)
53
+ end
52
54
 
53
- { key => mapper_to_use.with(path.to_s.split('.').inject(resource, :try), options.merge(rootless: true)) }
55
+ raise "'#{mapper_to_use.name}' should be a mapper" unless mapper_to_use.ancestors.map(&:to_s).include?("ActiveMappers::Base")
56
+
57
+ { key => mapper_to_use.with(path.to_s.split('.').inject(resource, :try), default_options.merge(options)) }
54
58
  end
55
59
  end
56
60
 
57
- def self.polymorphic(key)
58
- each do |resource|
59
- resource_mapper = "#{KeyTransformer.base_namespace(self)}::#{resource.send("#{key}_type")}Mapper".constantize
60
- { key => resource_mapper.with(resource.send(key), rootless: true) }
61
+ def self.polymorphic(key, **options)
62
+ each do |resource, context|
63
+ options[:context] = context
64
+ if polymorphic_resource = resource.send("#{key}_type")
65
+ resource_mapper = "#{KeyTransformer.base_namespace(self)}::#{polymorphic_resource}Mapper".constantize
66
+ { key => resource_mapper.with(resource.send(key), default_options.merge(options)) }
67
+ else
68
+ {}
69
+ end
61
70
  end
62
71
  end
63
72
 
64
- def self.acts_as_polymorph
73
+ def self.acts_as_polymorph(**options)
65
74
  each do |resource|
66
75
  mapper = KeyTransformer.resource_to_mapper(resource, self)
67
- mapper.with(resource, rootless: true)
76
+ mapper.with(resource, default_options.merge(options))
68
77
  rescue NameError
69
78
  raise NotImplementedError, 'No mapper found for this type of resource'
70
79
  end
@@ -76,7 +85,7 @@ module ActiveMappers
76
85
 
77
86
  def self.with(args, options = {})
78
87
  return evaluate_scopes(args, options) unless options[:scope].nil?
79
-
88
+
80
89
  response = if options[:rootless]
81
90
  args.respond_to?(:each) ? all(args, options[:context]) : one(args, options[:context])
82
91
  else
@@ -86,14 +95,13 @@ module ActiveMappers
86
95
  end
87
96
 
88
97
  def self.evaluate_scopes(args, options)
89
- class_to_call = "::#{name}Scope#{options[:scope].capitalize}".constantize rescue raise("ActiveMappers [#{name}] No scope named #{options[:scope]} found")
98
+ class_to_call = "::#{name}Scope#{options[:scope].capitalize}".constantize rescue (options[:fallback_on_missing_scope] ? self : raise("ActiveMappers [#{name}] No scope named #{options[:scope]} found"))
90
99
  return class_to_call.with(args, options.except(:scope))
91
100
  end
92
101
 
93
102
  def self.scope(*params, &block)
94
- raise "ActiveMappers [#{name}] scope must be a bloc" if block.nil? || !block.respond_to?(:call)
103
+ raise "ActiveMappers [#{name}] scope must be a block" if block.nil? || !block.respond_to?(:call)
95
104
 
96
-
97
105
  params.each do |param|
98
106
  block_content = Ruby2Ruby.new.process(RubyParser.new.process(block.source).to_a.last)
99
107
  eval("class ::#{name}Scope#{param.capitalize} < ::#{name} ; #{block_content}; end")
@@ -103,7 +111,7 @@ module ActiveMappers
103
111
  def self.render_with_root(args, options = {})
104
112
  resource_name = options[:root]
105
113
  resource_name ||= KeyTransformer.apply_on(self.name)
106
-
114
+
107
115
  if args.respond_to?(:each)
108
116
  { resource_name.to_s.pluralize.to_sym => all(args, options[:context]) }
109
117
  else
@@ -124,5 +132,10 @@ module ActiveMappers
124
132
 
125
133
  KeyTransformer.format_keys(renderers)
126
134
  end
135
+
136
+ def self.default_options
137
+ { rootless: true, fallback_on_missing_scope: true }
138
+ end
139
+
127
140
  end
128
141
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_mappers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michaël Villeneuve
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-10 00:00:00.000000000 Z
11
+ date: 2020-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -101,7 +101,7 @@ homepage: https://github.com/fidme/active_mappers
101
101
  licenses:
102
102
  - MIT
103
103
  metadata: {}
104
- post_install_message:
104
+ post_install_message:
105
105
  rdoc_options: []
106
106
  require_paths:
107
107
  - lib
@@ -116,9 +116,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
118
  requirements: []
119
- rubyforge_project:
120
- rubygems_version: 2.7.3
121
- signing_key:
119
+ rubygems_version: 3.0.3
120
+ signing_key:
122
121
  specification_version: 4
123
122
  summary: Slick, fast view layer for you Rails API.
124
123
  test_files: []