module Risky::SecondaryIndexes::ClassMethods
Public Instance Methods
create(key, values = {}, indexes2i = {}, opts = {})
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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 = {})
click to toggle source
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'
# 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
indexes2i()
click to toggle source
A list of all secondary indexes
# File lib/risky/secondary_indexes.rb, line 42 def indexes2i @indexes2i ||= {} end
map_model(model_name, opts = {})
click to toggle source
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 “‘
# 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 = {})
click to toggle source
# 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 = {})
click to toggle source
# 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