Class: Spider::Model::Mappers::DocumentMapper

Inherits:
Spider::Model::Mapper show all
Includes:
Storage::Document
Defined in:
lib/spiderfw/model/mappers/document_mapper.rb

Instance Attribute Summary

Attributes inherited from Spider::Model::Mapper

#model, #storage, #type

Instance Method Summary (collapse)

Methods inherited from Spider::Model::Mapper

#after_delete, #after_save, #associate_external, #association_elements, #base_type, #basic_preprocess, #before_delete, #before_insert, #before_update, #bulk_update, #count, #delete, #delete_all!, #delete_element_associations, #execute_action, #expand_request, #fetch, #find, #find_with_superclass, #get_dependencies, #get_external, #initialize, #insert, #load, #load_element, #load_element!, #lock, #map_back_value, #map_condition_value, #map_elements, #map_save_value, #map_value, #mapped?, #merge_object, #no_map, #normalize, #prepare_delete_condition, #prepare_query, #prepare_query_request, #preprocess_condition, #queryset_siblings, #save, #save_all, #save_associations, #save_done, #save_element_associations, #save_integrated, #sequence_next, #set_pks_condition, #sortable?, #split_condition_polymorphs, #storage_value_to_mapper, #truncate!, #update, write?

Constructor Details

This class inherits a constructor from Spider::Model::Mapper

Instance Method Details

- (Object) assign_pk(obj)



63
64
65
66
67
68
69
70
71
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 63

def assign_pk(obj)
    if obj.is_a?(QuerySet)
        obj.each{ |o| assign_pk(o) }
        return
    end
    obj.class.elements_array.select{ |el| el.type <= Spider::DataTypes::PK }.each do |el|
        obj.set(el, storage.generate_pk) unless obj.element_has_value?(el)
    end
end

- (Object) before_save(obj, mode)



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 82

def before_save(obj, mode)
    super
    obj.class.elements_array.select{ |el| 
        el.attributes[:embedded] && !el.attributes[:extended_model] && obj.element_has_value?(el)
    }.each do |el|
        if el.multiple?
            obj.get(el).each do |o|
                o.mapper.before_save(o, mode)
            end
        else
            o = obj.get(el)
            next unless o
            o.mapper.before_save(o, mode)
        end
    end
end

- (Object) children_for_unit_of_work(obj, action)



283
284
285
286
287
288
289
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 283

def children_for_unit_of_work(obj, action)
    obj.class.elements_array.select{ |el| 
        el.model? && obj.element_has_value?(el) && !el.attributes[:embedded]  && !el.attributes[:added_reverse]
    }.map do |el|
        obj.get(el)
    end
end

- (Object) determine_save_mode(obj)



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 296

def determine_save_mode(obj)
    if @model.extended_models && !@model.extended_models.empty?
        has_pks = true
        @model.primary_keys.each do |pk|
            if pk.integrated? && pk.integrated_from.attributes[:extended_model]
                return super unless pk.integrated_from.attributes[:embedded]
                unless obj.element_has_value?(pk)
                    has_pks = false
                    break
                end
            end
        end
        return has_pks ? :update : :insert
    end
end

- (Object) do_delete(condition, force)



73
74
75
76
77
78
79
80
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 73

def do_delete(condition, force)
    query = Spider::Model::Query.new(condition)
    objs = self.find(query)
    return unless objs
    objs.each do |obj|
        storage.delete(obj.class.name, obj_to_hash(obj)) 
    end
end

- (Object) do_insert(obj)



59
60
61
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 59

def do_insert(obj)
    storage.insert(obj.class.name, obj_to_hash(obj)) 
end

- (Object) do_update(obj)



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 31

