Class: ThemeCheck::JsonFile

Inherits:
ThemeFile show all
Defined in:
lib/theme_check/json_file.rb

Instance Attribute Summary

Attributes inherited from ThemeFile

#storage, #version

Instance Method Summary collapse

Methods inherited from ThemeFile

#==, #liquid?, #name, #path, #relative_path, #source

Constructor Details

#initialize(relative_path, storage) ⇒ JsonFile

Returns a new instance of JsonFile.



6
7
8
9
10
11
# File 'lib/theme_check/json_file.rb', line 6

def initialize(relative_path, storage)
  super
  @loaded = false
  @content = nil
  @parser_error = nil
end

Instance Method Details

#contentObject



13
14
15
16
# File 'lib/theme_check/json_file.rb', line 13

def content
  load!
  @content
end

#json?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/theme_check/json_file.rb', line 39

def json?
  true
end

#parse_errorObject



18
19
20
21
# File 'lib/theme_check/json_file.rb', line 18

def parse_error
  load!
  @parser_error
end

#update_contents(new_content = {}) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
# File 'lib/theme_check/json_file.rb', line 23

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

#writeObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/theme_check/json_file.rb', line 28

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