Sha256: 2e01a74f854b356e368a382b0d65990f5c05e781b755167aa0c9e8f3f1163cae

Contents?: true

Size: 774 Bytes

Versions: 75

Compression:

Stored size: 774 Bytes

Contents

module Coco
  class ComponentResolver
    CLASS_CACHE = {}

    delegate_missing_to :@klass

    def initialize(klass, *args, **kwargs)
      @klass = klass
      @args = args
      @kwargs = kwargs
    end

    def render_in(view_context, ...)
      view_context.render(instance, ...)
    end

    def instance
      @_instance ||= @klass.new(*@args, **@kwargs)
    end

    class << self
      def new(target, *args, **kwargs)
        klass = target.is_a?(String) ? lookup_class(target) : target

        if args.any? || kwargs.any?
          klass.new(*args, **kwargs)
        else
          super(klass, *args, **kwargs)
        end
      end

      def lookup_class(path)
        CLASS_CACHE[path] ||= path.tr("-", "_").camelize.constantize
      end
    end
  end
end

Version data entries

75 entries across 75 versions & 1 rubygems

Version Path
coveragebook_components-0.19.10 lib/coco/component_resolver.rb
coveragebook_components-0.19.9 lib/coco/component_resolver.rb
coveragebook_components-0.19.8 lib/coco/component_resolver.rb
coveragebook_components-0.19.7 lib/coco/component_resolver.rb
coveragebook_components-0.19.6 lib/coco/component_resolver.rb
coveragebook_components-0.19.5 lib/coco/component_resolver.rb
coveragebook_components-0.19.4 lib/coco/component_resolver.rb
coveragebook_components-0.19.3 lib/coco/component_resolver.rb
coveragebook_components-0.19.2 lib/coco/component_resolver.rb
coveragebook_components-0.19.1 lib/coco/component_resolver.rb
coveragebook_components-0.19.0 lib/coco/component_resolver.rb
coveragebook_components-0.18.8 lib/coco/component_resolver.rb
coveragebook_components-0.18.7 lib/coco/component_resolver.rb
coveragebook_components-0.18.0 lib/coco/component_resolver.rb
coveragebook_components-0.17.7 lib/coco/component_resolver.rb
coveragebook_components-0.17.6 lib/coco/component_resolver.rb
coveragebook_components-0.17.5 lib/coco/component_resolver.rb
coveragebook_components-0.17.4 lib/coco/component_resolver.rb
coveragebook_components-0.17.3 lib/coco/component_resolver.rb
coveragebook_components-0.17.2 lib/coco/component_resolver.rb