Class: Spider::Model::Mappers::HashMapper

Inherits:
Spider::Model::Mapper show all
Defined in:
lib/spiderfw/model/mappers/hash_mapper.rb

Overview

Simple mapper for an array of hash data. Used by the InlineModel.

Instance Attribute Summary

Attributes inherited from Spider::Model::Mapper

#model, #storage, #type

Class Method Summary collapse

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_save, #before_update, #bulk_update, #children_for_unit_of_work, #count, #delete, #delete_all!, #delete_element_associations, #determine_save_mode, #do_delete, #do_insert, #do_update, #execute_action, #expand_request, #find, #find_with_superclass, #get_dependencies, #get_external, #get_external_element, #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_request, #preprocess_condition, #queryset_siblings, #save, #save_all, #save_associations, #save_done, #save_element_associations, #save_extended_models, #save_integrated, #sequence_next, #set_pks_condition, #someone_have_references?, #sortable?, #split_condition_polymorphs, #storage_value_to_mapper, #truncate!, #update

Constructor Details

#initialize(model, storage) ⇒ HashMapper

Returns a new instance of HashMapper.



9
10
11
12
# File 'lib/spiderfw/model/mappers/hash_mapper.rb', line 9

def initialize(model, storage)
    super
    @type = :hash
end

Class Method Details

.write?Boolean

False. This mapper is read-only.

Returns:

  • (Boolean)


15
16
17
# File 'lib/spiderfw/model/mappers/hash_mapper.rb', line 15

def self.write?
    false
end

Instance Method Details

#check_condition(condition, row) ⇒ Object

Checks if a row (hash) matches a Condition.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/spiderfw/model/mappers/hash_mapper.rb', line 54

def check_condition(condition, row)
    return true if condition.empty?
    condition.each_with_comparison do |key, value, comp|
        has_check = true
        test = case comp
        when '='
            row[key] == value
        when '>'
            row[key] > value
        when '<'
            row[key] < value
        when '<>'
            row[key] != value
        when 'like', 'ilike'
            check_value = row[key]
            if (comp == 'ilike')
                value.upcase!
                check_value.upcase!
            end
            test_re = Regexp.new(Regexp.quote(value).gsub('%', '.+'))
            check_value =~ test_re
        end
        if (test) 
            return true if (condition.conjunction == :or)
        else
            return false if (condition.conjunction == :and)
        end
    end
    condition.subconditions.each do |sub|
        has_check = true
        test = check_condition(sub, row)
        if (test) 
            return true if (condition.conjunction == :or)
        else
            return false if (condition.conjunction == :and)
        end
    end
    return false if (condition.conjunction == :or)
    return true
end

#fetch(query) ⇒ Object

Fetch implementation. – TODO: This is only for one-element hashes; make this a subclass of a generic hashmapper

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/spiderfw/model/mappers/hash_mapper.rb', line 27

def fetch(query)
    return [] unless @model.data && @model.data.length > 0
    primary_key = nil
    desc = nil
    alt_desc = nil
    @model.elements.each do |name, el|
        if el.primary_key?
            primary_key = el.name
        elsif el.attributes[:desc]
            desc = el.name
        else
            alt_desc ||= el.name if el.type == String
        end
    end
    desc ||= alt_desc
    raise MapperError, "Model has no primary key or no description element" unless primary_key && desc
    res =  @model.data.map{ |id, val| 
        id = id.to_s if id.is_a?(Symbol)
        {primary_key => id, desc => val} 
    }.select do |row|
        check_condition(query.condition, row)
    end
    res.extend(Spider::Model::Storage::StorageResult)
    return res
end

#have_references?(key) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/spiderfw/model/mappers/hash_mapper.rb', line 19

def have_references?(key)
    true
end

#map(request, result, obj_or_model) ⇒ Object

:nodoc:



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

def map(request, result, obj_or_model) #:nodoc:
    obj = obj_or_model.is_a?(Class) ? obj_or_model.new : obj_or_model
    request.keys.each do |element_name|
        element = @model.elements[element_name]
        next if element.model?
        result_value = result[element_name]
        obj.set_loaded_value(element, prepare_map_value(element.type, result_value))
    end
    return obj
end

#prepare_map_value(type, value) ⇒ Object

:nodoc:



114
115
116
# File 'lib/spiderfw/model/mappers/hash_mapper.rb', line 114

def prepare_map_value(type, value) #:nodoc:
    return value
end

#prepare_query(query, obj = nil) ⇒ Object

:nodoc:



95
96
97
98
99
100
101
# File 'lib/spiderfw/model/mappers/hash_mapper.rb', line 95

def prepare_query(query, obj=nil) #:nodoc:
    query = super
    @model.elements.select{ |name, element| !element.model? }.each do |name, element|
        query.request[element] = true
    end
    return query
end