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



67
68
69
70
71
72
73
74
75
# File 'lib/objects/registry/loader.rb', line 67

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



44
45
46
# File 'lib/objects/registry/loader.rb', line 44

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

#restrict_config?(node, command_config) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
# File 'lib/objects/registry/loader.rb', line 48

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



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

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
36
37
38
39
40
41
42
# 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|

          unless c[:console_command]
            error_message = "Registry method is missing key 'console_command'."
            error_message += "\n\n#{c.inspect}"
            raise Bcome::Exception::InvalidRegistryDataConfig.new 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
          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