Module: Bsm::Sso::Client::Ability::ClassMethods

Defined in:
lib/bsm/sso/client/ability.rb

Instance Method Summary collapse

Instance Method Details

#as(scope, name, &block) ⇒ Object

Ability definition helper

Parameters:

  • scope (Symbol)

    the user scope

  • name (String)

    the role name



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bsm/sso/client/ability.rb', line 30

def as(scope, name, &block)
  method_name = :"as__#{scope}__#{name}"

  define_method(method_name) do
    return false if self.scope != scope || applied.include?(name.to_s)

    applied.add(name.to_s)
    instance_eval(&block)
    true
  end
  private method_name
end

#rolesHash

Returns roles, scoped by user type.

Returns:

  • (Hash)

    roles, scoped by user type



17
18
19
20
21
22
23
24
25
# File 'lib/bsm/sso/client/ability.rb', line 17

def roles
  private_instance_methods(false).each_with_object({}) do |name, result|
    prefix, scope, name = name.to_s.split('__')
    next result unless prefix == 'as' && scope && name

    result[scope.to_sym] ||= []
    result[scope.to_sym] << name
  end
end