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.17.1 lib/coco/component_resolver.rb
coveragebook_components-0.17.0 lib/coco/component_resolver.rb
coveragebook_components-0.16.0 lib/coco/component_resolver.rb
coveragebook_components-0.15.0 lib/coco/component_resolver.rb
coveragebook_components-0.14.0 lib/coco/component_resolver.rb
coveragebook_components-0.13.1 lib/coco/component_resolver.rb
coveragebook_components-0.13.0 lib/coco/component_resolver.rb
coveragebook_components-0.12.2 lib/coco/component_resolver.rb
coveragebook_components-0.12.1 lib/coco/component_resolver.rb
coveragebook_components-0.12.0 lib/coco/component_resolver.rb
coveragebook_components-0.11.0 lib/coco/component_resolver.rb
coveragebook_components-0.10.1.beta.2 lib/coco/component_resolver.rb
coveragebook_components-0.10.1.beta.1 lib/coco/component_resolver.rb
coveragebook_components-0.10.1.beta.0 lib/coco/component_resolver.rb
coveragebook_components-0.10.0 lib/coco/component_resolver.rb
coveragebook_components-0.9.1 lib/coco/component_resolver.rb
coveragebook_components-0.9.0 lib/coco/component_resolver.rb
coveragebook_components-0.8.9 lib/coco/component_resolver.rb
coveragebook_components-0.8.8 lib/coco/component_resolver.rb
coveragebook_components-0.8.7 lib/coco/component_resolver.rb