Class: Cedar::ComponentWarden

Inherits:
Object
  • Object
show all
Defined in:
lib/cedar/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



11
12
13
14
15
16
17
# File 'lib/cedar/component_warden.rb', line 11

def initialize(component, string)
  @component = component
  @raw_input = string

  @collection = @raw_input.end_with? "_collection"
  @input = @raw_input.to_s.chomp("_collection")
end

Instance Method Details

#collection?Boolean



19
20
21
# File 'lib/cedar/component_warden.rb', line 19

def collection?
  @collection
end

#component_classObject



27
28
29
# File 'lib/cedar/component_warden.rb', line 27

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

#component_nameObject



23
24
25
# File 'lib/cedar/component_warden.rb', line 23

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

#exists?Boolean



31
32
33
# File 'lib/cedar/component_warden.rb', line 31

def exists?
  component_class.present?
end

#new(*args, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/cedar/component_warden.rb', line 35

def new(*args, &block)
  return nil unless exists?

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