Class: Coco::ComponentResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/coco/component_resolver.rb

Constant Summary collapse

CLASS_CACHE =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, *args, **kwargs) ⇒ ComponentResolver

Returns a new instance of ComponentResolver.



7
8
9
10
11
# File 'lib/coco/component_resolver.rb', line 7

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

Class Method Details

.lookup_class(path) ⇒ Object



32
33
34
# File 'lib/coco/component_resolver.rb', line 32

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

.new(target, *args, **kwargs) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/coco/component_resolver.rb', line 22

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

Instance Method Details

#instanceObject



17
18
19
# File 'lib/coco/component_resolver.rb', line 17

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

#render_in(view_context) ⇒ Object



13
14
15
# File 'lib/coco/component_resolver.rb', line 13

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