Class: Hubeye::Server::Strategies::Decision

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hubeye/server/strategies/decision.rb

Constant Summary collapse

InvalidInput =
Class.new(StandardError)
@@strategy_classes =

Get all the strategy classes from the files names in the /server/strategies/ directory

[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, options = {}) ⇒ Decision

Returns a new instance of Decision.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hubeye/server/strategies/decision.rb', line 45

def initialize(server, options={})
  @server = server
  opts = {:internal_input => nil}.merge options
  invalid_input = lambda {
    @server.remote_connection = false
    throw(:invalid_input)
  }

  if !opts[:internal_input]
    begin
      @input = socket.read_all
    rescue => e
      STDOUT.puts e
      invalid_input.call
    end
    # check if the client pressed ^C or ^D
    if @input.nil?
      invalid_input.call
    end
  else
    @input = opts[:internal_input]
  end
  @input = @input.strip.downcase
  @input.gsub! /diiv/, '/'
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



40
41
42
# File 'lib/hubeye/server/strategies/decision.rb', line 40

def input
  @input
end

#serverObject (readonly)

Returns the value of attribute server.



40
41
42
# File 'lib/hubeye/server/strategies/decision.rb', line 40

def server
  @server
end

Instance Method Details

#call_strategyObject

Raises:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/hubeye/server/strategies/decision.rb', line 103

def call_strategy
  STRATEGIES.each do |inp,strat|
    if inp.respond_to? :match
      if m = @input.match(inp)
        return strat.call(self, m)
      end
    elsif inp.respond_to? :call
      if inp.call(@input)
        return strat.call(self)
      end
    end
  end
  raise InvalidInput
end