Sha256: 07496a76e05df9baacf81dee10413e149703a5a0a343390e94ad7939d17bdffe
Contents?: true
Size: 852 Bytes
Versions: 8
Compression:
Stored size: 852 Bytes
Contents
class MockRecord IDS = Hash.new { |h, k| h[k] = 0 } QUERY_COUNTS = Hash.new { |h, k| h[k] = 0 } INSTANCES = Hash.new { |h, k| h[k] = {} } attr_reader :id def initialize(attrs = {}) attrs.each_pair do |name, value| send(:"#{name}=", value) end @id = IDS[self.class.name.to_sym] += 1 INSTANCES[self.class.name.to_sym][@id] = self end def self.inherited(base) base.extend(ClassMethods) end module ClassMethods def get(id) QUERY_COUNTS[self.name.to_sym] += 1 get_instance(id) end def get_all(ids) QUERY_COUNTS[self.name.to_sym] += 1 ids.map { |id| get_instance(id) }.sort_by { |instance| instance.id } end def query_count QUERY_COUNTS[self.name.to_sym] end private def get_instance(id) INSTANCES[self.name.to_sym][id] end end end
Version data entries
8 entries across 8 versions & 3 rubygems