Class: LcpRuby::Groups::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/lcp_ruby/groups/registry.rb

Class Method Summary collapse

Class Method Details

.all_group_namesArray<String>

Returns all group names from the configured loader. Results are cached until reload! is called.

Returns:

  • (Array<String>)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lcp_ruby/groups/registry.rb', line 12

def all_group_names
  return [] unless available?

  result, operation = monitor.synchronize do
    if @cache
      [ @cache, :hit ]
    else
      @cache = loader.all_group_names
      [ @cache, :miss ]
    end
  end
  ActiveSupport::Notifications.instrument("cache.lcp_ruby", cache: "groups", operation: operation.to_s)
  result
end

.available?Boolean

Whether the groups subsystem is configured and ready.

Returns:

  • (Boolean)


92
93
94
# File 'lib/lcp_ruby/groups/registry.rb', line 92

def available?
  @available == true
end

.clear!Object

Full reset — called from LcpRuby.reset!



83
84
85
86
87
88
89
# File 'lib/lcp_ruby/groups/registry.rb', line 83

def clear!
  monitor.synchronize do
    @available = false
    @loader = nil
    @cache = nil
  end
end

.groups_for_user(user) ⇒ Array<String>

Returns group names the given user belongs to. Not cached — user identity varies per request.

Parameters:

  • user (Object)

Returns:

  • (Array<String>)


31
32
33
34
35
# File 'lib/lcp_ruby/groups/registry.rb', line 31

def groups_for_user(user)
  return [] unless available?

  loader.groups_for_user(user)
end

.mark_available!Object

Mark registry as available (called after setup completes).



97
98
99
# File 'lib/lcp_ruby/groups/registry.rb', line 97

def mark_available!
  @available = true
end

.reload!Object

Clears the cached group names, forcing a reload on next access.



75
76
77
78
79
80
# File 'lib/lcp_ruby/groups/registry.rb', line 75

def reload!
  monitor.synchronize do
    @cache = nil
  end
  ActiveSupport::Notifications.instrument("cache.lcp_ruby", cache: "groups", operation: "invalidate")
end

.roles_for_group(group_name) ⇒ Array<String>

Returns role names mapped to the given group.

Parameters:

  • group_name (String)

Returns:

  • (Array<String>)


40
41
42
43
44
# File 'lib/lcp_ruby/groups/registry.rb', line 40

def roles_for_group(group_name)
  return [] unless available?

  loader.roles_for_group(group_name)
end

.roles_for_user(user) ⇒ Array<String>

Returns all roles derived from a user’s group memberships. Delegates to the loader (which may optimize with a single query).

Parameters:

  • user (Object)

Returns:

  • (Array<String>)


50
51
52
53
54
# File 'lib/lcp_ruby/groups/registry.rb', line 50

def roles_for_user(user)
  return [] unless available?

  loader.roles_for_user(user)
end

.set_loader(loader_instance) ⇒ Object

Sets the loader that backs this registry.

Parameters:

  • loader_instance (Object)

    must include Groups::Contract



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

def set_loader(loader_instance)
  monitor.synchronize do
    @loader = loader_instance
    @cache = nil
  end
end

.user_ids_for_group(group_name) ⇒ Array<Integer>

Returns user IDs that belong to the given group.

Parameters:

  • group_name (String)

Returns:

  • (Array<Integer>)


59
60
61
62
63
# File 'lib/lcp_ruby/groups/registry.rb', line 59

def user_ids_for_group(group_name)
  return [] unless available?

  loader.user_ids_for_group(group_name)
end