active_mappers 1.4.1 → 1.4.6

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: c6645f47b539aaf463969e77a0124e6f175e1d416062a1b20b3cfb844a70d7f1
4
- data.tar.gz: e9281f76b54f95f5435aefd6d546456ad2e8370f747603360cc6623727c42a85
3
+ metadata.gz: ac980b374337decc27f455542605a5aaf6da146ec1a3b96547e69f9287061b44
4
+ data.tar.gz: 1df7c23ac090efebb1498bc6ecc8d9944a282edf03179fae71a88c5204c08102
5
5
  SHA512:
6
- metadata.gz: 6d1b60f6a7a67fd6f258427c202955cc4a60fcf143d57b4ccd6b10aa1b9d88e95ffeb3874cd72dcf997a5836dd7a59914be75bf7358d426bbabfb97a9195b4df
7
- data.tar.gz: 0a921774c1c38eee7d0ec1adbf436676190bbdb2b2b3e396da146aec2bc024acd278ee49cd469017e32d26db03ea17e0f0a28888afade69c297417b2954ba7e6
6
+ metadata.gz: 40d83c52a139b9f76e7eda851cc071f4435b3a7c8d35a294e632d45a92471c34753b72c5ac0d5b0e600646ad7cce468a7f56ffbca0b39ab8f410a28fea9c1a8e
7
+ data.tar.gz: 6aadec0ca9cf96a877df5e534b1be7870cee3417d2332d23410b8bb0e223af1224b6b85a0b4b5f11c319525c8f643f020e1dd98680acfaf7ad861ce622050947
@@ -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.1'
4
- s.date = '2019-05-10'
3
+ s.version = '1.4.6'
4
+ s.date = '2020-11-10'
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?)
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|
61
+ def self.polymorphic(key, **options)
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), 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.1
4
+ version: 1.4.6
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-05-10 00:00:00.000000000 Z
11
+ date: 2020-11-10 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.