Sha256: 3c87343dd7cd845fd0d605775b5e04b596c75d689a1c1a75a1790147ad9bc14e

Contents?: true

Size: 1.67 KB

Versions: 17

Compression:

Stored size: 1.67 KB

Contents

require 'yaml'

module Dugway
  class ThemeFont < Struct.new(:name, :family, :collection)
    class << self
      def all
        @@all ||= Array.new.tap { |fonts|
          source.each_pair { |collection, collection_fonts|
            collection_fonts.values.each { |font|
              fonts << ThemeFont.new(font[:name], font[:family], collection)
            }
          }
        }.sort_by { |font| font.name }
      end

      def options
        @@options ||= all.map(&:name)
      end

      def find_by_name(name)
        all.find { |font|
          name.downcase == font.name.downcase
        }
      end

      def find_family_by_name(name)
        if font = find_by_name(name)
          font.family
        else
          name
        end
      end

      def google_font_names
        @@google_font_names ||= all.select { |font| 
          font.collection == 'google' 
        }.map(&:name)
      end

      def google_font_url_for_fonts(fonts)
        "//fonts.googleapis.com/css?family=#{ fonts.map { |font| font.gsub(' ', '+') }.join('|') }"
      end

      def google_font_url_for_all_fonts
        google_font_url_for_fonts(google_font_names)
      end

      def google_font_url_for_theme
        theme = Dugway.theme
        fonts = theme.fonts.keys.map { |key|
          theme.customization[key] }.select { |font_name|
            google_font_names.include? font_name }.sort

        if fonts.present?
          google_font_url_for_fonts(fonts)
        else
          nil
        end
      end

      private

      def source
        @@source ||= HashWithIndifferentAccess.new(YAML.load(File.read(File.join(File.dirname(__FILE__), 'data', 'theme_fonts.yml'))))
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
dugway-0.6.7 lib/dugway/theme_font.rb
dugway-0.6.6 lib/dugway/theme_font.rb
dugway-0.6.5 lib/dugway/theme_font.rb
dugway-0.6.4 lib/dugway/theme_font.rb
dugway-0.6.3 lib/dugway/theme_font.rb
dugway-0.6.2 lib/dugway/theme_font.rb
dugway-0.6.1 lib/dugway/theme_font.rb
dugway-0.6.0 lib/dugway/theme_font.rb
dugway-0.5.9 lib/dugway/theme_font.rb
dugway-0.5.8 lib/dugway/theme_font.rb
dugway-0.5.7 lib/dugway/theme_font.rb
dugway-0.5.6 lib/dugway/theme_font.rb
dugway-0.5.4 lib/dugway/theme_font.rb
dugway-0.5.3 lib/dugway/theme_font.rb
dugway-0.5.2 lib/dugway/theme_font.rb
dugway-0.5.1 lib/dugway/theme_font.rb
dugway-0.5.0 lib/dugway/theme_font.rb