Sha256: ad98f6d3df9267a713dd4c5f3e6bd412fec7861d3c148ea2d9605e208c3e17b1
Contents?: true
Size: 1.27 KB
Versions: 9
Compression:
Stored size: 1.27 KB
Contents
# frozen_string_literal: true module MiniSql module Mysql class DeserializerCache DEFAULT_MAX_SIZE = 500 def initialize(max_size = nil) @cache = {} @max_size = max_size || DEFAULT_MAX_SIZE end def materialize(result, decorator_module = nil) key = result.fields.join(',') # trivial fast LRU implementation materializer = @cache.delete(key) if materializer @cache[key] = materializer else materializer = @cache[key] = new_row_materializer(result) @cache.shift if @cache.length > @max_size end if decorator_module materializer = materializer.decorated(decorator_module) end result.map do |data| materializer.materialize(data) end end private def new_row_materializer(result) fields = result.fields Class.new do extend MiniSql::Decoratable include MiniSql::Result attr_accessor(*fields) instance_eval <<~RUBY def materialize(data) r = self.new #{col = -1; fields.map { |f| "r.#{f} = data[#{col += 1}]" }.join("; ")} r end RUBY end end end end end
Version data entries
9 entries across 9 versions & 1 rubygems