Class: Bcome::Registry::Loader

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/objects/registry/loader.rb

Constant Summary collapse

FILE_PATH =
'bcome/registry.yml'

Instance Method Summary collapse

Instance Method Details

#dataObject



9
10
11
# File 'lib/objects/registry/loader.rb', line 9

def data
  @data ||= do_load
end

#do_loadObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/objects/registry/loader.rb', line 69

def do_load
  return {} unless File.exist?(FILE_PATH)

  begin
    file_data = YAML.load_file(FILE_PATH).deep_symbolize_keys
  rescue Psych::SyntaxError => e
    raise Bcome::Exception::InvalidRegistryDataConfig, "Error: #{e.message}"
  end
  file_data
end

#init_new_command_group(node) ⇒ Object



47
48
49
# File 'lib/objects/registry/loader.rb', line 47

def init_new_command_group(node)
  ::Bcome::Registry::Command::Group.new(node)
end

#restrict_config?(node, command_config) ⇒ Boolean



51
52
53
54
55
56
57
58
59
# File 'lib/objects/registry/loader.rb', line 51

def restrict_config?(node, command_config)
  return false unless command_config.key?(:restrict_to_node)

  node_klass_mapping = restriction_to_node_klass_mappings[command_config[:restrict_to_node].to_sym]

  raise Bcome::Exception::InvalidRestrictionKeyInRegistry, "'#{command_config[:restrict_to_node]}' is invalid. Valid keys: #{restriction_to_node_klass_mappings.keys.join(', ')}" unless node_klass_mapping

  !node.is_a?(node_klass_mapping)
end

#restriction_to_node_klass_mappingsObject



61
62
63
64
65
66
67
# File 'lib/objects/registry/loader.rb', line 61

def restriction_to_node_klass_mappings
  {
    server: ::Bcome::Node::Server::Base,
    inventory: ::Bcome::Node::Inventory,
    collection: ::Bcome::Node::Collection
  }
end

#set_command_group_for_node(node) ⇒ Object



13
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
# File 'lib/objects/registry/loader.rb', line 13

def set_command_group_for_node(node)
  if group_for_node = ::Bcome::Registry::CommandList.instance.group_for_node(node)
    return group_for_node
  end

  command_group = init_new_command_group(node)

  data.each do |key, commands|
    begin
      if /^#{key}$/.match(node.keyed_namespace)

        next if commands.nil?

        commands.each do |c|
          unless c[:console_command]
            error_message = "Registry method is missing key 'console_command'."
            error_message += "\n\n#{c.inspect}"
            raise Bcome::Exception::InvalidRegistryDataConfig, error_message
          end

          # Verify that the proposed user registered method does not conflict with either an existing method name, instance var, or other registry command name for this node
          raise Bcome::Exception::MethodNameConflictInRegistry, "'#{c[:console_command]}'" if node.is_node_level_method?(c[:console_command]) || command_group.console_method_name_exists?(c[:console_command])

          command_group << ::Bcome::Registry::Command::Base.new_from_raw_command(c) unless restrict_config?(node, c)
          ::Bcome::Registry::CommandList.instance.register(node, c[:console_command].to_sym)
        end
      end
    rescue RegexpError => e
      raise Bcome::Exception::InvalidRegexpMatcherInRegistry, e.message
    end
  end
  ::Bcome::Registry::CommandList.instance.add_group_for_node(node, command_group)
end