Class: AngryMob::Target::Mother

Inherits:
Object
  • Object
show all
Defined in:
lib/angry_mob/target/mother.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mob) ⇒ Mother

Returns a new instance of Mother.



6
7
8
# File 'lib/angry_mob/target/mother.rb', line 6

def initialize(mob)
  @mob = mob
end

Instance Attribute Details

#mobObject (readonly)

Returns the value of attribute mob.



4
5
6
# File 'lib/angry_mob/target/mother.rb', line 4

def mob
  @mob
end

Instance Method Details

#clear_instances!Object



28
29
30
# File 'lib/angry_mob/target/mother.rb', line 28

def clear_instances!
  @target_instances = nil
end

#key_classesObject



40
41
42
# File 'lib/angry_mob/target/mother.rb', line 40

def key_classes 
  @key_classes ||= Hash.new {|h,k| h[k] = []}
end

#pose_as(nickname, nickname_to_pose_as) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/angry_mob/target/mother.rb', line 14

def pose_as(nickname,nickname_to_pose_as)
  nickname            = nickname.to_s
  nickname_to_pose_as = nickname_to_pose_as.to_s

  posing_class = target_classes[nickname           ] || raise(TargetError, "posing class '#{nickname}' doesn't exist!")
  old_class    = target_classes[nickname_to_pose_as]

  target_classes[nickname_to_pose_as] = posing_class

  if old_class && instances = key_classes.delete(old_class)
    # TODO - merge instances if required
  end
end

#target_call(nickname, *args, &blk) ⇒ Object

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/angry_mob/target/mother.rb', line 44

def target_call(nickname, *args, &blk)
  raise(MobError, "no target nicknamed '#{nickname}'\n#{target_classes.keys.inspect}") unless target_classes.key?(nickname.to_s)
  klass = target_classes[nickname.to_s]

  args = Arguments.parse(args,&blk)

  if key = Target::Call.instance_key(klass,args)
    if call = target_instances[key]
      call.add_args(args)
    else
      call = target_instances[key] = Target::Call.new( klass, args )
    end
  else
    unkeyed_instances << call = Target::Call.new( klass, args )
  end

  call.target ||= klass.new

  call
end

#target_classesObject



10
11
12
# File 'lib/angry_mob/target/mother.rb', line 10

def target_classes
  Target::Tracking.subclasses
end

#target_instancesObject



32
33
34
# File 'lib/angry_mob/target/mother.rb', line 32

def target_instances
  @target_instances ||= {}
end

#unkeyed_instancesObject



36
37
38
# File 'lib/angry_mob/target/mother.rb', line 36

def unkeyed_instances
  @unkeyed_instances ||= []
end