Class: Radiator::Operation

Inherits:
Object
  • Object
show all
Includes:
OperationIds, OperationTypes, Utils
Defined in:
lib/radiator/operation.rb

Constant Summary

Constants included from OperationTypes

Radiator::OperationTypes::TYPES

Constants included from OperationIds

Radiator::OperationIds::IDS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#debug, #error, #extract_signatures, #hexlify, #pakArr, #pakC, #pakHash, #pakI, #pakL!, #pakS, #pakStr, #pakc, #paks, #send_log, #unhexlify, #varint, #warning

Methods included from OperationTypes

#type

Methods included from OperationIds

#id

Constructor Details

#initialize(options = {}) ⇒ Operation

Returns a new instance of Operation.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/radiator/operation.rb', line 7

def initialize(options = {})
  opt = options.dup
  @type = opt.delete(:type)
  
  opt.each do |k, v|
    instance_variable_set("@#{k}", type(@type, k, v))
  end
  
  unless Operation::known_operation_names.include? @type
    raise OperationError, "Unsupported operation type: #{@type}"
  end
end

Class Method Details

.broadcast_operationsObject



70
71
72
# File 'lib/radiator/operation.rb', line 70

def self.broadcast_operations
  @broadcast_operations ||= JSON[File.read broadcast_operations_json_path]
end

.broadcast_operations_json_pathObject



66
67
68
# File 'lib/radiator/operation.rb', line 66

def self.broadcast_operations_json_path
  @broadcast_operations_json_path ||= "#{File.dirname(__FILE__)}/broadcast_operations.json"
end

.known_operation_namesObject



74
75
76
# File 'lib/radiator/operation.rb', line 74

def self.known_operation_names
  broadcast_operations.map { |op| op["operation"].to_sym }
end

.param_names(type) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/radiator/operation.rb', line 78

def self.param_names(type)
  broadcast_operations.each do |op|
    if op['operation'].to_sym == type.to_sym
      return op['params'].map(&:to_sym)
    end
  end
end

Instance Method Details

#payloadObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/radiator/operation.rb', line 46

def payload
  params = {}
  
  Operation::param_names(@type.to_sym).each do |p|
    next unless defined? p
    
    v = instance_variable_get("@#{p}")
    next if v.nil?
    next if v.class == Radiator::Type::Future
    
    params[p] = case v
    when Radiator::Type::Beneficiaries then [[0, v.to_h]]
    when Radiator::Type::Amount then v.to_a
    else; v
    end
  end
  
  [@type, params]
end

#to_bytesObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/radiator/operation.rb', line 20

def to_bytes
  bytes = [id(@type.to_sym)].pack('C')
  
  Operation::param_names(@type.to_sym).each do |p|
    next unless defined? p
    
    v = instance_variable_get("@#{p}")
    bytes += v.to_bytes and next if v.respond_to? :to_bytes
    
    bytes += case v
    when Symbol then pakStr(v.to_s)
    when String then pakStr(v)
    when Integer then paks(v)
    when TrueClass then pakC(1)
    when FalseClass then pakC(0)
    when Array then pakArr(v)
    when Hash then pakHash(v)
    when NilClass then next
    else
      raise OperationError, "Unsupported type: #{v.class}"
    end
  end
  
  bytes
end