Module: Component::Framework

Defined in:
lib/component/framework.rb,
lib/component/framework/version.rb,
lib/component/framework/sass_importer.rb

Defined Under Namespace

Classes: DirectiveProcessor, SassImporter, ScssTemplate

Constant Summary collapse

COMPONENT_IMAGE_ASSETS =
lambda do |logical_path, filename|
  filename.start_with?(components_base_dir.to_s) &&
      %w(.png .jpg .jpeg .gif).include?(File.extname(logical_path))
end
VERSION =
"0.2.5"

Class Method Summary collapse

Class Method Details

.component_module_by_name(name) ⇒ Object

Get the component root module by component name

Parameters:

  • name (string)

    the component name

Returns:

  • (Object)

    Component root module



106
107
108
109
110
111
112
# File 'lib/component/framework.rb', line 106

def self.component_module_by_name(name)
  return name.camelize.constantize
rescue NameError, ArgumentError
  message = "Component #{name} not found"
  log(message)
  raise ComponentNotFoundError, message
end

.components_base_dirstring

Components root folder path

Returns:

  • (string)

    Components root folder path



16
17
18
# File 'lib/component/framework.rb', line 16

def self.components_base_dir
  @base_dir ||= Rails.root.join("components")
end

.get_component_modules(load_initializers: false) ⇒ Array<Object>

List of component root modules

Parameters:

  • load_initializers (bool) (defaults to: false)

    force component initialize.rb load

Returns:

  • (Array<Object>)

    List of component root modules



92
93
94
95
96
97
98
99
# File 'lib/component/framework.rb', line 92

def self.get_component_modules(load_initializers: false)
  if load_initializers
    Component::Framework.log("Load Components Initializers")
    Component::Framework._load_components_initializers
  end

  get_component_names.map { |name| component_module_by_name(name) }
end

.get_component_namesArray<string>

List of component names

Returns:

  • (Array<string>)

    List of component names



83
84
85
86
# File 'lib/component/framework.rb', line 83

def self.get_component_names
  Dir.entries(components_base_dir)
      .select { |entry| (entry !="." && entry != "..") and File.directory? components_base_dir.join(entry) }.sort
end

.initialize(application, assets_pipeline: true, verbose: false) ⇒ Object

Initialize Component Framework

Parameters:

  • application (Rails::Application)

    the application class

  • assets_pipeline (bool) (defaults to: true)

    specifies whether Sprockets asset_pipeline should be configured for components

  • verbose (bool) (defaults to: false)

    allows to show components initialization log, default ‘false`



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/component/framework.rb', line 26

def self.initialize(application, assets_pipeline: true, verbose: false)
  @verbose = verbose

  # patch Rails
  require "component/framework/railtie"

  log("Components Initialization Started")
  log("Components Path: #{components_base_dir}")

  # add eager and autoload path that will allow to eager load and resolve components with namespaces.
  application.config.paths.add components_base_dir.to_s, eager_load: true

  log("Discovered Components: #{get_component_names.join(", ")}")

  log("Register DB Migrations")
  _get_component_migrations_paths.each { |path| application.config.paths["db/migrate"].push(path) }

  log("Register Components Routes")
  application.config.paths["config/routes.rb"].unshift(*_get_component_routing_paths)

  log("Register Components Helpers")
  application.config.paths["app/helpers"].unshift(*_get_component_helpers_paths)

  if assets_pipeline
    _initialize_assets_pipeline(application)
  end

  # Initialize components
  #
  # Initialization happen in 2 steps to allow components to perform actions upon initialized components.
  # First cycle is a part of Rails initialization, so it's possible to configure Rails env in it
  # like registering middleware etc.
  application.initializer :initialize_components, group: :all do |app|
    components = Component::Framework.get_component_modules(load_initializers: true)
    Component::Framework.log("Initialize Components")
    components.each {|component| component.send("init") if component.respond_to?("init") }
  end

  # Post initialization of components
  # when all the other parts are ready to be referenced
  #
  # A place to register subscribers as well as call other component's services.
  application.config.after_initialize do |app|
    components = get_component_modules

    log("Post-Initialize Components")
    components.each {|component| component.send("ready") if component.respond_to?("ready") }

    log("Components Initialization Done")
  end

  log("Configuration Finished")
end