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.



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

def initialize(application)
  @application = application
  @stages = Array.new

  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.



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

def stages
  @stages
end

Instance Method Details

#after(*stage_path, &block) ⇒ Object



73
74
75
76
# File 'lib/praxis/bootloader.rb', line 73

def after(*stage_path, &block)
  stage_name = stage_path.shift
  stages.find { |stage| stage.name == stage_name }.after(*stage_path, &block)
end

#before(*stage_path, &block) ⇒ Object



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

def before(*stage_path, &block)
  stage_name = stage_path.shift
  stages.find { |stage| stage.name == stage_name }.before(*stage_path, &block)
end

#configObject



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

def config
  @application.config
end

#delete_stage(stage_name) ⇒ Object



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

def delete_stage(stage_name)
  if (stage = stages.find { |stage| stage.name == stage_name })
    stages.delete(stage)
  else
    raise Exceptions::StageNotFound.new(
      "Cannot remove stage with name #{stage_name}, stage does not exist."
    )
  end
end

#rootObject



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

def root
  @application.root
end

#runObject



116
117
118
119
120
121
# File 'lib/praxis/bootloader.rb', line 116

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

#setup!Object



108
109
110
111
112
113
114
# File 'lib/praxis/bootloader.rb', line 108

def setup!
  # use the Stats and Notifications plugins by default
  use Praxis::Stats
  use Praxis::Notifications

  run
end

#setup_stages!Object



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
53
54
55
# File 'lib/praxis/bootloader.rb', line 23

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::Mapper.finalize!
    Praxis::Blueprint.finalize!
    Praxis::ResourceDefinition.finalize!
  end

end

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



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
# File 'lib/praxis/bootloader.rb', line 78

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.kind_of?(Class)
    plugin.new
  else
    plugin
  end

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

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

  if instance.config_key.nil?
    raise "Error initializing plugin: #{plugin}, config_key may not be nil."
  end

  application.plugins[instance.config_key] = instance
  instance
end