Class: Aker::GroupMemberships

Inherits:
Array
  • Object
show all
Defined in:
lib/aker/group_membership.rb

Overview

An authority-independent collection of all the group memberships for a particular user at a particular portal.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (GroupMemberships) initialize(portal)

Create a new instance.

Parameters:

  • portal (#to_sym)


73
74
75
# File 'lib/aker/group_membership.rb', line 73

def initialize(portal)
  @portal = portal.to_sym
end

Instance Attribute Details

- (Symbol) portal (readonly)

The portal for which all these group memberships apply.

Returns:

  • (Symbol)


64
65
66
# File 'lib/aker/group_membership.rb', line 64

def portal
  @portal
end

Instance Method Details

- (Array<GroupMembership>) find(group, *affiliate_ids)

Finds the group memberships that match the given group, possibly constrained by one or more affiliates.

(Note that this method hides the Enumerable method find. You can still use it under its detect alias.)

Parameters:

  • group (Group, #to_s)

    the group in question or its name

  • *affiliate_ids (Array<Object>, nil)

    the affiliates to use to constrain the query.

Returns:



108
109
110
111
112
# File 'lib/aker/group_membership.rb', line 108

def find(group, *affiliate_ids)
  candidates = self.select { |gm| gm.group.include?(group) }
  return candidates if affiliate_ids.empty?
  candidates.select { |gm| affiliate_ids.detect { |id| gm.include_affiliate?(id) } }
end

- (Boolean) include?(group, *affiliate_ids)

Determines whether this collection indicates that the user is authorized in the the given group, possibly constrained by one or more affiliates.

(Note that this method hides the superclass include? method.)

Parameters:

  • group (Group, #to_s)

    the group in question or its name

  • *affiliate_ids (Array<Object>, nil)

    the affiliates to use to constrain the query.

Returns:

  • (Boolean)

    true so long as the user is authorized in group for at least one of the specified affiliates. If no affiliates are specified, only the groups themselves are considered.



92
93
94
# File 'lib/aker/group_membership.rb', line 92

def include?(group, *affiliate_ids)
  !find(group, *affiliate_ids).empty?
end

- (Hash) marshal_dump

Custom serialization for this array. Needed because we need to serialize the full tree for all referenced groups in order to be able to do #include? and #find correctly on the deserialized result.

Returns:



121
122
123
124
125
126
127
# File 'lib/aker/group_membership.rb', line 121

def marshal_dump
  {
    :group_roots => find_group_roots,
    :memberships => dump_gm_hashes,
    :portal => portal
  }
end

- marshal_load(dump)

This method returns an undefined value.

Custom deserialization for this array. Reverses #marshal_dump.



134
135
136
137
138
139
140
141
# File 'lib/aker/group_membership.rb', line 134

def marshal_load(dump)
  @portal = dump[:portal]
  roots = dump[:group_roots]
  dump[:memberships].each do |gm_hash|
    self << GroupMembership.new(find_group_from_roots(gm_hash[:group_name], roots)).
      tap { |gm| gm.affiliate_ids.concat(gm_hash[:affiliate_ids]) }
  end
end