Sha256: f99bc85e46d9be952429f15a8e7fbd3c46f0ac6013cfc30c31125ccd12306535

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

require 'colorized_string'

module Helium
  class Console
    class Registry
      class Element
        def initialize(object, **options)
          @object = object
          @options = options
        end

        attr_reader :object, :options

        def format_nested(other_object, **options)
          Helium::Console.format(other_object, **nested_opts(options))
        end

        def format(other_object, **options)
          Helium::Console.format(other_object, **nested_opts(options, increase_level: false))
        end

        def format_string(string, **options)
          Helium::Console.format_string(string, **options)
        end

        def simple?(object)
          Helium::Console.simple?(object)
        end

        def is_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
          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
      end

      def add(klass, &handler)
        define(klass) do
          define_method(:call, &handler)
        end
      end

      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)
        end
        nil
      end

    private

      def handlers
        @handlers ||= {}
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
helium-console-0.1.7 lib/helium/console/registry.rb
helium-console-0.1.6 lib/helium/console/registry.rb
helium-console-0.1.5 lib/helium/console/registry.rb
helium-console-0.1.4 lib/helium/console/registry.rb
helium-console-0.1.3 lib/helium/console/registry.rb