Sha256: 522876b1ccc8ddf5a6d60bb2b17e65ee65d61606fbe26e9bd49413d17a9ab8a2

Contents?: true

Size: 1.05 KB

Versions: 25

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

require "json"

module PlatformosCheck
  class JsonFile < AppFile
    def initialize(relative_path, storage)
      super
      @loaded = false
      @content = nil
      @parser_error = nil
    end

    def content
      load!
      @content
    end

    def parse_error
      load!
      @parser_error
    end

    def update_contents(new_content = {})
      raise ArgumentError if new_content.is_a?(String)

      @content = new_content
    end

    def write
      pretty = JSON.pretty_generate(@content)
      return unless source.rstrip != pretty.rstrip

      # Most editors add a trailing \n at the end of files. Here we
      # try to maintain the convention.
      eof = source.end_with?("\n") ? "\n" : ""
      @storage.write(@relative_path, pretty.gsub("\n", @eol) + eof)
      @source = pretty
    end

    def json?
      true
    end

    private

    def load!
      return if @loaded

      @content = JSON.parse(source)
    rescue JSON::ParserError => e
      @parser_error = e
    ensure
      @loaded = true
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
platformos-check-0.4.14 lib/platformos_check/json_file.rb
platformos-check-0.4.13 lib/platformos_check/json_file.rb
platformos-check-0.4.12 lib/platformos_check/json_file.rb
platformos-check-0.4.11 lib/platformos_check/json_file.rb
platformos-check-0.4.10 lib/platformos_check/json_file.rb
platformos-check-0.4.9 lib/platformos_check/json_file.rb
platformos-check-0.4.8 lib/platformos_check/json_file.rb
platformos-check-0.4.7 lib/platformos_check/json_file.rb
platformos-check-0.4.6 lib/platformos_check/json_file.rb
platformos-check-0.4.5 lib/platformos_check/json_file.rb
platformos-check-0.4.4 lib/platformos_check/json_file.rb
platformos-check-0.4.3 lib/platformos_check/json_file.rb
platformos-check-0.4.2 lib/platformos_check/json_file.rb
platformos-check-0.4.1 lib/platformos_check/json_file.rb
platformos-check-0.4.0 lib/platformos_check/json_file.rb
platformos-check-0.3.3 lib/platformos_check/json_file.rb
platformos-check-0.3.1 lib/platformos_check/json_file.rb
platformos-check-0.3.0 lib/platformos_check/json_file.rb
platformos-check-0.2.2 lib/platformos_check/json_file.rb
platformos-check-0.2.1 lib/platformos_check/json_file.rb