class Spark::CommandBuilder
Builder for building correct {Spark::Command}
Attributes
command[R]
Public Class Methods
new(serializer, deserializer=nil)
click to toggle source
# File lib/spark/command_builder.rb, line 20 def initialize(serializer, deserializer=nil) create_command self.serializer = serializer self.deserializer = deserializer || serializer.dup end
Public Instance Methods
add_command(klass, *args)
click to toggle source
# 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)
click to toggle source
# File lib/spark/command_builder.rb, line 68 def add_library(*libraries) @command.libraries += libraries end
bind(objects)
click to toggle source
# File lib/spark/command_builder.rb, line 72 def bind(objects) objects.symbolize_keys! @command.bound_objects.merge!(objects) end
build()
click to toggle source
Serialize Command
class for worker Java use signed number
# File lib/spark/command_builder.rb, line 45 def build unpack_chars(Marshal.dump(@command)) end
create_command()
click to toggle source
# File lib/spark/command_builder.rb, line 26 def create_command @command = Spark::Command.new end
deep_copy()
click to toggle source
Do not user Marshal.dump(Marshal.load(self)) because some variables have marshal_dump prepared for worker.
# 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
Private Instance Methods
serialize_function(func)
click to toggle source
Serialized can be Proc and Method
Func¶ ↑
-
string: already serialized proc
-
proc: proc
-
symbol: name of method
-
method: Method class
# File lib/spark/command_builder.rb, line 87 def serialize_function(func) case func when String serialize_function_from_string(func) when Symbol serialize_function_from_symbol(func) when Proc serialize_function_from_proc(func) when Method serialize_function_from_method(func) else raise Spark::CommandError, 'You must enter String, Symbol, Proc or Method.' end end
serialize_function_from_method(meth)
click to toggle source
Serialize method as string
def test(x) x*x end serialize_function_from_method(method(:test)) # => "def test(x)\n x*x\nend\n"
# File lib/spark/command_builder.rb, line 130 def serialize_function_from_method(meth) if pry? meth = Pry::Method.new(meth) end {type: 'method', name: meth.name, content: meth.source} rescue raise Spark::SerializeError, 'Method can not be serialized. Use full path or Proc.' end
serialize_function_from_proc(proc)
click to toggle source
Serialize Proc as String
lambda{|x| x*x}.to_source # => "proc { |x| (x * x) }"
# File lib/spark/command_builder.rb, line 115 def serialize_function_from_proc(proc) serialize_function_from_string(proc.to_source) rescue raise Spark::SerializeError, 'Proc can not be serialized. Use String instead.' end
serialize_function_from_string(string)
click to toggle source
# File lib/spark/command_builder.rb, line 102 def serialize_function_from_string(string) {type: 'proc', content: string} end
serialize_function_from_symbol(symbol)
click to toggle source
# File lib/spark/command_builder.rb, line 106 def serialize_function_from_symbol(symbol) {type: 'symbol', content: symbol} end