active_mappers 1.4.2 → 1.4.7

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: '0474908efb2d39ccf71fce1f9b50c66b92fd25e079983d16393dbf2effd111e3'
4
- data.tar.gz: f1f3d5e37d076424e2dda3935177f7d73c35d56cbab4cb45e0cf11a0dcd5d7e4
3
+ metadata.gz: d9c952cdc3025177afc55d21f7967db40c7dad0fee5209701332433978d00c65
4
+ data.tar.gz: dae057bb4874202b6534ca59d7c0e11ca3765863ed27c9ddd1b83e64baa4bb41
5
5
  SHA512:
6
- metadata.gz: 106e4e860470afcab4d85def1fab45b9b1649548d1d28be7e5e9f72c3d009b36caaa83b77938b5671bf1210cac75a15ad487813aebda0299b6e847a7fcc81eab
7
- data.tar.gz: c2104b4b538e95049bb7de6aa04c9e63fdad645a76c1af11cfea1942e62fdc27a8e2bdb9a8e8c13df6d50e41067b37ff9926ed7ba007b10101cee0aedde7a257
6
+ metadata.gz: 4fae6b5c455eb0c8eb1798a94dfac64b3bcc8cc82df6761f8669e9f574fdc3efc8e51bcfd862abf9a9293601bf28e8ea2642b55e13ba4a17f4af05efde430085
7
+ data.tar.gz: 339571d128963541e8ba5ee0bde89b688b17b409169e03cecdc0afc3787d9b3fe4203a91ec1de32f32a7b87dde75b3d9d3079965934c8383df27f5aaa3b77a13
@@ -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
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'active_mappers'
3
- s.version = '1.4.2'
4
- s.date = '2019-10-09'
3
+ s.version = '1.4.7'
4
+ s.date = '2020-11-12'
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,31 +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?)
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.dup, self)
53
+ end
49
54
 
50
- mapper_to_use = mapper || KeyTransformer.resource_class_to_mapper(relation_class_name, self)
51
55
  raise "'#{mapper_to_use.name}' should be a mapper" unless mapper_to_use.ancestors.map(&:to_s).include?("ActiveMappers::Base")
52
-
53
- { key => mapper_to_use.with(path.to_s.split('.').inject(resource, :try), options.merge(rootless: true)) }
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
61
  def self.polymorphic(key, **options)
58
- each do |resource|
62
+ each do |resource, context|
63
+ options[:context] = context
59
64
  if polymorphic_resource = resource.send("#{key}_type")
60
65
  resource_mapper = "#{KeyTransformer.base_namespace(self)}::#{polymorphic_resource}Mapper".constantize
61
- { key => resource_mapper.with(resource.send(key), options.merge(rootless: true)) }
66
+ { key => resource_mapper.with(resource.send(key), default_options.merge(options)) }
62
67
  else
63
68
  {}
64
69
  end
65
70
  end
66
71
  end
67
72
 
68
- def self.acts_as_polymorph
73
+ def self.acts_as_polymorph(**options)
69
74
  each do |resource|
70
75
  mapper = KeyTransformer.resource_to_mapper(resource, self)
71
- mapper.with(resource, rootless: true)
76
+ mapper.with(resource, default_options.merge(options))
72
77
  rescue NameError
73
78
  raise NotImplementedError, 'No mapper found for this type of resource'
74
79
  end
@@ -80,7 +85,7 @@ module ActiveMappers
80
85
 
81
86
  def self.with(args, options = {})
82
87
  return evaluate_scopes(args, options) unless options[:scope].nil?
83
-
88
+
84
89
  response = if options[:rootless]
85
90
  args.respond_to?(:each) ? all(args, options[:context]) : one(args, options[:context])
86
91
  else
@@ -90,14 +95,13 @@ module ActiveMappers
90
95
  end
91
96
 
92
97
  def self.evaluate_scopes(args, options)
93
- 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"))
94
99
  return class_to_call.with(args, options.except(:scope))
95
100
  end
96
101
 
97
102
  def self.scope(*params, &block)
98
- 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)
99
104
 
100
-
101
105
  params.each do |param|
102
106
  block_content = Ruby2Ruby.new.process(RubyParser.new.process(block.source).to_a.last)
103
107
  eval("class ::#{name}Scope#{param.capitalize} < ::#{name} ; #{block_content}; end")
@@ -107,7 +111,7 @@ module ActiveMappers
107
111
  def self.render_with_root(args, options = {})
108
112
  resource_name = options[:root]
109
113
  resource_name ||= KeyTransformer.apply_on(self.name)
110
-
114
+
111
115
  if args.respond_to?(:each)
112
116
  { resource_name.to_s.pluralize.to_sym => all(args, options[:context]) }
113
117
  else
@@ -128,5 +132,10 @@ module ActiveMappers
128
132
 
129
133
  KeyTransformer.format_keys(renderers)
130
134
  end
135
+
136
+ def self.default_options
137
+ { rootless: true, fallback_on_missing_scope: true }
138
+ end
139
+
131
140
  end
132
141
  end
@@ -18,14 +18,14 @@ module ActiveMappers
18
18
 
19
19
  def self.resource_class_to_mapper(resource_class_name, class_from)
20
20
  resource_class_name[0..1] = '' if resource_class_name.start_with?('::')
21
-
21
+
22
22
  "#{base_namespace(class_from)}::#{resource_class_name}Mapper".constantize
23
23
  rescue NameError
24
24
  raise "undefined mapper: '#{base_namespace(class_from)}::#{resource_class_name}Mapper'"
25
25
  end
26
26
 
27
27
  def initialize(name)
28
- @name = name
28
+ @name = name.dup
29
29
  end
30
30
 
31
31
  def remove_ignored_namespace
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.2
4
+ version: 1.4.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michaël Villeneuve
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-09 00:00:00.000000000 Z
11
+ date: 2020-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
118
  requirements: []
119
- rubygems_version: 3.0.1
119
+ rubygems_version: 3.1.2
120
120
  signing_key:
121
121
  specification_version: 4
122
122
  summary: Slick, fast view layer for you Rails API.