Class: BigcommerceTool::Template

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_path, missing_template_path, global_yaml_path, global_files_path, language_yaml_path, page_yaml_path, discount_yaml_path) ⇒ Template

Returns a new instance of Template.



5
6
7
8
9
10
11
12
# File 'lib/bigcommerce_tool/template.rb', line 5

def initialize(template_path, missing_template_path, global_yaml_path, global_files_path, language_yaml_path, page_yaml_path, discount_yaml_path)
  self.global = Global.new(global_yaml_path, global_files_path)
  self.language = Language.new(language_yaml_path)
  self.page = Page.new(page_yaml_path)
  self.discount = Discount.new(discount_yaml_path)
  @template_path = template_path
  @missing_template_path = missing_template_path
end

Instance Attribute Details

#discountObject

Returns the value of attribute discount.



3
4
5
# File 'lib/bigcommerce_tool/template.rb', line 3

def discount
  @discount
end

#globalObject

Returns the value of attribute global.



3
4
5
# File 'lib/bigcommerce_tool/template.rb', line 3

def global
  @global
end

#languageObject

Returns the value of attribute language.



3
4
5
# File 'lib/bigcommerce_tool/template.rb', line 3

def language
  @language
end

#pageObject

Returns the value of attribute page.



3
4
5
# File 'lib/bigcommerce_tool/template.rb', line 3

def page
  @page
end

Instance Method Details

#cms(page_name) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bigcommerce_tool/template.rb', line 80

def cms(page_name)
  template = YAML::load(File.open(File.join(Dir.pwd, 'config', 'cms.yml')))[page_name]
  if template && template['template']
    self.global.merge_additional_options(
      'PageTitle' => template['title'],
      'PageContent' => load_file(File.join(Dir.pwd, 'config', 'cms', "#{page_name}.html"))
    )
    render(template['template'])
  else
    "missing: #{template.inspect}"
  end
end

#extract_variables(string) ⇒ Object



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

def extract_variables(string)
  string.scan(/%%([A-Za-z0-9\.\-_\/]+)%%/).flatten
end

#get_panel(panel_name) ⇒ Object



32
33
34
35
# File 'lib/bigcommerce_tool/template.rb', line 32

def get_panel(panel_name)
  return '' if ['ProductVideos.html'].include?(panel_name)
  load_current_or_backup(panel_name, 'Panels')
end

#get_snippet(snippet_name) ⇒ Object



37
38
39
40
# File 'lib/bigcommerce_tool/template.rb', line 37

def get_snippet(snippet_name)
  return '' if ['ProductAddToCartBelow.html'].include?(snippet_name)
  load_current_or_backup(snippet_name, 'Snippets')
end

#label_element(str, name) ⇒ Object



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

def label_element(str, name)
  "<!-- start #{name} -->\n#{str}\n<!-- end #{name} -->"
  str
end

#load_current_or_backup(name, type) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bigcommerce_tool/template.rb', line 19

def load_current_or_backup(name, type)
  path = File.join(@template_path, type, name)
  config_path = File.join(@missing_template_path, type, name)
  if File.exists?(path)
    load_file(path)
  elsif File.exists?(config_path)
    load_file(config_path)
  else
    puts " --- missing (#{config_path}): #{name} - #{type}"
    label_element('', "(#{type} - #{name})")
  end
end

#load_file(file_path) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/bigcommerce_tool/template.rb', line 71

def load_file(file_path)
  if File.exists?(file_path)
    process_file(File.open(file_path, "rb").read)
  else
    puts "missing file: #{file_path}"
    ''
  end
end

#process_file(string) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bigcommerce_tool/template.rb', line 42

def process_file(string)
  extract_variables(string).each do |variable|
    rendered =  if variable.match(/Panel\.(.+)/)
                  get_panel("#{$1}.html")
                elsif variable.match(/SNIPPET_(.+)/)
                  get_snippet("#{$1}.html")
                elsif variable.match(/ASSET_(.+)/)
                  "/#{$1}"
                elsif variable.match(/GLOBAL_([\w_]+)/)
                  self.global.lookup($1)
                elsif variable.match(/LNG_([\w_]+)/)
                  self.language.lookup($1)
                elsif variable.match(/Discount\.(.+)/)
                  self.discount.lookup($1)
                elsif variable.match(/Page\.(.+)/)
                  self.page.lookup($1)
                else
                  puts " --- missing: #{variable}"
                  ''
                end
    string.gsub!("%%#{variable}%%", rendered.to_s)
  end
  string
end

#render(file_path) ⇒ Object



93
94
95
# File 'lib/bigcommerce_tool/template.rb', line 93

def render(file_path)
  load_file(File.join(@template_path, file_path))
end