Class: Fontist::Import::Google::Models::FontFamily
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Fontist::Import::Google::Models::FontFamily
- Defined in:
- lib/fontist/import/google/models/font_family.rb
Instance Method Summary collapse
-
#axes_count ⇒ Integer
Get the number of axes.
-
#axis_by_tag(tag) ⇒ Axis?
Find an axis by its tag.
-
#custom_axes ⇒ Array<Axis>
Get all custom (non-standard) axes.
-
#file_urls ⇒ Array<String>
Get all file URLs.
-
#files ⇒ Hash
Get files as a Hash.
-
#files=(value) ⇒ Object
Set files from a Hash or JSON string.
-
#initialize(**attributes) ⇒ FontFamily
constructor
Initialize with support for files as Hash.
-
#slant_axes ⇒ Array<Axis>
Get all slant axes.
-
#summary ⇒ String
Get a human-readable summary.
-
#variable_font? ⇒ Boolean
Check if this is a variable font family Variable fonts have axes defined.
-
#variant_exists?(variant_name) ⇒ Boolean
Check if a specific variant exists.
-
#variant_names ⇒ Array<String>
Get all variant names.
-
#variant_url(variant_name) ⇒ String?
Get URL for a specific variant.
-
#variants_by_format(format) ⇒ Hash
Get all variants in a specific format Note: This requires the files hash to contain URLs with the format extension.
-
#weight_axes ⇒ Array<Axis>
Get all weight axes.
-
#width_axes ⇒ Array<Axis>
Get all width axes.
Constructor Details
#initialize(**attributes) ⇒ FontFamily
Initialize with support for files as Hash
41 42 43 44 45 46 47 |
# File 'lib/fontist/import/google/models/font_family.rb', line 41 def initialize(**attributes) if attributes.key?(:files) files_value = attributes.delete(:files) attributes[:files_data] = files_value end super(**attributes) end |
Instance Method Details
#axes_count ⇒ Integer
Get the number of axes
154 155 156 |
# File 'lib/fontist/import/google/models/font_family.rb', line 154 def axes_count variable_font? ? axes.length : 0 end |
#axis_by_tag(tag) ⇒ Axis?
Find an axis by its tag
109 110 111 112 113 |
# File 'lib/fontist/import/google/models/font_family.rb', line 109 def axis_by_tag(tag) return nil unless variable_font? axes.find { |axis| axis.tag == tag } end |
#custom_axes ⇒ Array<Axis>
Get all custom (non-standard) axes
145 146 147 148 149 |
# File 'lib/fontist/import/google/models/font_family.rb', line 145 def custom_axes return [] unless variable_font? axes.select(&:custom_axis?) end |
#file_urls ⇒ Array<String>
Get all file URLs
168 169 170 |
# File 'lib/fontist/import/google/models/font_family.rb', line 168 def file_urls files ? files.values : [] end |
#files ⇒ Hash
Get files as a Hash
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/fontist/import/google/models/font_family.rb', line 52 def files return {} if files_data.nil? return {} if files_data.respond_to?(:empty?) && files_data.empty? return files_data if files_data.is_a?(Hash) # Try to parse as JSON first begin JSON.parse(files_data) rescue JSON::ParserError, TypeError # If it fails, convert Ruby hash syntax to JSON and parse if files_data.is_a?(String) # Convert Ruby hash syntax (=>) to JSON syntax (:) json_str = files_data.gsub("=>", ":") begin return JSON.parse(json_str) rescue JSON::ParserError {} end end {} end end |
#files=(value) ⇒ Object
Set files from a Hash or JSON string
78 79 80 |
# File 'lib/fontist/import/google/models/font_family.rb', line 78 def files=(value) self.files_data = value end |
#slant_axes ⇒ Array<Axis>
Get all slant axes
136 137 138 139 140 |
# File 'lib/fontist/import/google/models/font_family.rb', line 136 def slant_axes return [] unless variable_font? axes.select(&:slant_axis?) end |
#summary ⇒ String
Get a human-readable summary
193 194 195 196 197 |
# File 'lib/fontist/import/google/models/font_family.rb', line 193 def summary parts = [family, version] parts << "(#{axes_count} axes)" if variable_font? parts.join(" ") end |
#variable_font? ⇒ Boolean
Check if this is a variable font family Variable fonts have axes defined
86 87 88 |
# File 'lib/fontist/import/google/models/font_family.rb', line 86 def variable_font? !axes.nil? && !axes.empty? end |
#variant_exists?(variant_name) ⇒ Boolean
Check if a specific variant exists
176 177 178 |
# File 'lib/fontist/import/google/models/font_family.rb', line 176 def variant_exists?(variant_name) variant_names.include?(variant_name) end |
#variant_names ⇒ Array<String>
Get all variant names
161 162 163 |
# File 'lib/fontist/import/google/models/font_family.rb', line 161 def variant_names variants || [] end |
#variant_url(variant_name) ⇒ String?
Get URL for a specific variant
184 185 186 187 188 |
# File 'lib/fontist/import/google/models/font_family.rb', line 184 def variant_url(variant_name) return nil if files.nil? || files.empty? files[variant_name] end |
#variants_by_format(format) ⇒ Hash
Get all variants in a specific format Note: This requires the files hash to contain URLs with the format extension
96 97 98 99 100 101 102 103 |
# File 'lib/fontist/import/google/models/font_family.rb', line 96 def variants_by_format(format) return {} if files.nil? extension = format_extension(format) files.select do |_variant_name, url| url.end_with?(extension) end end |
#weight_axes ⇒ Array<Axis>
Get all weight axes
118 119 120 121 122 |
# File 'lib/fontist/import/google/models/font_family.rb', line 118 def weight_axes return [] unless variable_font? axes.select(&:weight_axis?) end |
#width_axes ⇒ Array<Axis>
Get all width axes
127 128 129 130 131 |
# File 'lib/fontist/import/google/models/font_family.rb', line 127 def width_axes return [] unless variable_font? axes.select(&:width_axis?) end |