class Dugway::Theme

Constants

REQUIRED_FILES

Attributes

errors[R]

Public Class Methods

new(overridden_customization={}) click to toggle source
# File lib/dugway/theme.rb, line 14
def initialize(overridden_customization={})
  @overridden_customization = overridden_customization.stringify_keys
end

Public Instance Methods

build_file(name) click to toggle source
# File lib/dugway/theme.rb, line 71
def build_file(name)
  @building = true
  file_content(name)
end
customization() click to toggle source
# File lib/dugway/theme.rb, line 38
def customization
  Hash.new.tap do |customization|
    %w( fonts colors options images image_sets ).each do |type|
      customization.update(customization_for_type(type))
    end

    customization.update(@overridden_customization)
  end
end
file_content(name) click to toggle source
# File lib/dugway/theme.rb, line 56
def file_content(name)
  case name
  when 'theme.js'
    if @building
      Uglifier.new(harmony: true).compile(sprockets[name].to_s)
    else
      sprockets[name].to_s
    end
  when 'theme.css'
    sprockets[name].to_s
  else
    read_source_file(name)
  end
end
files() click to toggle source
# File lib/dugway/theme.rb, line 76
def files
  REQUIRED_FILES + image_files + font_files
end
font_files() click to toggle source
# File lib/dugway/theme.rb, line 86
def font_files
  Dir.glob(File.join(source_dir, 'fonts', '**', '*.{eot,ttf,otf,woff,svg}')).map do |i|
    i.gsub(source_dir, '')[1..-1]
  end
end
fonts() click to toggle source
# File lib/dugway/theme.rb, line 26
def fonts
  customization_for_type('fonts')
end
image_files() click to toggle source
# File lib/dugway/theme.rb, line 80
def image_files
  Dir.glob(File.join(source_dir, 'images', '**', '*.{png,jpg,jpeg,gif,ico,svg}')).map do |i|
    i.gsub(source_dir, '')[1..-1]
  end
end
image_sets() click to toggle source
# File lib/dugway/theme.rb, line 34
def image_sets
  customization_for_type('image_sets')
end
images() click to toggle source
# File lib/dugway/theme.rb, line 30
def images
  customization_for_type('images')
end
layout() click to toggle source
# File lib/dugway/theme.rb, line 18
def layout
  read_source_file('layout.html')
end
name() click to toggle source
# File lib/dugway/theme.rb, line 48
def name
  settings['name']
end
settings() click to toggle source
# File lib/dugway/theme.rb, line 22
def settings
  JSON.parse(read_source_file('settings.json'))
end
valid?() click to toggle source
# File lib/dugway/theme.rb, line 92
def valid?
  @errors = []

  REQUIRED_FILES.each do |file|
    @errors << "Missing source/#{ file }" if read_source_file(file).nil?
  end

  @errors << 'Missing theme name in source/settings.json' if name.blank?
  @errors << 'Invalid theme version in source/settings.json (ex: 1.0.3)' unless !!(version =~ /\d+\.\d+\.\d+/)
  @errors << 'Missing images in source/images' if image_files.empty?

  @errors.empty?
end
version() click to toggle source
# File lib/dugway/theme.rb, line 52
def version
  settings['version']
end

Private Instance Methods

customization_for_type(type) click to toggle source
# File lib/dugway/theme.rb, line 137
def customization_for_type(type)
  Hash.new.tap do |hash|
    if settings.has_key?(type)
      case type
      when 'images'
        settings[type].each do |setting|
          if name = setting['default']
            hash[setting['variable']] = { :url => image_path_from_setting_name(name), :width => 1, :height => 1 }
          end
        end
      when 'image_sets'
        settings[type].each do |setting|
          if defaults = setting['default'] || setting['defaults']
            hash[setting['variable']] ||= []
            defaults.each do |name|
              hash[setting['variable']] << { :url => image_path_from_setting_name(name), :width => 1, :height => 1 }
            end
          end
        end
      else
        settings[type].each do |setting|
          hash[setting['variable']] = setting['default']
        end
      end
    end
  end
end
image_path_from_setting_name(name) click to toggle source
# File lib/dugway/theme.rb, line 165
def image_path_from_setting_name(name)
  image_files.detect { |path| path =~ /#{ Regexp.escape(name) }$/ }
end
read_source_file(file_name) click to toggle source
# File lib/dugway/theme.rb, line 127
def read_source_file(file_name)
  file_path = File.join(source_dir, file_name)

  if File.exist?(file_path)
    File.read(file_path)
  else
    nil
  end
end
source_dir() click to toggle source
# File lib/dugway/theme.rb, line 108
def source_dir
  Dugway.source_dir
end
sprockets() click to toggle source
# File lib/dugway/theme.rb, line 112
def sprockets
  @sprockets ||= begin
    sprockets = Sprockets::Environment.new
    sprockets.append_path source_dir

    Sprockets::Sass.options[:line_comments] = false

    sprockets.register_preprocessor 'text/css', :liquifier do |context, data|
      @building ? data : Liquifier.render_styles(data)
    end

    sprockets
  end
end