def do_update(obj)
    search_obj = obj
    emb = embedding(obj)
    path = nil
    search_obj, path = emb if emb
    condition = {}
    # search_obj.class.primary_keys.each do |pk|
    #     name = pk.name == :id ? '_id' : pk.name
    #     condition[name] = map_condition_value(pk.type, search_obj.get(pk))
    # end
    condition["_id"] = obj.keys_string
    set = {}
    document_elements(obj).select{ |el| obj.element_modified?(el) }.each do |el|
        name = el.name
        name = "#{path}.#{name}" if path
        if el.model?
            if el.attributes[:embedded]
                set[name] = obj_to_hash(obj.get(el), :embed_key => el.attributes[:embed_key])
            else
                set[name] = obj.get(el).keys_string
            end
        else
            set[name] = map_save_value(el.type, obj.get(el), :update)
        end
    end
    storage.update(search_obj.class.name, condition, set)
end

- (Boolean) document_element?(el, model = @model)

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
112
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 103

def document_element?(el, model=@model)
    return true if el.attributes[:document_element]
    return false if el.multiple? && !el.attributes[:embedded]
    return false if el.attributes[:extended_model]
    return false if el.attributes[:added_reverse]
    return false if el.attributes[:computed_from]
    return false if el.integrated? && !el.integrated_from.attributes[:extended_model]
    return true

end

- (Object) document_elements(obj)



99
100
101
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 99

def document_elements(obj)
    obj.class.elements_array.select{ |el| document_element?(el, obj.class) }
end

- (Boolean) embed?(element, val = nil)

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 114

def embed?(element, val=nil)
    return false unless element.attributes[:embedded] && element.model.embeddable?
    return false if element.integrated?
    model = nil
    if val
        model = val.is_a?(QuerySet) ? val.model : val.class
    else
        model = element.model
    end
    return false unless model.embeddable?
    
    return true
end

- (Object) embedding(obj)



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 10

def embedding(obj)
    embedder_el = obj.embedder
    return nil unless embedder_el
    first_embedder = obj.get(embedder_el)
    while first_embedder._subclass_object
        first_embedder = first_embedder._subclass_object
    end
    reverse_el = embedder_el.type.elements[embedder_el.reverse]
    path = "#{reverse_el.name}.#{obj.get(reverse_el.attributes[:embed_key])}"
    parent_obj = obj.get(embedder_el)
    # while parent_obj._subclass_object
    #     parent_obj = parent_obj._subclass_object
    # end
    parent_embedding = parent_obj.mapper.embedding(parent_obj)
    if parent_embedding
        first_embedder = parent_embedding[0]            
        path = "#{parent_embedding[0]}.#{path}"
    end
    [first_embedder, path]
end

- (Object) get_external_element(element, query, objects)



291
292
293
294
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 291

def get_external_element(element, query, objects)
    return objects if element.attributes[:embedded] || element.attributes[:extended_model]
    super
end

- (Boolean) have_references?(element)

Returns:

  • (Boolean)


184
185
186
187
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 184

def have_references?(element)
    element = @model.elements[element] unless element.is_a?(Element)
    document_element?(element)
end

- (Object) map(request, result, obj_or_model)



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 198

def map(request, result, obj_or_model)
    # FIXME: ignores request for now
    model = obj_or_model.is_a?(Class) ? obj_or_model : obj_or_model.model
    if (!request || request == true)
        request = Request.new
        model.elements_array.each{ |el| request.request(el.name) }
    end
    subclass_type_el = model.elements_array.select{ |cel| cel.attributes[:subclass_type] }.first
    if subclass_type_el && subclass_type = result[subclass_type_el.name]
        model = const_get_full(subclass_type)
    end
    embedded = {}
    embedded_keys = model.elements_array.select{ |el| embed?(el) && !el.attributes[:extended_model] }.map{ |el| el.name }
    # debugger
    # embed?(:civilia_open_persona) if $DO_DEBUG
    embedded_keys.each do |k|
        embedded[k] = result.delete(k)
    end
    data = {}
    request.keys.each do |element_name|
        element = model.elements[element_name]
        result_value = nil
        next if !element || (element.integrated? && !element.integrated_from.embedded?) || !have_references?(element)
        next if embedded[element_name]
        if element.model?
            pks = {}
            keys_str = result[element_name]
            next if keys_str.blank?
            pks_array = element.model.split_keys_string(keys_str)
            element.model.primary_keys.each_with_index do |key, i|
                key_val = pks_array[i]
                pks[key.name] = map_back_value(key.type, key_val)
            end
