Module: IAmICan::Role::Definition

Includes:
Helpers::Cls
Defined in:
lib/i_am_i_can/role/definition.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Cls

#_role_definition_result, #_to_store_role, #defined_local_roles, #defined_role_group_names, #defined_role_groups, #defined_roles, #defined_stored_role_names, #defined_stored_roles

Class Method Details

.extended(kls) ⇒ Object



53
54
55
# File 'lib/i_am_i_can/role/definition.rb', line 53

def self.extended(kls)
  kls.delegate :defined_local_roles, :defined_stored_roles, :defined_roles, to: kls
end

Instance Method Details

#declare_role(*names, **options) ⇒ Object



30
31
32
# File 'lib/i_am_i_can/role/definition.rb', line 30

def declare_role *names, **options
  have_role *names, save: false, **options
end

#group_roles(*members, by_name:, which_can: [ ], obj: nil) ⇒ Object Also known as: group_role, groups_role, groups_roles

Raises:



36
37
38
39
40
# File 'lib/i_am_i_can/role/definition.rb', line 36

def group_roles *members, by_name:, which_can: [ ], obj: nil
  raise Error, 'Some of members have not been defined' unless (members - defined_stored_role_names).empty?
  raise Error, "Given name #{by_name} has been used by a role" if i_am_i_can.role_model.exists?(name: by_name)
  i_am_i_can.role_group_model.find_or_create_by!(name: by_name).members_add(members)
end

#have_and_group_roles(*members, by_name:) ⇒ Object Also known as: has_and_groups_roles



46
47
48
49
# File 'lib/i_am_i_can/role/definition.rb', line 46

def have_and_group_roles *members, by_name:
  has_roles *members
  group_roles *members, by_name: by_name
end

#have_role(*names, desc: nil, save: i_am_i_can.default_save, which_can: [ ], obj: nil) ⇒ Object Also known as: have_roles, has_role, has_roles



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/i_am_i_can/role/definition.rb', line 8

def have_role *names, desc: nil, save: i_am_i_can.default_save, which_can: [ ], obj: nil
  failed_items, preds = [ ], which_can

  names.each do |name|
    description = desc || name.to_s.humanize
    if save
      next failed_items << name unless _to_store_role(name, desc: description)
      i_am_i_can.role_model.which(name: name).can *preds, obj: obj, auto_define_before: true, strict_mode: true if which_can.present?
    else
      next failed_items << name if defined_local_roles.key?(name)
      defined_local_roles[name] ||= { desc: description, permissions: [ ] }
      local_role_which(name: name, can: preds, obj: obj, auto_define_before: true, strict_mode: true) if which_can.present?
    end
  end

  _role_definition_result(names, failed_items)
end