Sha256: 6e21f3f7577a1b4f180ec71d24829a0cffd4d650ebaabad1c6ffbe56f80241b2

Contents?: true

Size: 1.05 KB

Versions: 20

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true
require "json"

module ThemeCheck
  class JsonFile < ThemeFile
    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)
      if 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
    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

20 entries across 20 versions & 1 rubygems

Version Path
theme-check-1.15.0 lib/theme_check/json_file.rb
theme-check-1.14.0 lib/theme_check/json_file.rb
theme-check-1.13.0 lib/theme_check/json_file.rb
theme-check-1.12.1 lib/theme_check/json_file.rb
theme-check-1.12.0 lib/theme_check/json_file.rb
theme-check-1.11.0 lib/theme_check/json_file.rb
theme-check-1.10.3 lib/theme_check/json_file.rb
theme-check-1.10.2 lib/theme_check/json_file.rb
theme-check-1.10.1 lib/theme_check/json_file.rb
theme-check-1.10.0 lib/theme_check/json_file.rb
theme-check-1.9.2 lib/theme_check/json_file.rb
theme-check-1.9.1 lib/theme_check/json_file.rb
theme-check-1.9.0 lib/theme_check/json_file.rb
theme-check-1.8.0 lib/theme_check/json_file.rb
theme-check-1.7.2 lib/theme_check/json_file.rb
theme-check-1.7.1 lib/theme_check/json_file.rb
theme-check-1.7.0 lib/theme_check/json_file.rb
theme-check-1.6.2 lib/theme_check/json_file.rb
theme-check-1.6.1 lib/theme_check/json_file.rb
theme-check-1.6.0 lib/theme_check/json_file.rb