Sha256: cb56b5e3923dfe614c68e5c706689401ee10bfd5761aee982ae23d386499e83d
Contents?: true
Size: 1.35 KB
Versions: 1
Compression:
Stored size: 1.35 KB
Contents
# frozen_string_literal: true module Measured::Cache class Json attr_reader :filename, :path def initialize(filename) @filename = filename @path = Pathname.new(File.join(File.dirname(__FILE__), "../../../cache", @filename)).cleanpath end def exist? File.exist?(@path) end def read return unless exist? decode(JSON.load(File.read(@path), nil, freeze: true)) end def write(table) raise ArgumentError, "Cannot overwrite file cache at runtime." end private # JSON dump and load of Rational objects exists, but it changes the behaviour of JSON globally if required. # Instead, the same marshalling technique is rewritten here to prevent changing this behaviour project wide. # https://github.com/ruby/ruby/blob/trunk/ext/json/lib/json/add/rational.rb def encode(table) table.each_with_object(table.dup) do |(k1, v1), accu| v1.each do |k2, v2| if v2.is_a?(Rational) accu[k1][k2] = { "numerator" => v2.numerator, "denominator" => v2.denominator } end end end end def decode(table) table.each_with_object(table.dup) do |(k1, v1), accu| v1.each do |k2, v2| if v2.is_a?(Hash) accu[k1][k2] = Rational(v2["numerator"], v2["denominator"]) end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
measured-2.7.0 | lib/measured/cache/json.rb |