Class: BackgroundQueue::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/background_queue/command.rb

Overview

store a command and all its parameters as a hash to be serialized when passing to/from the server.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, options, args) ⇒ Command

Returns a new instance of Command.



12
13
14
15
16
# File 'lib/background_queue/command.rb', line 12

def initialize(code, options, args)
  @code = code
  @options = BackgroundQueue::Utils::AnyKeyHash.new(options)
  @args = BackgroundQueue::Utils::AnyKeyHash.new(args)
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



10
11
12
# File 'lib/background_queue/command.rb', line 10

def args
  @args
end

#codeObject

Returns the value of attribute code.



8
9
10
# File 'lib/background_queue/command.rb', line 8

def code
  @code
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/background_queue/command.rb', line 9

def options
  @options
end

Class Method Details

.from_buf(buf) ⇒ Object

load a command from a string



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/background_queue/command.rb', line 24

def self.from_buf(buf)
  hash_data = nil
  begin
    hash_data = JSON.load(buf)
  rescue Exception=>e
    raise InvalidCommand, "Invalid data format (should be json) when loading command from buffer: #{e.message}"
  end
  begin
    raise "Missing 'c' (code)" if hash_data['c'].nil?
    code = hash_data['c'].intern
    raise "Missing 'a' (args)" if hash_data['a'].nil?
    args = hash_data['a']
    raise "Missing 'o' (options)" if hash_data['o'].nil?
    options = hash_data['o']
    BackgroundQueue::Command.new(code, options, args)
  rescue Exception=>e
    raise InvalidCommand, "Error loading command from buffer: #{e.message}"
  end
end

Instance Method Details

#to_bufObject

convert the command to a string (currently json) to get sent



19
20
21
# File 'lib/background_queue/command.rb', line 19

def to_buf
  {:c=>@code, :a=>@args.hash, :o=>@options.hash}.to_json
end