lib/protos/icon.rb in protos-icon-0.2.0 vs lib/protos/icon.rb in protos-icon-0.3.0

- old
+ new

@@ -1,56 +1,47 @@ # frozen_string_literal: true require "phlex" require_relative "icon/version" +require_relative "icon/heroicons" +require_relative "icon/inhouse" +require_relative "icon/component" module Protos module Icon # Your code goes here... + class Error < StandardError; end + MissingIcon = Class.new(Error) GEM_ROOT = Pathname.new(__dir__).join("..", "..").expand_path public_constant :GEM_ROOT - class Component < Phlex::HTML - def initialize(filepath) - @filepath = filepath - end + def self.heroicon(name, variant: :solid) + Heroicons.build(name, variant:) + end - def view_template - unsafe_raw File.read(@filepath) - end + def self.inhouse(name, **) + Inhouse.build(name) end - module Heroicons - def self.new(name, variant: :solid) - filepath = lookup(name, variant:) - Component.new(filepath) - end + def self.build(name, ...) + component = maybe(Heroicons, name, ...) + component ||= maybe(Inhouse, name, ...) + raise MissingIcon, "Could not find an icon for #{name}" unless component - def self.lookup(name, variant: :solid) - filepath = Pathname.new(GEM_ROOT) - filepath = case variant - when :solid then filepath.join("assets/heroicons/24/solid") - when :outline then filepath.join("assets/heroicons/24/outline") - when :mini then filepath.join("assets/heroicons/20/solid") - when :micro then filepath.join("assets/heroicons/16/solid") - else raise ArgumentError, "Unknown variant: #{variant}" - end - - filepath - .join("#{name}.svg") - .tap do |path| - File.exist?(path) or raise ArgumentError, "Unknown icon: #{name}" - end - end + component end - def self.heroicon(name, variant: :solid) - Heroicons.new(name, variant:) + def self.maybe(mod, name, ...) + mod.build(name, ...) + rescue MissingIcon + nil end def icon(...) - render Protos::Icon.heroicon(...) + raise MissingIcon unless component = Protos::Icon.build(...) + + render component end end end