Module: Componentize::ComponentHost::ClassMethods

Defined in:
lib/autoload/componentize.rb

Instance Method Summary collapse

Instance Method Details

#add_component(component) ⇒ Object

Associates the given component with the current host.

Parameters:

  • component (Component)

    The component which included the host.


93
94
95
# File 'lib/autoload/componentize.rb', line 93

def add_component(component)
  component_names << component.name unless component_names.include?(component.name)
end

#componentsArray<Component>

Gets all the components associated with this host.

Returns:

  • (Array<Component>)

    The components associated with this host.


100
101
102
# File 'lib/autoload/componentize.rb', line 100

def components
  component_names.map(&:constantize)
end

#eager_load_components(in_path) ⇒ Object

Eager loads all components in the provided path. Components have the suffix Component in their class names.

the files must follow Rails conventions.

Parameters:

  • in_path (Dir|String)

    The directory to eager load all components from. The naming of


77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/autoload/componentize.rb', line 77

def eager_load_components(in_path)
  if in_path.is_a?(String)
    return unless Dir.exist?(in_path)
    in_path = Dir.open(in_path)
  end

  base_path = Pathname.new(in_path.path).realpath

  Dir.glob("#{base_path}/**/*_component.rb").each do |file|
    load_component(file, base_path)
  end
end