Class: Ilex::ComponentWarden

Inherits:
Object
  • Object
show all
Defined in:
lib/ilex/component_warden.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(component, string) ⇒ ComponentWarden

Returns a new instance of ComponentWarden.



11
12
13
14
15
16
17
18
19
# 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

Instance Method Details

#collection?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/ilex/component_warden.rb', line 21

def collection?
  @collection
end

#component_classObject



33
34
35
# File 'lib/ilex/component_warden.rb', line 33

def component_class
  @component_class ||= @component.class.find_component(component_name, @base_module)
end

#component_nameObject



29
30
31
# File 'lib/ilex/component_warden.rb', line 29

def component_name
  "#{@input}_component".camelize
end

#exists?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ilex/component_warden.rb', line 37

def exists?
  component_class.present?
end

#nested?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/ilex/component_warden.rb', line 25

def nested?
  @nested
end

#new(*args, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
# 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