Class: ThemeCheck::Template

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relative_path, storage) ⇒ Template

Returns a new instance of Template.



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

def initialize(relative_path, storage)
  @storage = storage
  @relative_path = relative_path
end

Class Method Details

.parse(source) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/theme_check/template.rb', line 96

def self.parse(source)
  Liquid::Template.parse(
    source,
    line_numbers: true,
    error_mode: :warn,
  )
end

Instance Method Details

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



91
92
93
# File 'lib/theme_check/template.rb', line 91

def ==(other)
  other.is_a?(Template) && relative_path == other.relative_path
end

#excerpt(line) ⇒ Object



66
67
68
# File 'lib/theme_check/template.rb', line 66

def excerpt(line)
  lines[line - 1].strip
end

#full_line(line) ⇒ Object



75
76
77
# File 'lib/theme_check/template.rb', line 75

def full_line(line)
  lines[line - 1]
end

#json?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/theme_check/template.rb', line 35

def json?
  false
end

#linesObject



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

def lines
  # Retain trailing newline character
  @lines ||= source.split("\n", -1)
end

#liquid?Boolean

Returns:

  • (Boolean)


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

def liquid?
  true
end

#nameObject



31
32
33
# File 'lib/theme_check/template.rb', line 31

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

#parseObject



79
80
81
# File 'lib/theme_check/template.rb', line 79

def parse
  @ast ||= self.class.parse(source)
end

#pathObject



11
12
13
# File 'lib/theme_check/template.rb', line 11

def path
  @storage.path(@relative_path)
end

#relative_pathObject



15
16
17
# File 'lib/theme_check/template.rb', line 15

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

#rootObject



87
88
89
# File 'lib/theme_check/template.rb', line 87

def root
  parse.root
end

#section?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/theme_check/template.rb', line 47

def section?
  name.start_with?('sections')
end

#snippet?Boolean

Returns:

  • (Boolean)


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

def snippet?
  name.start_with?('snippets')
end

#sourceObject



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

def source
  @source ||= @storage.read(@relative_path)
end

#source_excerpt(line) ⇒ Object



70
71
72
73
# File 'lib/theme_check/template.rb', line 70

def source_excerpt(line)
  original_lines = source.split("\n")
  original_lines[line - 1].strip
end

#template?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/theme_check/template.rb', line 43

def template?
  name.start_with?('templates')
end

#updated_contentObject

Not entirely obvious but lines is mutable, corrections are to be applied on @lines.



62
63
64
# File 'lib/theme_check/template.rb', line 62

def updated_content
  lines.join("\n")
end

#warningsObject



83
84
85
# File 'lib/theme_check/template.rb', line 83

def warnings
  @ast.warnings
end

#writeObject



23
24
25
26
27
28
29
# File 'lib/theme_check/template.rb', line 23

def write
  content = updated_content
  if source != content
    @storage.write(@relative_path, content)
    @source = content
  end
end