Class: Runner::MonkeyRun

Inherits:
Object
  • Object
show all
Defined in:
lib/tasks/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(monkey_service, runtime) ⇒ MonkeyRun

Returns a new instance of MonkeyRun.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tasks/engine.rb', line 15

def initialize(monkey_service, runtime)
  @words = Array.new
  @monkey_service = monkey_service
  @runtime = runtime.to_i

  @monkey_service.add_observer self

  @thread = Thread.new {
    go
    sleep @runtime
  }

  self
end

Instance Attribute Details

#monkey_serviceObject (readonly)

Returns the value of attribute monkey_service.



13
14
15
# File 'lib/tasks/engine.rb', line 13

def monkey_service
  @monkey_service
end

#runtimeObject (readonly)

Returns the value of attribute runtime.



13
14
15
# File 'lib/tasks/engine.rb', line 13

def runtime
  @runtime
end

#threadObject (readonly)

Returns the value of attribute thread.



13
14
15
# File 'lib/tasks/engine.rb', line 13

def thread
  @thread
end

#wordsObject (readonly)

Returns the value of attribute words.



13
14
15
# File 'lib/tasks/engine.rb', line 13

def words
  @words
end

Instance Method Details

#goObject

protected



32
33
34
35
36
37
# File 'lib/tasks/engine.rb', line 32

def go
  @monkey_service.add(MonkeyFactory::create :groucho)
  @monkey_service.add(MonkeyFactory::create :harpo)
  @monkey_service.add(MonkeyFactory::create :chico)
  @monkey_service.add(MonkeyFactory::create :zeppo)
end

#update(time, action, param) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tasks/engine.rb', line 39

def update(time, action, param)
  begin

    return unless param.is_a?(Hash) && param.has_key?(:action)

    if param[:action].is_a?(MonkeyActionType) && action == :action_completed
    
      monkey = param[:action].monkey.monkey_symbol.to_s
      is_word = param[:action].keyboard_input.is_word
      word = param[:action].keyboard_input.input_to_s

      #if param[:action].is_a?(MonkeyActionType) && action == :action_completed
        puts "Monkey: [#{monkey.capitalize}] | Is Word: [#{is_word}] | Value: [#{word.capitalize}]"
      #end

      if is_word
        @words << { word: word, monkey: monkey }
      end
    end
  rescue Exception => e
    puts "Exception: #{e}"
  end
end