Class: Osm::Role

Inherits:
Object
  • Object
show all
Defined in:
lib/osm/role.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Role

Initialize a new UserRole using the hash returned by the API call

Parameters:

  • data

    the hash of data for the object returned by the API



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/osm/role.rb', line 17

def initialize(data)
  @section = Osm::Section.new(data['sectionid'], data['sectionname'], ActiveSupport::JSON.decode(data['sectionConfig']), self)
  @group_name = data['groupname']
  @group_id = Osm::to_i_or_nil(data['groupid'])
  @permissions = data['permissions'].is_a?(Hash) ? Osm::symbolize_hash(data['permissions']) : {}

  # Convert permission values to a number
  @permissions.each_key do |key|
    @permissions[key] = @permissions[key].to_i
  end
end

Instance Attribute Details

#group_idFixnum (readonly)

Returns the group the section is in.

Returns:

  • (Fixnum)

    the group the section is in



6
7
8
# File 'lib/osm/role.rb', line 6

def group_id
  @group_id
end

#group_nameString (readonly)

Returns the name of the group the section is in.

Returns:

  • (String)

    the name of the group the section is in



6
7
8
# File 'lib/osm/role.rb', line 6

def group_name
  @group_name
end

#permissionsHash (readonly)

Returns the permissions the user has in this role.

Returns:

  • (Hash)

    the permissions the user has in this role



6
7
8
# File 'lib/osm/role.rb', line 6

def permissions
  @permissions
end

#sectionOsm::Section (readonly)

Returns the section this role related to.

Returns:



6
7
8
# File 'lib/osm/role.rb', line 6

def section
  @section
end

Instance Method Details

#<=>(another_role) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/osm/role.rb', line 55

def <=>(another_role)
  compare_group_name = self.group_name <=> another_role.group_name
  return compare_group_name unless compare_group_name == 0

  return 0 if self.section.type == another_role.section.type
  [:beavers, :cubs, :scouts, :explorers, :waiting, :adults].each do |type|
    return -1 if self.section.type == type
    return 1 if another_role.section.type == type
  end
end

#==(another_role) ⇒ Object



66
67
68
# File 'lib/osm/role.rb', line 66

def ==(another_role)
  self.section == another_role.try(:section)
end

#can_read?(key) ⇒ Boolean

Determine if this role has read access for the provided permission

Parameters:

  • key (Symbol)

    the permission being queried

Returns:

  • (Boolean)

    if this role can read the passed permission



32
33
34
# File 'lib/osm/role.rb', line 32

def can_read?(key)
  return [10, 20, 100].include?(@permissions[key])
end

#can_write?(key) ⇒ Boolean

Determine if this role has write access for the provided permission

Parameters:

  • key (Symbol)

    the permission being queried

Returns:

  • (Boolean)

    if this role can write the passed permission



39
40
41
# File 'lib/osm/role.rb', line 39

def can_write?(key)
  return [20, 100].include?(@permissions[key])
end

#full_nameString

Get section’s full name in a consistent format

Returns:

  • (String)

    e.g. “1st Somewhere Beavers”



51
52
53
# File 'lib/osm/role.rb', line 51

def full_name
  @group_name.blank? ? @section.name : "#{@group_name} #{@section.name}"
end

#long_nameString

Get section’s long name in a consistent format

Returns:

  • (String)

    e.g. “Scouts (1st Somewhere)”



45
46
47
# File 'lib/osm/role.rb', line 45

def long_name
  @group_name.blank? ? @section.name : "#{@section.name} (#{@group_name})"
end