Class: ThemeCheck::ThemeFile

Inherits:
Object
  • Object
show all
Defined in:
lib/theme_check/theme_file.rb

Direct Known Subclasses

AssetFile, JsonFile, LiquidFile

Instance Method Summary collapse

Constructor Details

#initialize(relative_path, storage) ⇒ ThemeFile

Returns a new instance of ThemeFile.



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

def initialize(relative_path, storage)
  @relative_path = relative_path
  @storage = storage
  @source = nil
  @eol = "\n"
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



52
53
54
# File 'lib/theme_check/theme_file.rb', line 52

def ==(other)
  other.is_a?(self.class) && relative_path == other.relative_path
end

#json? ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/theme_check/theme_file.rb', line 44

def json?
  false
end

#liquid? ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/theme_check/theme_file.rb', line 48

def liquid?
  false
end

#name ⇒ Object



21
22
23
# File 'lib/theme_check/theme_file.rb', line 21

def name
  relative_path.sub_ext('').to_s
end

#path ⇒ Object



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

def path
  @storage.path(@relative_path)
end

#relative_path ⇒ Object



17
18
19
# File 'lib/theme_check/theme_file.rb', line 17

def relative_path
  @relative_pathname ||= Pathname.new(@relative_path)
end

#source ⇒ Object

For the corrector to work properly, we should have a simple mental model of the internal representation of eol characters (Windows uses \r\n, Linux uses \n).

Parser::Source::Buffer strips the \r from the source file, so if you are autocorrecting the file you might lose that info and cause a git diff. It also makes the node.start_index/end_index calculation break. That's not cool.

So in here we track whether the source file has \r\n in it and we'll make sure that the file we write has the same eol as the source file.



37
38
39
40
41
42
# File 'lib/theme_check/theme_file.rb', line 37

def source
  return @source if @source
  @source = @storage.read(@relative_path)
  @eol = @source.include?("\r\n") ? "\r\n" : "\n"
  @source = @source.gsub("\r\n", "\n")
end