#                    begin
            data[element_name] = Spider::Model.get(element.model, pks, true)
#                    rescue IdentityMapperException
                # null keys, nothing to set
#                    end
        elsif !element.model?
            data[element_name] = map_back_value(element.type, result[element_name])
        end
    end
    obj = Spider::Model.get(model, data, true)
    embedded.each do |k, v|
        if !v
            obj.set_loaded_value(k, nil) if request[k]
            next
        end
        next if v.is_a?(Array) && v.empty?
        el = obj.class.elements[k]
        if el.multiple?
            vals = []
            if el.attributes[:embed_key]
                vals = v.values
            else
                vals = v
            end
            qs = QuerySet.static(el.model)
            el_model = el.model
            el_mapper = el.model.mapper
            if el.junction? && !el.attributes[:keep_junction]
                el_model = el.type
                el_mapper = el.model.mapper
            end
            vals.each do |val|
                # FIXME: pass sub request
                sub_obj = el_mapper.map(true, val, el_model)#Spider::Model.get(el_model, val, true)
                qs << sub_obj
            end
            obj.set_loaded_value(k, qs)
        else
            # FIXME: pass sub request request[el.name]
            sub_obj = el.model.mapper.map(true, v, el.model)
            obj.set_loaded_value(k, sub_obj)
        end
        
    end
    obj
end

- (Object) obj_to_hash(obj, options = {})



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 129

def obj_to_hash(obj, options={})
    h = {}
    if obj.class == QuerySet
        res = obj.map{ |obj| obj_to_hash(obj) } 
        if options[:embed_key]
            h = {}
            res.each do |val|
                h[val[options[:embed_key].to_s]] = val
            end
            return h
        end
        return res
    end
    
    document_elements(obj).each do |el|
        next if el.primary_key?
        name = el.name
        next if options[:skip] && options[:skip].include?(el.name)
        val = obj.get(el)
        sub_options = {}
        hval = if el.model? && val
            if embed?(el, val)
                if el.multiple?
                    # if el.junction?
                    #     sub_options[:skip] = [el.reverse]
                    # end
                    if el.attributes[:embed_key]
                        el_h = {}
                        val.each do |row|
                            el_h[row.get(el.attributes[:embed_key])] = row.mapper.obj_to_hash(row, sub_options)
                        end
                        el_h
                    else
                        h_mapper = val[0] && val[0].mapper.respond_to?(:obj_to_hash) ? val[0].mapper : self
                        val.map{ |row| h_mapper.obj_to_hash(row, sub_options) }
                    end
                else
                    sub_mapper = val.mapper.respond_to?(:obj_to_hash) ? val.mapper : self
                    sub_mapper.obj_to_hash(val, sub_options)
                end
            else
                val.keys_string
            end
        else
            map_save_value(el.type, val)
        end
        h[name] = hval
    end
    
    assign_pk(obj)
    h["_id"] = obj.keys_string
    
    h
end

- (Object) save_extended_models(obj, mode)



279
280
281
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 279

def save_extended_models(obj, mode)
    return
end

- (Boolean) someone_have_references?(element)

Returns:

  • (Boolean)


189
190
191
192
193
194
195
# File 'lib/spiderfw/model/mappers/document_mapper.rb', line 189

def someone_have_references?(element)
    element = @model.elements[element] unless element.is_a?(Element)
    if (element.integrated?)
        return element.model.someone_have_references?(element.attributes[:integrated_from_element])
    end
    return have_references?(element)
end