Module: Ditty::Components
- Defined in:
- lib/ditty.rb
Overview
Ripped off from Roda - github.com/jeremyevans/roda
Defined Under Namespace
Modules: Base
Class Method Summary collapse
- .components ⇒ Object
-
.load_component(name) ⇒ Object
If the registered component already exists, use it.
- .migrations ⇒ Object
- .navigation ⇒ Object
- .public_folder ⇒ Object
-
.register_component(name, mod) ⇒ Object
Register the given component with Component, so that it can be loaded using #component with a symbol.
-
.routes ⇒ Object
Return a hash of controllers with their routes as keys: ‘{ ’/users’ => Ditty::Controllers::Users }‘.
- .seeders ⇒ Object
- .workers ⇒ Object
Class Method Details
.components ⇒ Object
69 70 71 |
# File 'lib/ditty.rb', line 69 def self.components @components end |
.load_component(name) ⇒ Object
If the registered component already exists, use it. Otherwise, require it and return it. This raises a LoadError if such a component doesn’t exist, or a Component if it exists but it does not register itself correctly.
51 52 53 54 55 56 57 58 |
# File 'lib/ditty.rb', line 51 def self.load_component(name) h = @components unless (component = h[name]) require "ditty/components/#{name}" raise ComponentError, "Component #{name} did not register itself correctly in Ditty::Components" unless (component = h[name]) end component end |
.migrations ⇒ Object
90 91 92 93 94 |
# File 'lib/ditty.rb', line 90 def self.migrations components.map do |_name, comp| comp.migrations if comp.respond_to?(:migrations) end.compact end |
.navigation ⇒ Object
83 84 85 86 87 88 |
# File 'lib/ditty.rb', line 83 def self. nav = components.each_with_object([]) do |comp, memo| memo.concat comp[1]. if comp[1].respond_to?(:navigation) end nav.sort_by { |v| v[:order] } end |
.public_folder ⇒ Object
96 97 98 99 100 |
# File 'lib/ditty.rb', line 96 def self.public_folder components.map do |_name, comp| comp.public_folder if comp.respond_to?(:public_folder) end.compact end |
.register_component(name, mod) ⇒ Object
Register the given component with Component, so that it can be loaded using #component with a symbol. Should be used by component files. Example:
Ditty::Components.register_component(:component_name, ComponentModule)
64 65 66 67 |
# File 'lib/ditty.rb', line 64 def self.register_component(name, mod) Ditty::Services::Logger.instance.info "Registering #{mod} as #{name}" @components[name] = mod end |
.routes ⇒ Object
Return a hash of controllers with their routes as keys: ‘{ ’/users’ => Ditty::Controllers::Users }‘
74 75 76 77 78 79 |
# File 'lib/ditty.rb', line 74 def self.routes rts = components.each_with_object({}) do |comp, memo| memo.merge! comp[1].routes if comp[1].respond_to?(:routes) end rts.compact end |
.seeders ⇒ Object
102 103 104 105 106 |
# File 'lib/ditty.rb', line 102 def self.seeders components.map do |_name, comp| comp.seeder if comp.respond_to?(:seeder) end.compact end |
.workers ⇒ Object
108 109 110 111 112 |
# File 'lib/ditty.rb', line 108 def self.workers components.each_with_object([]) do |comp, memo| memo.concat comp[1].workers if comp[1].respond_to?(:workers) end end |