Module: Fontico

Defined in:
lib/fontico.rb,
lib/fontico/icon.rb,
lib/fontico/prawn.rb,
lib/fontico/helper.rb,
lib/fontico/builder.rb,
lib/fontico/railtie.rb,
lib/fontico/version.rb,
lib/fontico/lockfile.rb,
lib/fontico/manifest.rb,
lib/fontico/outliner.rb,
lib/fontico/resolver.rb,
lib/fontico/node_runner.rb,
lib/fontico/preprocessor.rb,
lib/fontico/emitters/font.rb,
lib/fontico/provider_fonts.rb,
lib/fontico/emitters/sprite.rb,
lib/fontico/emitters/stylesheet.rb

Defined Under Namespace

Modules: Emitters, Helper, Prawn, ProviderFonts Classes: Builder, Error, Icon, Lockfile, Manifest, NodeRunner, Outliner, Preprocessor, Railtie, Resolver

Constant Summary collapse

VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.build_error ⇒ Object (readonly)

Why the last rebuild could not deliver: the exception it died on, and the icons it left out of the sprite.



87
88
89
# File 'lib/fontico.rb', line 87

def build_error
  @build_error
end

.css_class ⇒ Object

Base class on every emitted ; also the stylesheet's selector.



32
# File 'lib/fontico.rb', line 32

def css_class = @css_class ||= "ico"

.inline_sprite=(value) ⇒ Object (writeonly)

Sets the attribute inline_sprite

Parameters:

  • value —

    the value to set the attribute inline_sprite to.



29
30
31
# File 'lib/fontico.rb', line 29

def inline_sprite=(value)
  @inline_sprite = value
end

.manifest_path ⇒ Object



36
37
38
# File 'lib/fontico.rb', line 36

def manifest_path = @manifest_path ||= File.join(root, "icons.yml")
# Same shape as I18n.load_path: engines first, the app last, later wins
# a name. Unset, it is just the app's icons.yml (non-Rails, tests).

.output_dir ⇒ Object



35
# File 'lib/fontico.rb', line 35

def output_dir    = @output_dir ||= "app/assets/builds"

.root ⇒ Object



34
# File 'lib/fontico.rb', line 34

def root          = @root ||= Dir.pwd

Class Method Details

.build(**opts) ⇒ Object



71
# File 'lib/fontico.rb', line 71

def build(**opts) = Builder.new(manifest, root: root, output: output_dir, **opts).call

.check!(name = nil) ⇒ Object

Raised at the call site, because that is the only place with anything useful to say. An icon left out of the sprite still resolves through the manifest and renders a perfectly valid at a symbol that isn't there — an invisible empty box, on a page that 200s. Silence is the one outcome worse than a stack trace.

Free in production: only the dev reloader ever fills these in.

Raises:



98
99
100
101
102
103
# File 'lib/fontico.rb', line 98

def check!(name = nil)
  raise Error, "icons.yml did not build: #{@build_error.message}" if @build_error

  reason = missing_icons[name]
  raise Error, "icon #{name.inspect} was left out of the sprite: #{reason}" if reason
end

.codepoint(name) ⇒ Object

The character to print when drawing this icon from the font — for Prawn, where you write the glyph literally. Pinned append-only in icons.lock so a reference in Ruby source never goes stale.

Raises:



112
113
114
115
116
117
# File 'lib/fontico.rb', line 112

def codepoint(name)
  cp = lockfile.codepoint_for(name.to_s)
  raise Error, "no icon named #{name.inspect} in icons.lock" if cp.nil?

  cp
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Fontico) —

    the object that the method was called on



122
# File 'lib/fontico.rb', line 122

def configure = yield(self)

.discover(app) ⇒ Object

Rails engines that ship config/icons.yml (or icons.yml at the gem root), then the app's file. The app is last on purpose.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fontico.rb', line 51

def discover(app)
  files = []
  app.railties.each do |rt|
    next unless rt.is_a?(Rails::Engine)
    next if rt.is_a?(Rails::Application)

    root = rt.root
    found = %w[config/icons.yml icons.yml].map { root.join(_1) }.find { File.file?(_1.to_s) }
    files << found.to_s if found
  end
  # manifest_path goes in even when absent: the dev watcher has to notice
  # an icons.yml created after boot. Manifest.load skips what isn't there.
  (files.select { File.file?(_1) } << manifest_path).uniq
end

.font_file ⇒ Object



107
# File 'lib/fontico.rb', line 107

def font_file = File.join(root, output_dir, "icons.ttf")

.glyph(name) ⇒ Object



119
# File 'lib/fontico.rb', line 119

def glyph(name) = [codepoint(name)].pack("U")

.inline_sprite? ⇒ Boolean

Inline mode embeds definitions in the layout instead of referencing an external file — required when assets are served from a CDN, where cross-origin silently renders nothing.

Returns:

  • (Boolean)


69
# File 'lib/fontico.rb', line 69

def inline_sprite? = !!@inline_sprite

.load_path ⇒ Object

Same shape as I18n.load_path: engines first, the app last, later wins a name. Unset, it is just the app's icons.yml (non-Rails, tests).



39
# File 'lib/fontico.rb', line 39

def load_path     = @load_path || [manifest_path].select { File.file?(_1) }

.load_path=(paths) ⇒ Object



41
42
43
44
# File 'lib/fontico.rb', line 41

def load_path=(paths)
  @load_path = paths.nil? ? nil : Array(paths)
  reset!
end

.lockfile ⇒ Object



105
# File 'lib/fontico.rb', line 105

def lockfile = Lockfile.new(File.join(root, "icons.lock"))

.manifest ⇒ Object



46
# File 'lib/fontico.rb', line 46

def manifest      = @manifest ||= Manifest.load(*load_path)

.missing_icons ⇒ Object



89
# File 'lib/fontico.rb', line 89

def missing_icons = @missing_icons ||= {}

.rebuild! ⇒ Object

The dev reloader's build, and the only caller that records what went wrong. Building stays forgiving on purpose — one typo must not stop the other 199 icons — so nothing raises here; the complaint is held until someone actually draws an icon. See #check!.



77
78
79
80
81
82
83
# File 'lib/fontico.rb', line 77

def rebuild!
  @build_error = nil
  @missing_icons = build.missing || {}
rescue StandardError => e
  @build_error = e
  @missing_icons = {}
end

.reset! ⇒ Object



120
# File 'lib/fontico.rb', line 120

def reset!        = (@manifest = @build_error = @missing_icons = nil)

.sprite_file ⇒ Object



47
# File 'lib/fontico.rb', line 47

def sprite_file   = File.join(root, output_dir, "icons.svg")