lib/protos/icon.rb in protos-icon-0.3.1 vs lib/protos/icon.rb in protos-icon-0.4.0
- old
+ new
@@ -1,47 +1,47 @@
# frozen_string_literal: true
+require "protos"
require "phlex"
+require "dry/inflector"
require_relative "icon/version"
-require_relative "icon/heroicons"
+require_relative "icon/heroicon"
require_relative "icon/inhouse"
require_relative "icon/component"
+require_relative "icon/heroicon_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
- def self.heroicon(name, variant: :solid)
- Heroicons.build(name, variant:)
- end
+ INFLECTOR = Dry::Inflector.new
- def self.inhouse(name, **)
- Inhouse.build(name)
+ def self.lookup(constant, name)
+ klass = "#{constant}::#{name}"
+ INFLECTOR.constantize(klass)
+ rescue NameError
+ nil
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.find(name)
+ [Heroicon, Inhouse].each do |constant|
+ component = lookup(constant, INFLECTOR.camelize(name))
+ return component if component
+ end
- component
+ raise MissingIcon, "Unknown icon: #{name}"
end
- def self.maybe(mod, name, ...)
- mod.build(name, ...)
- rescue MissingIcon
- nil
- end
-
- def icon(...)
- raise MissingIcon unless component = Protos::Icon.build(...)
-
+ def icon(name, ...)
+ component = Protos::Icon
+ .find(name)
+ .new(...)
render component
end
end
end