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'.freeze

Instance Method Summary collapse

Instance Method Details

#dataObject



7
8
9
# File 'lib/objects/registry/loader.rb', line 7

def data
  @data ||= do_load
end

#do_loadObject



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

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



37
38
39
# File 'lib/objects/registry/loader.rb', line 37

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

#restrict_config?(node, command_config) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
# File 'lib/objects/registry/loader.rb', line 41

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]

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

  !node.is_a?(node_klass_mapping)
end

#restriction_to_node_klass_mappingsObject



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

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/objects/registry/loader.rb', line 11

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.to_s}$/.match(node.keyed_namespace)
        commands.each do |c|
          # 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
          if node.is_node_level_method?(c[:console_command]) || command_group.console_method_name_exists?(c[:console_command])
            raise Bcome::Exception::MethodNameConflictInRegistry, "'#{c[:console_command]}'"
          end
          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