active_mappers 1.5.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3667d085d34c9dc82b81c74d86ee0d71586582b2b52072e22c0ef79b2cf12bfa
4
- data.tar.gz: 2062f976d9bd10e73020fedeab83fcc468a14685e713fbd44e0b927e160872b7
3
+ metadata.gz: 32a8f6aba4888e9ff50871fab7eb457de33f785d5a32758c77bbb01c1ef75d80
4
+ data.tar.gz: a1c03591088552e3bbdf29e291de4e9628c50b06af5a294358935812d2419271
5
5
  SHA512:
6
- metadata.gz: 91225abdb07c262dedcd047a97ea95a8ef132519591880decde3525311d9a1cba65e5f3b33542cf826dfa5ddc639b2744dfbd08dcaa4e185cddd0aaf1c80a5f6
7
- data.tar.gz: 0dcaa09aed08f57a9026c4ac6887315c39664959573d76ca95ef81c6c7b17c7f141deb73742debcbd0a972fce9b065e8123bf0587597db59b49bca7eb14a1383
6
+ metadata.gz: 0b44b4c1097e49c8c4520eff7bf8d64cc5c98709b87ae429f9d53e613ca914e5cf15f4526dddbf422d8f45beaa8c7280ed520cd39e51f0c79b396739fc8d3f3c
7
+ data.tar.gz: f281126bf5b01be6eb640fcde71fbda419d9eed32417af5afd30f4c6f7245ccbbef6bade670bfe176730ce904562088623dd76c9e93b5d199cf675af374e3018
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'active_mappers'
3
- s.version = '1.5.0'
3
+ s.version = '1.5.1'
4
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'
@@ -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
@@ -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)
@@ -85,12 +85,15 @@ module ActiveMappers
85
85
  end
86
86
 
87
87
  def self.each(&block)
88
+ # puts "[l. #{__LINE__}] [#{name}] each"
88
89
  @@renderers[name] = (@@renderers[name] || []) << block
89
90
  end
90
91
 
91
92
  def self.with(args, options = {})
92
- return evaluate_scopes(args, options) unless options[:scope].nil?
93
-
93
+ # puts "[l. #{__LINE__}] WITH - #{name} - #{options}"
94
+ if options[:scope].present? && !name.include?('Scope')
95
+ return evaluate_scopes(args, options)
96
+ end
94
97
  response = if options[:rootless]
95
98
  args.respond_to?(:each) ? all(args, options) : one(args, options)
96
99
  else
@@ -100,13 +103,25 @@ module ActiveMappers
100
103
  end
101
104
 
102
105
  def self.evaluate_scopes(args, options)
103
- 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"))
104
- return class_to_call.with(args, options.except(:scope))
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))
105
120
  end
106
121
 
107
122
  def self.scope(*params, &block)
123
+ # puts "[l.#{__LINE__}] [#{name}] CREATING SCOPE CLASSES (name: #{name} | params: #{params.inspect}) ===> ::#{name}Scope#{params.first.capitalize}"
108
124
  raise "ActiveMappers [#{name}] scope must be a block" if block.nil? || !block.respond_to?(:call)
109
-
110
125
  params.each do |param|
111
126
  block_content = Ruby2Ruby.new.process(RubyParser.new.process(block.source).to_a.last)
112
127
  eval("class ::#{name}Scope#{param.capitalize} < ::#{name} ; #{block_content}; end")
@@ -129,13 +144,30 @@ module ActiveMappers
129
144
  end
130
145
 
131
146
  def self.one(resource, options = {})
147
+ # puts "[l.#{__LINE__}] [#{name}] ONE - options: #{options.inspect} - inheritance_column: #{@@inheritance_column[name]}"
132
148
  return nil unless resource
133
- if @@inheritance_column[name] && (mapper = KeyTransformer.resource_to_mapper(resource, self) rescue nil) && name != mapper&.name
134
- return mapper.with(resource, default_options.merge(options))
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
135
158
  end
136
159
 
137
160
  return {} if @@renderers[name].nil? # Mapper is empty
138
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
+
139
171
  renderers = @@renderers[name].map do |renderer|
140
172
  renderer.call(resource, options[:context])
141
173
  end.reduce(&:merge)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_mappers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michaël Villeneuve