Class: Rack::Fontserve::Font
- Inherits:
-
Object
- Object
- Rack::Fontserve::Font
- Defined in:
- lib/rack-fontserve/font.rb
Overview
Representation of fonts in the Rack::Fontserve.fonts_path, including various helpers to check and retrieve formats, license and custom css
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.all ⇒ Object
Returns an array of all present and valid fonts.
Instance Method Summary collapse
-
#custom_css ⇒ Object
Returns the content of the custom CSS file if present, nil otherwise.
-
#custom_css? ⇒ Boolean
custom css file exists?.
-
#format_path(format) ⇒ Object
Returns the path to the font file in given format.
-
#formats ⇒ Object
Retruns a list of available formats.
-
#initialize(name) ⇒ Font
constructor
A new instance of Font.
-
#license ⇒ Object
Returns the content of the LICENSE file if present, nil otherwise.
-
#license? ⇒ Boolean
LICENSE file exists?.
-
#path ⇒ Object
Returns the base path of this font.
Constructor Details
#initialize(name) ⇒ Font
Returns a new instance of Font.
17 18 19 20 |
# File 'lib/rack-fontserve/font.rb', line 17 def initialize(name) @name = File.basename(name) # Ensure we remove any path sections that might get burned into here raise Rack::Fontserve::InvalidFontError unless valid? end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/rack-fontserve/font.rb', line 4 def name @name end |
Class Method Details
.all ⇒ Object
Returns an array of all present and valid fonts
7 8 9 10 11 12 13 14 15 |
# File 'lib/rack-fontserve/font.rb', line 7 def self.all Dir[File.join(Rack::Fontserve.fonts_path.to_s, '*')].map do |path| begin new(File.basename(path)) rescue Rack::Fontserve::InvalidFontError nil end end.compact end |
Instance Method Details
#custom_css ⇒ Object
Returns the content of the custom CSS file if present, nil otherwise
44 45 46 |
# File 'lib/rack-fontserve/font.rb', line 44 def custom_css File.read file_path("#{name}.css") if custom_css? end |
#custom_css? ⇒ Boolean
custom css file exists?
39 40 41 |
# File 'lib/rack-fontserve/font.rb', line 39 def custom_css? File.exist? file_path("#{name}.css") end |
#format_path(format) ⇒ Object
Returns the path to the font file in given format. Raises InvalidFormatError if the font is not available in this format
33 34 35 36 |
# File 'lib/rack-fontserve/font.rb', line 33 def format_path(format) raise Rack::Fontserve::InvalidFormatError unless formats.include?(format) file_path "#{name}.#{format}" end |
#formats ⇒ Object
Retruns a list of available formats
28 29 30 |
# File 'lib/rack-fontserve/font.rb', line 28 def formats @formats ||= Dir[File.join(path, "#{name}.{otf,svg,ttf,woff,eot}")].map {|file| File.extname(file)[1..-1] }.sort end |
#license ⇒ Object
Returns the content of the LICENSE file if present, nil otherwise
54 55 56 |
# File 'lib/rack-fontserve/font.rb', line 54 def license File.read file_path('LICENSE') if license? end |
#license? ⇒ Boolean
LICENSE file exists?
49 50 51 |
# File 'lib/rack-fontserve/font.rb', line 49 def license? File.exist? file_path('LICENSE') end |