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

Class Method Details

.componentsObject



65
66
67
# File 'lib/ditty.rb', line 65

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.



47
48
49
50
51
52
53
54
# File 'lib/ditty.rb', line 47

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

.migrationsObject



86
87
88
89
90
# File 'lib/ditty.rb', line 86

def self.migrations
  components.map do |_name, comp|
    comp.migrations if comp.respond_to?(:migrations)
  end.compact
end

Return an ordered list of navigation items: ‘[’/users/‘, text:’Users’, ‘/roles/’, text:‘Roles’]



79
80
81
82
83
84
# File 'lib/ditty.rb', line 79

def self.navigation
  components.inject([]) do |memo, comp|
    memo.concat comp[1].navigation if comp[1].respond_to?(:navigation)
    memo
  end.sort_by { |v| v[:order] }
end

.public_folderObject



92
93
94
95
96
# File 'lib/ditty.rb', line 92

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)


60
61
62
63
# File 'lib/ditty.rb', line 60

def self.register_component(name, mod)
  Ditty::Services::Logger.instance.info "Registering #{mod} as #{name}"
  @components[name] = mod
end

.routesObject

Return a hash of controllers with their routes as keys: ‘{ ’/users’ => Ditty::Controllers::Users }‘



70
71
72
73
74
75
# File 'lib/ditty.rb', line 70

def self.routes
  components.inject({}) do |memo, comp|
    memo.merge! comp[1].routes if comp[1].respond_to?(:routes)
    memo
  end.compact
end

.seedersObject



98
99
100
101
102
# File 'lib/ditty.rb', line 98

def self.seeders
  components.map do |_name, comp|
    comp.seeder if comp.respond_to?(:seeder)
  end.compact
end

.workersObject



104
105
106
107
108
109
# File 'lib/ditty.rb', line 104

def self.workers
  components.inject([]) do |memo, comp|
    memo.concat comp[1].workers if comp[1].respond_to?(:workers)
    memo
  end
end