Class: ThemeFont

Inherits:
Struct
  • Object
show all
Defined in:
lib/bigcartel/theme/fonts/theme_font.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#collectionObject

Returns the value of attribute collection

Returns:

  • (Object)

    the current value of collection



3
4
5
# File 'lib/bigcartel/theme/fonts/theme_font.rb', line 3

def collection
  @collection
end

#familyObject

Returns the value of attribute family

Returns:

  • (Object)

    the current value of family



3
4
5
# File 'lib/bigcartel/theme/fonts/theme_font.rb', line 3

def family
  @family
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



3
4
5
# File 'lib/bigcartel/theme/fonts/theme_font.rb', line 3

def name
  @name
end

#weightsObject

Returns the value of attribute weights

Returns:

  • (Object)

    the current value of weights



3
4
5
# File 'lib/bigcartel/theme/fonts/theme_font.rb', line 3

def weights
  @weights
end

Class Method Details

.allObject



5
6
7
8
9
10
11
12
13
# File 'lib/bigcartel/theme/fonts/theme_font.rb', line 5

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'], font['weights'], collection)
      }
    }
  }.sort_by { |font| font.name }
end

.find_by_name(name) ⇒ Object



19
20
21
22
23
# File 'lib/bigcartel/theme/fonts/theme_font.rb', line 19

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

.find_family_by_name(name) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/bigcartel/theme/fonts/theme_font.rb', line 25

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

.google_font_namesObject



39
40
41
# File 'lib/bigcartel/theme/fonts/theme_font.rb', line 39

def google_font_names
  google_fonts.map(&:name)
end

.google_font_url_for_all_fontsObject



55
56
57
# File 'lib/bigcartel/theme/fonts/theme_font.rb', line 55

def google_font_url_for_all_fonts
  google_font_url_for_fonts(google_fonts)
end

.google_font_url_for_fonts(fonts) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bigcartel/theme/fonts/theme_font.rb', line 43

def google_font_url_for_fonts(fonts)
  fonts = fonts.map do |font|
    if font.kind_of? ThemeFont
      font.weights ? "#{font.name}:#{font.weights}" : font.name
    else
      font
    end
  end

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

.google_font_url_for_theme(fonts, settings) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/bigcartel/theme/fonts/theme_font.rb', line 59

def google_font_url_for_theme(fonts, settings)
  google_fonts = fonts.keys.map { |key| settings[key] }.compact.
    map { |font_name| find_by_name font_name }.
    compact.
    select { |font| font.collection == 'google' }.
    sort_by { |font| font.name }

  google_fonts.empty? ? nil : google_font_url_for_fonts(google_fonts)
end

.google_fontsObject



33
34
35
36
37
# File 'lib/bigcartel/theme/fonts/theme_font.rb', line 33

def google_fonts
  @@google_fonts ||= all.select { |font|
    font.collection == 'google'
  }
end

.optionsObject



15
16
17
# File 'lib/bigcartel/theme/fonts/theme_font.rb', line 15

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