Module: Redis::Objects::RMap::ClassMethods

Defined in:
lib/redis/objects/rmap.rb

Instance Method Summary collapse

Instance Method Details

#has_rmap(title = nil, id = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/redis/objects/rmap.rb', line 24

def has_rmap(title=nil, id=nil)
  if title.is_a?(Hash) && title.length > 1
    id = {title.keys[1] => title.values[1]}
  end

  options = {
    :title => title,
    :id    => id || :id
  }.with_indifferent_access

  raise "title can not be blank" if options[:title].blank?

  self.rmap_options = {}.with_indifferent_access

  %w(id title).each do |x|
    if options[x].is_a?(Hash)
      self.rmap_options["#{x}_field"] = options[x].keys[0].to_sym
      self.rmap_options["#{x}_proc"]  = options[x].values[0]
    else
      self.rmap_options["#{x}_field"] = options[x].to_sym
      self.rmap_options["#{x}_proc"]  = nil
    end
  end
end

#rmapObject



70
71
72
# File 'lib/redis/objects/rmap.rb', line 70

def rmap
  rmap_storage.value || rmap_cache
end

#rmap_cacheObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/redis/objects/rmap.rb', line 49

def rmap_cache
  models = self.select([rmap_options[:id_field], rmap_options[:title_field]]).map do |a|
    data = []

    %w(id title).each do |x|
      value = a.send(rmap_options["#{x}_field"])
      value = rmap_options["#{x}_proc"].call(value) unless rmap_options["#{x}_proc"].blank?

      data << value
    end

    data
  end

  self.rmap_storage = Hash[*models.flatten]
end

#rmap_clearObject



66
67
68
# File 'lib/redis/objects/rmap.rb', line 66

def rmap_clear
  self.rmap_storage = nil
end