Class: Praxis::Bootloader

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/bootloader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application) ⇒ Bootloader

Returns a new instance of Bootloader.



7
8
9
10
11
12
# File 'lib/praxis/bootloader.rb', line 7

def initialize(application)
  @application = application
  @stages = []

  setup_stages!
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



5
6
7
# File 'lib/praxis/bootloader.rb', line 5

def application
  @application
end

#stagesObject (readonly)

Returns the value of attribute stages.



5
6
7
# File 'lib/praxis/bootloader.rb', line 5

def stages
  @stages
end

Instance Method Details

#after(*stage_path, &block) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/praxis/bootloader.rb', line 68

def after(*stage_path, &block)
  stage_name = stage_path.shift
  the_stage = stages.find { |stage| stage.name == stage_name }
  raise Exceptions::StageNotFound, "Error running an after block for stage #{stage_name}" unless the_stage

  the_stage.after(*stage_path, &block)
end

#before(*stage_path, &block) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/praxis/bootloader.rb', line 60

def before(*stage_path, &block)
  stage_name = stage_path.shift
  the_stage = stages.find { |stage| stage.name == stage_name }
  raise Exceptions::StageNotFound, "Error running a before block for stage #{stage_name}" unless the_stage

  the_stage.before(*stage_path, &block)
end

#configObject



14
15
16
# File 'lib/praxis/bootloader.rb', line 14

def config
  @application.config
end

#delete_stage(stage_name) ⇒ Object



54
55
56
57
58
# File 'lib/praxis/bootloader.rb', line 54

def delete_stage(stage_name)
  raise Exceptions::StageNotFound, "Cannot remove stage with name #{stage_name}, stage does not exist." unless (stage = stages.find { |s| s.name == stage_name })

  stages.delete(stage)
end

#rootObject



18
19
20
# File 'lib/praxis/bootloader.rb', line 18

def root
  @application.root
end

#runObject



119
120
121
122
123
124
# File 'lib/praxis/bootloader.rb', line 119

def run
  stages.each do |stage|
    stage.setup!
    stage.run
  end
end

#setup!Object



113
114
115
116
117
# File 'lib/praxis/bootloader.rb', line 113

def setup!
  # use the Notifications plugin by default
  use Praxis::Notifications
  run
end

#setup_stages!Object



22
23
24
25
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
# File 'lib/praxis/bootloader.rb', line 22

def setup_stages!
  # Require environment first. they define constants and environment specific settings
  stages << BootloaderStages::Environment.new(:environment, application)

  # then setup plugins
  stages << BootloaderStages::PluginLoader.new(:plugins, application)

  # then the initializers. as it is their job to ensure monkey patches and other
  # config is in place first.
  stages << BootloaderStages::FileLoader.new(:initializers, application)

  # then require lib/ code.
  stages << BootloaderStages::FileLoader.new(:lib, application)

  # design-specific code.
  stages << BootloaderStages::SubgroupLoader.new(:design, application)

  # app-specific code.
  stages << BootloaderStages::SubgroupLoader.new(:app, application)

  # setup routing
  stages << BootloaderStages::Routing.new(:routing, application)

  # naggy warning about unloaded files
  stages << BootloaderStages::WarnUnloadedFiles.new(:warn_unloaded_files, application)

  after(:app) do
    Praxis::Blueprint.finalize!
    Praxis::EndpointDefinition.finalize!
  end
end

#use(plugin, **options, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/praxis/bootloader.rb', line 76

def use(plugin, **options, &block)
  if plugin.ancestors.include?(PluginConcern)
    plugin.setup!
    plugin = plugin::Plugin
  end

  instance = if plugin.ancestors.include?(Singleton)
               plugin.instance
             elsif plugin.is_a?(Class)
               plugin.new
             else
               plugin
             end

  instance.application = application
  instance.options.merge!(options)
  instance.block = block if block_given?

  config_key = if instance.config_key.nil?
                 raise "Cannot use plugin: #{plugin}. It does not have a config_key defined, and its class does not have a name" unless instance.class.name

                 # Default the config key based on the full class name transformed to snake case (and joining modules with '_')
                 instance.class.name.to_s.split('::').collect(&:underscore).join('_').to_sym
               else
                 instance.config_key
               end

  if application.plugins.key?(instance.config_key)
    used_in = application.plugins[config_key].class
    raise "Can not use plugin: #{plugin}, another plugin (#{used_in}) is already registered with key: #{instance.config_key}"
  end

  application.plugins[config_key] = instance

  instance
end