class Ilex::ComponentWarden

A ComponentWarden is responsible for locating potential components given a snake_case representation of that component.

If a component exists, the warden is also responsible for calling the correct instantiation method, depending if the component is a collection or not

Public Class Methods

new(component, string) click to toggle source
# File lib/ilex/component_warden.rb, line 11
def initialize(component, string)
  @component = component
  @input = string.to_s

  @collection = @input.end_with? "_collection"
  @nested = @input.include? "__"

  process_input!(@input)
end

Public Instance Methods

collection?() click to toggle source
# File lib/ilex/component_warden.rb, line 21
def collection?
  @collection
end
component_class() click to toggle source
# File lib/ilex/component_warden.rb, line 33
def component_class
  @component_class ||= @component.class.find_component(component_name, @base_module)
end
component_name() click to toggle source
# File lib/ilex/component_warden.rb, line 29
def component_name
  "#{@input}_component".camelize
end
exists?() click to toggle source
# File lib/ilex/component_warden.rb, line 37
def exists?
  component_class.present?
end
nested?() click to toggle source
# File lib/ilex/component_warden.rb, line 25
def nested?
  @nested
end
new(*args, &block) click to toggle source
# File lib/ilex/component_warden.rb, line 41
def new(*args, &block)
  return nil unless exists?

  if collection?
    component_class.with_collection(*args)
  else
    component_class.new(*args)
  end
end

Private Instance Methods

process_input!(input) click to toggle source
# File lib/ilex/component_warden.rb, line 53
def process_input!(input)
  @input.chomp! "_collection"
  @input.chomp! "_component"

  if nested?
    parts = @input.split("__")
    @input = parts.pop

    @base_module = parts.map(&:camelize).join("::").constantize
  end
end