active_mappers 1.5.0 → 1.5.2

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: 3667d085d34c9dc82b81c74d86ee0d71586582b2b52072e22c0ef79b2cf12bfa
4
- data.tar.gz: 2062f976d9bd10e73020fedeab83fcc468a14685e713fbd44e0b927e160872b7
3
+ metadata.gz: a95ea925512e3657ad168bc4cc6afaeab5689bc9ec80102dbf49d696e414957f
4
+ data.tar.gz: 5b4a775f480aa38b2ef0a83d16096c157467d1e59e5a76b004114a3c589fbe51
5
5
  SHA512:
6
- metadata.gz: 91225abdb07c262dedcd047a97ea95a8ef132519591880decde3525311d9a1cba65e5f3b33542cf826dfa5ddc639b2744dfbd08dcaa4e185cddd0aaf1c80a5f6
7
- data.tar.gz: 0dcaa09aed08f57a9026c4ac6887315c39664959573d76ca95ef81c6c7b17c7f141deb73742debcbd0a972fce9b065e8123bf0587597db59b49bca7eb14a1383
6
+ metadata.gz: bf6455490ce818e13fe29d088c05e277223a44fbc84bc3e60496300f17acb5be7d03a321783235f5b14ce1e5d71aa817132e2f40f4b389e52a2330ceee8f0079
7
+ data.tar.gz: 4635133599db4ac2d6da88da02c6bdd2de64cd521a45f0c270a6e943630a6ce7cf13a3bbf9cf2c8337d199e7d96de3e006c9c4a09a493a97729d0a514fdb6692
@@ -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.2'
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,26 @@ 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
+ options.delete :scope
114
+ self
115
+ else
116
+ raise("ActiveMappers [#{name}] No scope named #{options[:scope]} found")
117
+ end
118
+ end
119
+ # puts "[l.#{__LINE__}] evaluate_scopes class_to_call -> #{class_to_call}"
120
+ return class_to_call.with(args, options.merge(initial_mapper: self))
105
121
  end
106
122
 
107
123
  def self.scope(*params, &block)
124
+ # puts "[l.#{__LINE__}] [#{name}] CREATING SCOPE CLASSES (name: #{name} | params: #{params.inspect}) ===> ::#{name}Scope#{params.first.capitalize}"
108
125
  raise "ActiveMappers [#{name}] scope must be a block" if block.nil? || !block.respond_to?(:call)
109
-
110
126
  params.each do |param|
111
127
  block_content = Ruby2Ruby.new.process(RubyParser.new.process(block.source).to_a.last)
112
128
  eval("class ::#{name}Scope#{param.capitalize} < ::#{name} ; #{block_content}; end")
@@ -129,13 +145,30 @@ module ActiveMappers
129
145
  end
130
146
 
131
147
  def self.one(resource, options = {})
148
+ # puts "[l.#{__LINE__}] [#{name}] ONE - options: #{options.inspect} - inheritance_column: #{@@inheritance_column[name]}"
132
149
  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))
150
+
151
+ if @@inheritance_column[name] && !options[:fallback_class]
152
+ main_mapper = KeyTransformer.resource_to_mapper(resource, self)
153
+ # puts "[l.#{__LINE__}] [#{name}] ONE - main_mapper #{main_mapper}"
154
+ mapper = options[:scope] ? (KeyTransformer.resource_to_mapper(resource, self, options[:scope]) rescue main_mapper) : main_mapper
155
+ # puts "[l.#{__LINE__}] [#{name}] ONE - mapper #{mapper}"
156
+ if name != mapper&.name
157
+ return mapper.with(resource, options.merge(rootless: true, fallback_class: options[:initial_mapper]))
158
+ end
135
159
  end
136
160
 
137
161
  return {} if @@renderers[name].nil? # Mapper is empty
138
162
 
163
+ # base_mapper = name.rpartition('::').first
164
+
165
+ # renderers = ancestors.select { |it| it.name.start_with?(base_mapper) }.map do |ancestor|
166
+ # puts "---> #{ancestor.name}"
167
+ # @@renderers[ancestor.name].map do |renderer|
168
+ # renderer.call(resource, options[:context])
169
+ # end.reduce(&:merge)
170
+ # end.reduce(&:merge)
171
+
139
172
  renderers = @@renderers[name].map do |renderer|
140
173
  renderer.call(resource, options[:context])
141
174
  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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michaël Villeneuve