module Docks::Templates

Public Class Methods

<<(template) click to toggle source
# File lib/docks/templates.rb, line 44
def self.<<(template)
  register(template)
end
default() click to toggle source
# File lib/docks/templates.rb, line 28
def self.default; @default_template end
default=(template) click to toggle source
# File lib/docks/templates.rb, line 30
def self.default=(template); @default_template = Template.new(template) end
default_layout() click to toggle source
# File lib/docks/templates.rb, line 33
def self.default_layout; @default_layout end
default_layout=(layout) click to toggle source
# File lib/docks/templates.rb, line 34
def self.default_layout=(layout); @default_layout = layout end
demo() click to toggle source
# File lib/docks/templates.rb, line 25
def self.demo; @demo_template end
demo=(template) click to toggle source
# File lib/docks/templates.rb, line 26
def self.demo=(template); @demo_template = Template.new(template, layout: "demo") end
fallback() click to toggle source
# File lib/docks/templates.rb, line 29
def self.fallback; default end
fallback=(template) click to toggle source
# File lib/docks/templates.rb, line 31
def self.fallback=(template); self.default = template end
last_template_update() click to toggle source
# File lib/docks/templates.rb, line 73
def self.last_template_update
  @last_modified ||= begin
    templates = Dir[Docks.config.templates + "**/*.*"]
    templates.map { |template| File.mtime(template) }.sort.last
  end
end
register(template, options = {}) click to toggle source
# File lib/docks/templates.rb, line 36
def self.register(template, options = {})
  if template.kind_of?(Hash)
    register_from_hash(template)
  else
    @templates << Template.new(template, options)
  end
end
search_for_template(template, options = {}) click to toggle source
# File lib/docks/templates.rb, line 58
def self.search_for_template(template, options = {})
  return unless template.kind_of?(String) || template.kind_of?(Pathname)
  return template if File.exists?(template)

  if options[:must_be].nil?
    in_root = loose_search_for(template)
    return in_root unless in_root.nil?
  end

  in_specific = loose_search_for(File.join("#{(options[:must_be] || :partial).to_s.sub(/s$/, '')}{s,}", template))
  return in_specific unless in_specific.nil?

  raise Docks::NoTemplateError, "No #{options[:must_be] || "template"} matching '#{template}' was found. Make sure that you have a template by that name in the '#{options[:must_be].nil? ? Docks.config.asset_folders.templates : options[:must_be].to_s.pluralize}' folder of your pattern library's assets (or in a subdirectory of that folder), or provide a full path to the desired file."
end
template_for(id) click to toggle source
# File lib/docks/templates.rb, line 48
def self.template_for(id)
  id = id.name if id.kind_of?(Containers::Pattern)

 @templates.reverse_each do |template|
    return template if template.matches?(id)
  end

  fallback
end

Private Class Methods

clean() click to toggle source
# File lib/docks/templates.rb, line 104
def self.clean
  @demo_template = Template.new("demo", layout: (rails? ? "docks/demo" : "demo"))
  @default_template = Template.new("pattern")
  @default_layout = rails? ? "docks/pattern" : "pattern"
  @templates = []
end
loose_search_for(path) click to toggle source
# File lib/docks/templates.rb, line 96
def self.loose_search_for(path)
  return if path.nil?
  path = Docks.config.templates + path
  path_pieces = path.to_s.sub(File.extname(path), "").split("/")
  path_pieces[path_pieces.length - 1] = "{_,}#{path_pieces.last}"
  Dir.glob("#{path_pieces.join("/")}.*").first
end
rails?() click to toggle source
# File lib/docks/templates.rb, line 111
def self.rails?
  defined?(Rails)
end
register_from_hash(templates) click to toggle source
# File lib/docks/templates.rb, line 82
def self.register_from_hash(templates)
  if fallback = templates.delete("default") || templates.delete("fallback")
    self.fallback = fallback
  end

  if demo = templates.delete("demo")
    self.demo = demo
  end

  templates.each do |match, template|
    register(template, for: Regexp.new(match.to_s))
  end
end