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 +4 -4
- data/active_mappers.gemspec +1 -1
- data/lib/active_mappers/handlers/inheritance.rb +0 -1
- data/lib/active_mappers/key_transformer.rb +2 -2
- data/lib/active_mappers.rb +40 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a95ea925512e3657ad168bc4cc6afaeab5689bc9ec80102dbf49d696e414957f
|
4
|
+
data.tar.gz: 5b4a775f480aa38b2ef0a83d16096c157467d1e59e5a76b004114a3c589fbe51
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf6455490ce818e13fe29d088c05e277223a44fbc84bc3e60496300f17acb5be7d03a321783235f5b14ce1e5d71aa817132e2f40f4b389e52a2330ceee8f0079
|
7
|
+
data.tar.gz: 4635133599db4ac2d6da88da02c6bdd2de64cd521a45f0c270a6e943630a6ce7cf13a3bbf9cf2c8337d199e7d96de3e006c9c4a09a493a97729d0a514fdb6692
|
data/active_mappers.gemspec
CHANGED
@@ -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
@@ -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
|
-
|
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
|
-
|
104
|
-
|
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
|
-
|
134
|
-
|
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)
|