Class: LcpRuby::Groups::YamlLoader

Inherits:
Object
  • Object
show all
Includes:
Contract
Defined in:
lib/lcp_ruby/groups/yaml_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Contract

#roles_for_user

Constructor Details

#initializeYamlLoader

Returns a new instance of YamlLoader.



8
9
10
# File 'lib/lcp_ruby/groups/yaml_loader.rb', line 8

def initialize
  @group_definitions = {}
end

Instance Attribute Details

#group_definitionsObject (readonly)

Returns the value of attribute group_definitions.



6
7
8
# File 'lib/lcp_ruby/groups/yaml_loader.rb', line 6

def group_definitions
  @group_definitions
end

Instance Method Details

#all_group_namesArray<String>

Returns:

  • (Array<String>)


43
44
45
# File 'lib/lcp_ruby/groups/yaml_loader.rb', line 43

def all_group_names
  @group_definitions.keys.sort
end

#groups_for_user(user) ⇒ Array<String>

Returns group names the user belongs to by calling the configured group_method.

Parameters:

  • user (Object)

Returns:

  • (Array<String>)


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lcp_ruby/groups/yaml_loader.rb', line 50

def groups_for_user(user)
  return [] unless user

  group_method = LcpRuby.configuration.group_method
  return [] unless user.respond_to?(group_method)

  user_groups = Array(user.send(group_method)).map(&:to_s)
  unknown = user_groups - @group_definitions.keys
  if unknown.any?
    log_warn("User has groups not defined in YAML: #{unknown.join(', ')}")
  end
  user_groups.select { |g| @group_definitions.key?(g) }
end

#load(base_path) ⇒ Object

Loads group definitions from groups.yml/groups.yaml in the metadata path.

Parameters:

  • base_path (Pathname, String)

    the metadata directory



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
# File 'lib/lcp_ruby/groups/yaml_loader.rb', line 14

def load(base_path)
  base_path = Pathname.new(base_path)
  file_path = find_groups_file(base_path)
  unless file_path
    log_warn("No groups.yml or groups.yaml found in #{base_path}; no groups will be loaded")
    return
  end

  data = YAML.safe_load_file(file_path, permitted_classes: [ Symbol, Regexp ])
  unless data
    log_warn("#{file_path} is empty; no groups will be loaded")
    return
  end

  groups_data = data["groups"]
  unless groups_data.is_a?(Array)
    log_warn("#{file_path} missing 'groups' array key; no groups will be loaded")
    return
  end

  groups_data.each do |group_hash|
    definition = Metadata::GroupDefinition.from_hash(group_hash)
    @group_definitions[definition.name] = definition
  end
rescue Psych::SyntaxError => e
  raise MetadataError, "YAML syntax error in #{file_path}: #{e.message}"
end

#roles_for_group(group_name) ⇒ Array<String>

Parameters:

  • group_name (String)

Returns:

  • (Array<String>)


66
67
68
69
70
71
# File 'lib/lcp_ruby/groups/yaml_loader.rb', line 66

def roles_for_group(group_name)
  definition = @group_definitions[group_name.to_s]
  return [] unless definition

  definition.roles
end

#user_ids_for_group(_group_name) ⇒ Array<Integer>

YAML loader cannot enumerate users — returns empty array.

Parameters:

  • group_name (String)

Returns:

  • (Array<Integer>)


76
77
78
# File 'lib/lcp_ruby/groups/yaml_loader.rb', line 76

def user_ids_for_group(_group_name)
  []
end