Class: Spark::CommandBuilder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
CommandValidator, Helper::Serialize, Helper::System
Defined in:
lib/spark/command_builder.rb

Overview

Builder for building correct Command

Constant Summary

Constants included from Helper::Serialize

Helper::Serialize::DIRECTIVE_CHARS, Helper::Serialize::DIRECTIVE_DOUBLES_BIG_ENDIAN, Helper::Serialize::DIRECTIVE_DOUBLE_BIG_ENDIAN, Helper::Serialize::DIRECTIVE_INTEGERS_BIG_ENDIAN, Helper::Serialize::DIRECTIVE_INTEGER_BIG_ENDIAN, Helper::Serialize::DIRECTIVE_LONGS_BIG_ENDIAN, Helper::Serialize::DIRECTIVE_LONG_BIG_ENDIAN, Helper::Serialize::DIRECTIVE_UNSIGNED_CHARS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandValidator

#valid?, #validate, #validate_size, #validate_type

Methods included from Helper::System

included

Methods included from Helper::Serialize

#pack_double, #pack_doubles, #pack_int, #pack_ints, #pack_long, #pack_longs, #pack_unsigned_chars, #unpack_chars, #unpack_int, #unpack_long

Constructor Details

#initialize(serializer, deserializer = nil) ⇒ CommandBuilder

Returns a new instance of CommandBuilder.



20
21
22
23
24
# File 'lib/spark/command_builder.rb', line 20

def initialize(serializer, deserializer=nil)
  create_command
  self.serializer   = serializer
  self.deserializer = deserializer || serializer.dup
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



15
16
17
# File 'lib/spark/command_builder.rb', line 15

def command
  @command
end

Instance Method Details

#add_command(klass, *args) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/spark/command_builder.rb', line 49

def add_command(klass, *args)
  variables = klass.settings.variables
  validate_size(variables, args)

  built_args = []
  variables.values.zip(args) do |var, arg|
    if var[:function]
      arg = serialize_function(arg)
    end

    validate(arg, var)
    built_args << arg
  end

  comm = klass.new(*built_args)
  @command.commands << comm
  self
end

#add_library(*libraries) ⇒ Object



68
69
70
# File 'lib/spark/command_builder.rb', line 68

def add_library(*libraries)
  @command.libraries += libraries
end

#bind(objects) ⇒ Object



72
73
74
75
# File 'lib/spark/command_builder.rb', line 72

def bind(objects)
  objects.symbolize_keys!
  @command.bound_objects.merge!(objects)
end

#buildObject

Serialize Command class for worker Java use signed number



45
46
47
# File 'lib/spark/command_builder.rb', line 45

def build
  unpack_chars(Marshal.dump(@command))
end

#create_commandObject



26
27
28
# File 'lib/spark/command_builder.rb', line 26

def create_command
  @command = Spark::Command.new
end

#deep_copyObject

Do not user Marshal.dump(Marshal.load(self)) because some variables have marshal_dump prepared for worker.



32
33
34
35
36
37
38
39
40
41
# File 'lib/spark/command_builder.rb', line 32

def deep_copy
  copy = self.dup
  copy.create_command
  copy.serializer    = self.serializer.deep_copy
  copy.deserializer  = self.deserializer.deep_copy
  copy.commands      = self.commands.dup
  copy.libraries     = self.libraries.dup
  copy.bound_objects = self.bound_objects.dup
  copy
end