lib/helium/console/registry.rb in helium-console-0.1.7 vs lib/helium/console/registry.rb in helium-console-0.1.8
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require 'colorized_string'
module Helium
class Console
class Registry
@@ -7,10 +9,13 @@
def initialize(object, **options)
@object = object
@options = options
end
+ def call
+ end
+
attr_reader :object, :options
def format_nested(other_object, **options)
Helium::Console.format(other_object, **nested_opts(options))
end
@@ -21,32 +26,35 @@
def format_string(string, **options)
Helium::Console.format_string(string, **options)
end
- def simple?(object)
- Helium::Console.simple?(object)
- end
-
- def is_simple
+ def simple?
false
end
def method_missing(name, *args)
return @options[name] if @options.key?(name)
- if ColorizedString.colors.include?(name)
- return ColorizedString.new(*args).colorize(name)
- end
+ return ColorizedString.new(*args).colorize(name) if ColorizedString.colors.include?(name)
+
super
end
+ def respond_to_missing?(name, private = false)
+ @options.key?(name) || ColorizedString.colors.include?(name) || super
+ end
+
def nested_opts(new_options, increase_level: true)
new_options = options.merge(new_options)
new_options[:level] += 1 if increase_level
new_options[:ignore_objects] << object.object_id
new_options
end
+
+ def length_of(string)
+ ColorizedString.new(string).uncolorize.length
+ end
end
def add(klass, &handler)
define(klass) do
define_method(:call, &handler)
@@ -56,16 +64,18 @@
def define(klass, &block)
handlers[klass] = Class.new(Element, &block)
end
def handler_for(object, **options)
- object.class.ancestors.each do |ancestor|
- return handlers[ancestor].new(object, **options) if handlers.key?(ancestor)
+ element_class = object.class.ancestors.find do |ancestor|
+ break handlers[ancestor] if handlers.key?(ancestor)
end
- nil
+ return unless element_class
+
+ element_class.new(object, **options)
end
- private
+ private
def handlers
@handlers ||= {}
end
end