class BigcommerceTool::Template

Attributes

discount[RW]
global[RW]
language[RW]
page[RW]

Public Class Methods

new(template_path, missing_template_path, global_yaml_path, global_files_path, language_yaml_path, page_yaml_path, discount_yaml_path) click to toggle source
# 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

Public Instance Methods

cms(page_name) click to toggle source
# 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) click to toggle source
# File lib/bigcommerce_tool/template.rb, line 67
def extract_variables(string)
  string.scan(/%%([A-Za-z0-9\.\-_\/]+)%%/).flatten
end
get_panel(panel_name) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/bigcommerce_tool/template.rb, line 93
def render(file_path)
  load_file(File.join(@template_path, file_path))
end