Class: Lignite::DirectCommands

Inherits:
Object
  • Object
show all
Includes:
VariableDeclarer
Defined in:
lib/lignite/direct_commands.rb

Instance Method Summary collapse

Methods included from VariableDeclarer

#array8, #data16, #data32, #data8, #dataf, #datas

Constructor Details

#initialize(conn = Connection.create) ⇒ DirectCommands

Returns a new instance of DirectCommands.

Parameters:

  • conn (Connection) (defaults to: Connection.create)


4
5
6
7
8
# File 'lib/lignite/direct_commands.rb', line 4

def initialize(conn = Connection.create)
  @op_compiler = OpCompiler.new
  @sender = MessageSender.new(conn)
  @globals = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/lignite/direct_commands.rb', line 37

def method_missing(name, *args)
  if @op_compiler.respond_to?(name)
    insb = @op_compiler.send(name, *args)
    @sender.direct_command(insb)
  else
    super
  end
end

Instance Method Details

#block(&body) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lignite/direct_commands.rb', line 23

def block(&body)
  locals = Variables.new
  bodyc = BodyCompiler.new(@globals, locals)
  bodyc.instance_exec(&body)

  bs = bodyc.bytes
  lsize = locals.bytesize
  if @globals
    @sender.direct_command_with_reply(bs, global_size: @globals.bytesize, local_size: lsize)
  else
    @sender.direct_command(bs, global_size: 0, local_size: lsize)
  end
end

#variablesObject



10
11
12
# File 'lib/lignite/direct_commands.rb', line 10

def variables
  @globals
end

#with_reply(&body) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/lignite/direct_commands.rb', line 15

def with_reply(&body)
  @globals = Variables.new
  ret_bytes = instance_exec(&body)
  ret = @globals.unpack(ret_bytes)
  @globals = nil
  ret # TODO decode according to type
end