Class: BackgroundQueue::Command
- Inherits:
-
Object
- Object
- BackgroundQueue::Command
- 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
-
#args ⇒ Object
Returns the value of attribute args.
-
#code ⇒ Object
Returns the value of attribute code.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
-
.from_buf(buf) ⇒ Object
load a command from a string.
Instance Method Summary collapse
-
#initialize(code, options, args) ⇒ Command
constructor
A new instance of Command.
-
#to_buf ⇒ Object
convert the command to a string (currently json) to get sent.
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, , args) @code = code @options = BackgroundQueue::Utils::AnyKeyHash.new() @args = BackgroundQueue::Utils::AnyKeyHash.new(args) end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
10 11 12 |
# File 'lib/background_queue/command.rb', line 10 def args @args end |
#code ⇒ Object
Returns the value of attribute code.
8 9 10 |
# File 'lib/background_queue/command.rb', line 8 def code @code end |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/background_queue/command.rb', line 9 def @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.}" 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? = hash_data['o'] BackgroundQueue::Command.new(code, , args) rescue Exception=>e raise InvalidCommand, "Error loading command from buffer: #{e.}" end end |
Instance Method Details
#to_buf ⇒ Object
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 |