Class: AngryMob::MobLoader

Inherits:
Object show all
Defined in:
lib/angry_mob/mob_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMobLoader

Returns a new instance of MobLoader.



15
16
17
18
19
# File 'lib/angry_mob/mob_loader.rb', line 15

def initialize
  @builder = Builder.new
  @mobs = Dictionary.new
  @loaded_mobs = {}
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



9
10
11
# File 'lib/angry_mob/mob_loader.rb', line 9

def builder
  @builder
end

#loaded_mobsObject (readonly)

Returns the value of attribute loaded_mobs.



9
10
11
# File 'lib/angry_mob/mob_loader.rb', line 9

def loaded_mobs
  @loaded_mobs
end

#mobsObject (readonly)

Returns the value of attribute mobs.



9
10
11
# File 'lib/angry_mob/mob_loader.rb', line 9

def mobs
  @mobs
end

Instance Method Details

#add_mob(path, name = nil) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/angry_mob/mob_loader.rb', line 21

def add_mob(path,name=nil)
  path = Pathname(path).expand_path
  name ||= path.basename.to_s

  ui.log "added mob #{name} from #{path}"

  mobs[name] ||= path
end

#load_a_mob(name) ⇒ Object Also known as: depends_on_mob



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
56
57
# File 'lib/angry_mob/mob_loader.rb', line 30

def load_a_mob(name)
  path = mobs[name]
  raise "unable to load unknown mob #{name}" unless path

  return if loaded_mobs[name]
  loaded_mobs[name] = true

  old_path,@current_path = @current_path,path

  ui.push("loading mob #{name} at path #{path}") do

    mob_root = path

    loader = path+'load_mob.rb'

    if loader.exist?
      instance_eval(loader.read,loader.to_s)
    else
      load_lib(path+'lib')
      load_targets(path+'targets')
      load_acts(path+'acts')
    end
  end

  self
ensure
  @current_path = old_path
end

#load_act_file(path) ⇒ Object



107
108
109
110
111
112
# File 'lib/angry_mob/mob_loader.rb', line 107

def load_act_file(path)
  raise "act file at path #{path} didn't exist" unless path.exist?
  ui.log "loading acts from #{path}"

  @builder.from_file(path)
end

#load_acts(path = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/angry_mob/mob_loader.rb', line 96

def load_acts(path=nil)
  path ||= mob_root + 'acts'

  raise "acts path #{path} didn't exist" unless path.exist?
  ui.log "loading acts from #{path}"

  Pathname.glob(path+'**/*.rb').each do |file|
    @builder.from_file(file)
  end
end

#load_lib(path = nil) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/angry_mob/mob_loader.rb', line 87

def load_lib(path=nil)
  path ||= mob_root + 'lib'

  raise "lib path #{path} didn't exist" unless path.exist?

  ui.log "adding load path #{path}"
  $LOAD_PATH << path
end

#load_mobsObject



59
60
61
62
63
# File 'lib/angry_mob/mob_loader.rb', line 59

def load_mobs
  mobs.keys.each do |name|
    load_a_mob(name)
  end
end

#load_targets(path = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/angry_mob/mob_loader.rb', line 75

def load_targets(path=nil)
  path ||= mob_root + 'targets'

  raise "targets path #{path} didn't exist" unless path.exist?
  ui.log "loading targets from #{path}"

  $LOAD_PATH << path
  Pathname.glob(path+'**/*.rb').each do |file|
    require file
  end
end

#to_mobObject



65
66
67
68
# File 'lib/angry_mob/mob_loader.rb', line 65

def to_mob
  load_mobs
  @builder.to_mob
end

#uiObject



11
12
13
# File 'lib/angry_mob/mob_loader.rb', line 11

def ui
  Mob.ui
end