Class: GetTypeHandler

Inherits:
AbstractCommandHandler show all
Defined in:
lib/javonet-ruby-sdk/core/handler/get_type_handler.rb

Instance Method Summary collapse

Methods inherited from AbstractCommandHandler

#handle_command, #iterate

Constructor Details

#initializeGetTypeHandler

Returns a new instance of GetTypeHandler.



8
9
10
11
12
# File 'lib/javonet-ruby-sdk/core/handler/get_type_handler.rb', line 8

def initialize
  @required_parameters_count = 1
  @namespace_cache = NamespaceCache.instance
  @type_cache = TypeCache.instance
end

Instance Method Details

#process(command) ⇒ Object



14
15
16
17
18
19
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
45
46
47
48
49
50
# File 'lib/javonet-ruby-sdk/core/handler/get_type_handler.rb', line 14

def process(command)
  raise "#{self.class.name} parameters mismatch!" if command.payload.length < @required_parameters_count

  type_to_return = _get_type_from_payload(command)

  if type_to_return.nil?
    message = "Type #{command.payload[0]} not found.\n"
    message += "\nLoaded libraries:\n"
    loaded_libraries = LoadLibraryHandler.get_loaded_libraries
    # find types in loaded libraries

    loaded_libraries.each do |library|
      message += "#{library}\n"
    end
    raise ArgumentError, message
  end

  if (@namespace_cache.is_namespace_cache_empty? && @type_cache.is_type_cache_empty?) || # both caches are empty

     @namespace_cache.is_type_allowed(type_to_return) || # namespace is allowed

     @type_cache.is_type_allowed(type_to_return)
    # continue - type is allowed

  else
    allowed_namespaces = @namespace_cache.get_cached_namespaces.join(', ')
    allowed_types = @type_cache.get_cached_types.join(', ')
    raise "Type #{type_to_return.name} not allowed. \nAllowed namespaces: #{allowed_namespaces}\nAllowed types: #{allowed_types}"
  end

  type_to_return
rescue NameError => e
  message = e.message
  message += "\nLoaded libraries:\n"
  loaded_libraries = LoadLibraryHandler.get_loaded_libraries
  # find types in loaded libraries

  loaded_libraries.each do |library|
    message += "#{library}\n"
  end
  raise ArgumentError, message
end