Module: Risky::SecondaryIndexes::ClassMethods

Defined in:
lib/risky/secondary_indexes.rb

Instance Method Summary collapse

Instance Method Details

#create(key, values = {}, indexes2i = {}, opts = {}) ⇒ Object



78
79
80
81
# File 'lib/risky/secondary_indexes.rb', line 78

def create(key, values = {}, indexes2i = {}, opts = {})
  obj = new key, values, indexes2i
  obj.save(opts)
end

#find_all_by_index(index2i, value) ⇒ Object



54
55
56
57
58
59
# File 'lib/risky/secondary_indexes.rb', line 54

def find_all_by_index(index2i, value)
  index = "#{index2i}_#{indexes2i[index2i.to_s][:type]}"
  keys = bucket.get_index(index, value)

  find_all_by_key(keys)
end

#find_all_keys_by_index(index2i, value) ⇒ Object



61
62
63
64
# File 'lib/risky/secondary_indexes.rb', line 61

def find_all_keys_by_index(index2i, value)
  index = "#{index2i}_#{indexes2i[index2i.to_s][:type]}"
  bucket.get_index(index, value)
end

#find_by_index(index2i, value) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/risky/secondary_indexes.rb', line 46

def find_by_index(index2i, value)
  index = "#{index2i}_#{indexes2i[index2i.to_s][:type]}"
  key = bucket.get_index(index, value).first
  return nil if key.nil?

  find(key)
end

#index2i(name, opts = {}) ⇒ Object

Add a new secondary index to this model. Default option is :type => :int, can also be :bin Default option is :multi => false, can also be true Option :map can be used to map the index to a model (see map_model).

This assumes by default that the index name ends in _id (use :map => true)
If it ends in something else, use :map => '_suffix'


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/risky/secondary_indexes.rb', line 14

def index2i(name, opts = {})
  name = name.to_s

  opts.replace({:type => :int, :multi => false, :finder => :find}.merge(opts))
  indexes2i[name] = opts

  class_eval %Q{
    def #{name}
      @indexes2i['#{name}']
    end

    def #{name}=(value)
      @indexes2i['#{name}'] = value
    end
  }

  if opts[:map]
    if opts[:map] === true # assume that it ends in _id
      model_name = name[0..-4]
      map_model(model_name, opts)
    else
      model_name = name[0..-(opts[:map].length + 1)]
      map_model(model_name, opts.merge(:suffix => opts[:map]))
    end
  end
end

#indexes2iObject

A list of all secondary indexes



42
43
44
# File 'lib/risky/secondary_indexes.rb', line 42

def indexes2i
  @indexes2i ||= {}
end

#map_model(model_name, opts = {}) ⇒ Object

The map_model method is a convenience method to map the model_id to getters and setters. The assumption is that you have a value or index2i for model_id. The default suffix is ‘_id’, so map_model :promotion implies that promotion_id is the index2i.

For example, map_model :promotion will create these three methods “‘ruby def promotion

@promotion ||= Promotion.find_by_id promotion_id

end

def promotion=(value)

@promotion = promotion
self.promotion_id = value.nil? ? nil : value.id

end

def promotion_id=(value)

@promotion = nil if self.promotion_id != value
indexes2i['promotion_id'] = value

end “‘



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/risky/secondary_indexes.rb', line 103

def map_model(model_name, opts = {})
  model_name = model_name.to_s
  class_name = Risky::Inflector.classify(model_name)

  opts.replace({:type => :index2i, :suffix => '_id'}.merge(opts))

  class_eval %Q{
    def #{model_name}
      @#{model_name} ||= #{class_name}.#{opts[:finder]} #{model_name}#{opts[:suffix]}
    end

    def #{model_name}=(value)
      @#{model_name} = value
      self.#{model_name}#{opts[:suffix]} = value.nil? ? nil : value.id
    end

    def #{model_name}_id=(value)
      @#{model_name} = nil if self.#{model_name}_id != value
      indexes2i['#{model_name}#{opts[:suffix]}'] = value
    end
  }
end

#paginate_by_index(index2i, value, opts = {}) ⇒ Object



66
67
68
69
# File 'lib/risky/secondary_indexes.rb', line 66

def paginate_by_index(index2i, value, opts = {})
  keys = paginate_keys_by_index(index2i, value, opts)
  Risky::PaginatedCollection.new(find_all_by_key(keys), keys)
end

#paginate_keys_by_index(index2i, value, opts = {}) ⇒ Object



71
72
73
74
75
76
# File 'lib/risky/secondary_indexes.rb', line 71

def paginate_keys_by_index(index2i, value, opts = {})
  opts.reject! { |k, v| v.nil? }

  index = "#{index2i}_#{indexes2i[index2i.to_s][:type]}"
  bucket.get_index(index, value, opts)
end