Module: QB::Util::Interop

Includes:
NRSER::Log::Mixin
Defined in:
lib/qb/util/interop.rb

Class Method Summary collapse

Class Method Details

.receivereturn_type

TODO:

Document receive method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/qb/util/interop.rb', line 54

def receive
  logger.debug "Starting #receive..."
  
  # method body
  yaml = $stdin.read
  
  payload = YAML.load yaml
  
  logger.debug "Parsed",
    payload: payload
  
  # data = payload.fetch 'data'
  method = payload.fetch 'method'
  args = payload['args'] || []
  kwds = payload['kwds'] || {}
  args << kwds.symbolize_keys unless kwds.empty?
  
  result = if payload['data']
    send_to_instance payload['data'], method, *args
    
  elsif payload['const']
    send_to_const payload['const'], method, *args
    
  else
    raise ArgumentError.new binding.erb <<-ERB
      Expected payload to have 'data' or 'const' keys, neither found:
      
      Payload:
      
          <%= payload.pretty_inspect %>
      
      Input YAML:
      
          <%= yaml %>
      
    ERB
  end
  
  logger.debug "send succeeded", result: result
  
  yaml = result.to_yaml # don't work: sort_keys: true, use_header: true
  
  logger.debug "writing YAML:\n\n#{ yaml }"
  
  $stdout.write yaml
  
  logger.debug "done."
end

.send_to_const(name, method_name, *args) ⇒ return_type

TODO:

Document receive method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



34
35
36
37
38
39
40
41
42
43
# File 'lib/qb/util/interop.rb', line 34

def send_to_const name, method_name, *args
  logger.debug "Starting #send_to_const..."
  
  const = name.to_const
  
  logger.debug "Found constant", const: const
  
  const.public_send method_name, *args
  
end

.send_to_instance(data, method_name, *args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/qb/util/interop.rb', line 12

def send_to_instance data, method_name, *args
  logger.debug "Starting #send_to_instance..."
  
  obj = if  data.is_a?( Hash ) &&
            data.key?( NRSER::Props::DEFAULT_CLASS_KEY )
    NRSER::Props.UNSAFE_load_instance_from_data data
  else
    data
  end
  
  obj.send method_name, *args
end