active_mappers 1.4.7 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +32 -22
- data/active_mappers.gemspec +3 -3
- data/lib/active_mappers/handlers/inheritance.rb +1 -2
- data/lib/active_mappers/key_transformer.rb +2 -2
- data/lib/active_mappers.rb +54 -12
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 32a8f6aba4888e9ff50871fab7eb457de33f785d5a32758c77bbb01c1ef75d80
|
4
|
+
data.tar.gz: a1c03591088552e3bbdf29e291de4e9628c50b06af5a294358935812d2419271
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b44b4c1097e49c8c4520eff7bf8d64cc5c98709b87ae429f9d53e613ca914e5cf15f4526dddbf422d8f45beaa8c7280ed520cd39e51f0c79b396739fc8d3f3c
|
7
|
+
data.tar.gz: f281126bf5b01be6eb640fcde71fbda419d9eed32417af5afd30f4c6f7245ccbbef6bade670bfe176730ce904562088623dd76c9e93b5d199cf675af374e3018
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
active_mappers (1.
|
4
|
+
active_mappers (1.5.0)
|
5
5
|
activesupport (>= 4.2)
|
6
6
|
method_source (~> 0.9.2)
|
7
7
|
mocha (>= 1.8.0)
|
@@ -11,31 +11,41 @@ PATH
|
|
11
11
|
GEM
|
12
12
|
remote: https://rubygems.org/
|
13
13
|
specs:
|
14
|
-
activesupport (
|
14
|
+
activesupport (7.1.3)
|
15
|
+
base64
|
16
|
+
bigdecimal
|
15
17
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
connection_pool (>= 2.2.5)
|
19
|
+
drb
|
20
|
+
i18n (>= 1.6, < 2)
|
21
|
+
minitest (>= 5.1)
|
22
|
+
mutex_m
|
23
|
+
tzinfo (~> 2.0)
|
24
|
+
base64 (0.2.0)
|
25
|
+
bigdecimal (3.1.6)
|
26
|
+
concurrent-ruby (1.2.3)
|
27
|
+
connection_pool (2.4.1)
|
28
|
+
drb (2.2.0)
|
29
|
+
ruby2_keywords
|
30
|
+
i18n (1.14.1)
|
22
31
|
concurrent-ruby (~> 1.0)
|
23
|
-
metaclass (0.0.4)
|
24
32
|
method_source (0.9.2)
|
25
|
-
minitest (5.
|
26
|
-
mocha (1.
|
27
|
-
|
28
|
-
|
29
|
-
|
33
|
+
minitest (5.21.2)
|
34
|
+
mocha (2.1.0)
|
35
|
+
ruby2_keywords (>= 0.0.5)
|
36
|
+
mutex_m (0.2.0)
|
37
|
+
racc (1.7.3)
|
38
|
+
rake (13.1.0)
|
39
|
+
ruby2_keywords (0.0.5)
|
40
|
+
ruby2ruby (2.5.0)
|
30
41
|
ruby_parser (~> 3.1)
|
31
42
|
sexp_processor (~> 4.6)
|
32
|
-
ruby_parser (3.
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
tzinfo (
|
37
|
-
|
38
|
-
zeitwerk (2.2.1)
|
43
|
+
ruby_parser (3.21.0)
|
44
|
+
racc (~> 1.5)
|
45
|
+
sexp_processor (~> 4.16)
|
46
|
+
sexp_processor (4.17.1)
|
47
|
+
tzinfo (2.0.6)
|
48
|
+
concurrent-ruby (~> 1.0)
|
39
49
|
|
40
50
|
PLATFORMS
|
41
51
|
ruby
|
@@ -45,4 +55,4 @@ DEPENDENCIES
|
|
45
55
|
rake
|
46
56
|
|
47
57
|
BUNDLED WITH
|
48
|
-
|
58
|
+
2.5.4
|
data/active_mappers.gemspec
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'active_mappers'
|
3
|
-
s.version = '1.
|
4
|
-
s.date = '
|
3
|
+
s.version = '1.5.1'
|
4
|
+
s.date = '2024-01-26'
|
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
|
-
s.authors = ['Michaël Villeneuve']
|
7
|
+
s.authors = ['Michaël Villeneuve', 'Loïc SENCE']
|
8
8
|
s.homepage = 'https://github.com/fidme/active_mappers'
|
9
9
|
s.email = 'contact@michaelvilleneuve.fr'
|
10
10
|
s.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
@@ -11,7 +11,6 @@ module ActiveMappers
|
|
11
11
|
|
12
12
|
@klass.class_variables.each do |var_name|
|
13
13
|
dsl_values = @subclass.class_variable_get(var_name)
|
14
|
-
|
15
14
|
dsl_values[@subclass.name] = dsl_values[@klass.name].dup
|
16
15
|
end
|
17
16
|
end
|
@@ -23,4 +22,4 @@ module ActiveMappers
|
|
23
22
|
end
|
24
23
|
end
|
25
24
|
end
|
26
|
-
end
|
25
|
+
end
|
@@ -12,8 +12,8 @@ module ActiveMappers
|
|
12
12
|
Setup.camelcase_keys ? hash.to_lower_camel_case : hash
|
13
13
|
end
|
14
14
|
|
15
|
-
def self.resource_to_mapper(resource, class_from)
|
16
|
-
"#{base_namespace(class_from)}::#{resource.class.name}Mapper".constantize
|
15
|
+
def self.resource_to_mapper(resource, class_from, scope=nil)
|
16
|
+
"#{base_namespace(class_from)}::#{resource.class.name}Mapper#{scope ? "Scope#{scope.capitalize}" : nil}".constantize
|
17
17
|
end
|
18
18
|
|
19
19
|
def self.resource_class_to_mapper(resource_class_name, class_from)
|
data/lib/active_mappers.rb
CHANGED
@@ -14,11 +14,16 @@ require_relative 'active_mappers/key_transformer'
|
|
14
14
|
module ActiveMappers
|
15
15
|
class Base
|
16
16
|
@@renderers = {}
|
17
|
+
@@inheritance_column = {}
|
17
18
|
|
18
19
|
def self.inherited(subclass)
|
19
20
|
Handlers::Inheritance.new(subclass, self).handle
|
20
21
|
end
|
21
22
|
|
23
|
+
def self.inheritance_column(val)
|
24
|
+
@@inheritance_column[name] = val
|
25
|
+
end
|
26
|
+
|
22
27
|
def self.attributes(*params)
|
23
28
|
each do |resource|
|
24
29
|
h = {}
|
@@ -80,14 +85,17 @@ module ActiveMappers
|
|
80
85
|
end
|
81
86
|
|
82
87
|
def self.each(&block)
|
88
|
+
# puts "[l. #{__LINE__}] [#{name}] each"
|
83
89
|
@@renderers[name] = (@@renderers[name] || []) << block
|
84
90
|
end
|
85
91
|
|
86
92
|
def self.with(args, options = {})
|
87
|
-
|
88
|
-
|
93
|
+
# puts "[l. #{__LINE__}] WITH - #{name} - #{options}"
|
94
|
+
if options[:scope].present? && !name.include?('Scope')
|
95
|
+
return evaluate_scopes(args, options)
|
96
|
+
end
|
89
97
|
response = if options[:rootless]
|
90
|
-
args.respond_to?(:each) ? all(args, options
|
98
|
+
args.respond_to?(:each) ? all(args, options) : one(args, options)
|
91
99
|
else
|
92
100
|
render_with_root(args, options)
|
93
101
|
end
|
@@ -95,13 +103,25 @@ module ActiveMappers
|
|
95
103
|
end
|
96
104
|
|
97
105
|
def self.evaluate_scopes(args, options)
|
98
|
-
|
99
|
-
|
106
|
+
# puts "[l. #{__LINE__}] [#{name}] evaluate_scopes #{options}"
|
107
|
+
class_to_call = begin
|
108
|
+
"::#{name}Scope#{options[:scope].capitalize}".constantize
|
109
|
+
rescue
|
110
|
+
if options[:fallback_class]
|
111
|
+
options[:fallback_class]
|
112
|
+
elsif options[:fallback_on_missing_scope]
|
113
|
+
self
|
114
|
+
else
|
115
|
+
raise("ActiveMappers [#{name}] No scope named #{options[:scope]} found")
|
116
|
+
end
|
117
|
+
end
|
118
|
+
# puts "[l.#{__LINE__}] evaluate_scopes class_to_call -> #{class_to_call}"
|
119
|
+
return class_to_call.with(args, options.merge(initial_mapper: self))
|
100
120
|
end
|
101
121
|
|
102
122
|
def self.scope(*params, &block)
|
123
|
+
# puts "[l.#{__LINE__}] [#{name}] CREATING SCOPE CLASSES (name: #{name} | params: #{params.inspect}) ===> ::#{name}Scope#{params.first.capitalize}"
|
103
124
|
raise "ActiveMappers [#{name}] scope must be a block" if block.nil? || !block.respond_to?(:call)
|
104
|
-
|
105
125
|
params.each do |param|
|
106
126
|
block_content = Ruby2Ruby.new.process(RubyParser.new.process(block.source).to_a.last)
|
107
127
|
eval("class ::#{name}Scope#{param.capitalize} < ::#{name} ; #{block_content}; end")
|
@@ -113,21 +133,43 @@ module ActiveMappers
|
|
113
133
|
resource_name ||= KeyTransformer.apply_on(self.name)
|
114
134
|
|
115
135
|
if args.respond_to?(:each)
|
116
|
-
{ resource_name.to_s.pluralize.to_sym => all(args, options
|
136
|
+
{ resource_name.to_s.pluralize.to_sym => all(args, options) }
|
117
137
|
else
|
118
|
-
{ resource_name.to_s.singularize.to_sym => one(args, options
|
138
|
+
{ resource_name.to_s.singularize.to_sym => one(args, options) }
|
119
139
|
end
|
120
140
|
end
|
121
141
|
|
122
|
-
def self.all(collection,
|
123
|
-
collection.map { |el| one(el,
|
142
|
+
def self.all(collection, options = {})
|
143
|
+
collection.map { |el| one(el, options) }.compact
|
124
144
|
end
|
125
145
|
|
126
|
-
def self.one(resource,
|
146
|
+
def self.one(resource, options = {})
|
147
|
+
# puts "[l.#{__LINE__}] [#{name}] ONE - options: #{options.inspect} - inheritance_column: #{@@inheritance_column[name]}"
|
127
148
|
return nil unless resource
|
149
|
+
|
150
|
+
if @@inheritance_column[name] && !options[:fallback_class]
|
151
|
+
main_mapper = KeyTransformer.resource_to_mapper(resource, self)
|
152
|
+
# puts "[l.#{__LINE__}] [#{name}] ONE - main_mapper #{main_mapper}"
|
153
|
+
mapper = options[:scope] ? (KeyTransformer.resource_to_mapper(resource, self, options[:scope]) rescue main_mapper) : main_mapper
|
154
|
+
# puts "[l.#{__LINE__}] [#{name}] ONE - mapper #{mapper}"
|
155
|
+
if name != mapper&.name
|
156
|
+
return mapper.with(resource, options.merge(rootless: true, fallback_class: options[:initial_mapper]))
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
128
160
|
return {} if @@renderers[name].nil? # Mapper is empty
|
161
|
+
|
162
|
+
# base_mapper = name.rpartition('::').first
|
163
|
+
|
164
|
+
# renderers = ancestors.select { |it| it.name.start_with?(base_mapper) }.map do |ancestor|
|
165
|
+
# puts "---> #{ancestor.name}"
|
166
|
+
# @@renderers[ancestor.name].map do |renderer|
|
167
|
+
# renderer.call(resource, options[:context])
|
168
|
+
# end.reduce(&:merge)
|
169
|
+
# end.reduce(&:merge)
|
170
|
+
|
129
171
|
renderers = @@renderers[name].map do |renderer|
|
130
|
-
renderer.call(resource, context)
|
172
|
+
renderer.call(resource, options[:context])
|
131
173
|
end.reduce(&:merge)
|
132
174
|
|
133
175
|
KeyTransformer.format_keys(renderers)
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_mappers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michaël Villeneuve
|
8
|
-
|
8
|
+
- Loïc SENCE
|
9
|
+
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2024-01-26 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: activesupport
|
@@ -101,7 +102,7 @@ homepage: https://github.com/fidme/active_mappers
|
|
101
102
|
licenses:
|
102
103
|
- MIT
|
103
104
|
metadata: {}
|
104
|
-
post_install_message:
|
105
|
+
post_install_message:
|
105
106
|
rdoc_options: []
|
106
107
|
require_paths:
|
107
108
|
- lib
|
@@ -116,8 +117,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
117
|
- !ruby/object:Gem::Version
|
117
118
|
version: '0'
|
118
119
|
requirements: []
|
119
|
-
rubygems_version: 3.
|
120
|
-
signing_key:
|
120
|
+
rubygems_version: 3.5.4
|
121
|
+
signing_key:
|
121
122
|
specification_version: 4
|
122
123
|
summary: Slick, fast view layer for you Rails API.
|
123
124
|
test_files: []
|