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



87
88
89
90
91
92
93
# File 'lib/theme_check/template.rb', line 87

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

Instance Method Details

#==(other) ⇒ Object



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

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

#excerpt(line) ⇒ Object



58
59
60
# File 'lib/theme_check/template.rb', line 58

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

#full_line(line) ⇒ Object



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

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

#linesObject



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

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

#nameObject



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

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

#parseObject



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

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



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

def root
  parse.root
end

#section?Boolean

Returns:

  • (Boolean)


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

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

#snippet?Boolean

Returns:

  • (Boolean)


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

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



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

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

#template?Boolean

Returns:

  • (Boolean)


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

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

#updated_contentObject

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



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

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

#warningsObject



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

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