Class: ThemeCheck::ThemeFile

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

Direct Known Subclasses

AssetFile, JsonFile, LiquidFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relative_path, storage) ⇒ ThemeFile

Returns a new instance of ThemeFile.



8
9
10
11
12
13
14
# File 'lib/theme_check/theme_file.rb', line 8

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

Instance Attribute Details

#storageObject (readonly)

Returns the value of attribute storage.



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

def storage
  @storage
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

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



59
60
61
# File 'lib/theme_check/theme_file.rb', line 59

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

#json?Boolean

Returns:

  • (Boolean)


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

def json?
  false
end

#liquid?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/theme_check/theme_file.rb', line 55

def liquid?
  false
end

#nameObject



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

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

#pathObject



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

def path
  @storage.path(@relative_path)
end

#relative_pathObject



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

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

#sourceObject

For the corrector to work properly, we should have a simple mental model of the internal representation of eol characters (Windows uses rn, 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 rn in it and we’ll make sure that the file we write has the same eol as the source file.



40
41
42
43
44
45
46
47
48
49
# File 'lib/theme_check/theme_file.rb', line 40

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