Module: JamesBond::Core::MissionLoader

Defined in:
lib/james_bond/core/mission_loader.rb

Class Method Summary collapse

Class Method Details

.append_default_missions(mission_list) ⇒ Object



35
36
37
38
39
40
# File 'lib/james_bond/core/mission_loader.rb', line 35

def self.append_default_missions(mission_list)
  mission_list + default_missions.map do |mission_class|
    klass = Object.const_get(mission_class)
    klass.new
  end
end

.constantize_gem_name(gem_name) ⇒ Object



42
43
44
45
46
# File 'lib/james_bond/core/mission_loader.rb', line 42

def self.constantize_gem_name(gem_name)
  class_name = (gem_name || "").split("-")
                               .map { |split| split.split("_").collect(&:capitalize).join }
                               .join("::")
end

.default_missionsObject



8
9
10
# File 'lib/james_bond/core/mission_loader.rb', line 8

def self.default_missions
  @default_missions ||= %w(JamesBond::Core::HelpMission)
end

.default_missions=(list) ⇒ Object



12
13
14
# File 'lib/james_bond/core/mission_loader.rb', line 12

def self.default_missions=(list)
  @default_missions = list
end

.generate_gem_require_name(gem_name) ⇒ Object



48
49
50
# File 'lib/james_bond/core/mission_loader.rb', line 48

def self.generate_gem_require_name(gem_name)
  gem_name.gsub("-", "/")
end

.load_mission(mission_name) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/james_bond/core/mission_loader.rb', line 25

def self.load_mission(mission_name)
  gem_name      = "james_bond-#{sanitize_mission_name(mission_name)}_mission"
  require_name  = generate_gem_require_name(gem_name)
  constant_name = constantize_gem_name(gem_name)

  require require_name
  klass = Object.const_get(constant_name)
  klass.new
end

.load_missions(mission_names) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/james_bond/core/mission_loader.rb', line 16

def self.load_missions(mission_names)
  missions_loaded = mission_names.map do |mission_name|
    load_mission(mission_name)
  end
  missions_loaded = append_default_missions(missions_loaded)

  JamesBond::Core::MissionPool.new(mission_list: missions_loaded)
end

.sanitize_mission_name(mission_name) ⇒ Object



52
53
54
# File 'lib/james_bond/core/mission_loader.rb', line 52

def self.sanitize_mission_name(mission_name)
  mission_name.gsub(/[^0-9a-z\_\-]/i, "")
end