Sha256: 6c187d58c3f0fb39034c62e0a9d38181add82d13711660ba766b275102754cde
Contents?: true
Size: 1.33 KB
Versions: 3
Compression:
Stored size: 1.33 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))) 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
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
measured-2.6.0 | lib/measured/cache/json.rb |
measured-2.5.2 | lib/measured/cache/json.rb |
measured-2.5.1 | lib/measured/cache/json.rb |