roles_generic 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/roles_generic/generic/user/implementation.rb +3 -0
- data/lib/roles_generic/strategy/multi/many_roles.rb +1 -8
- data/lib/roles_generic/strategy/multi/role_strings.rb +3 -5
- data/lib/roles_generic/strategy/multi/roles_mask.rb +3 -4
- data/lib/roles_generic/strategy/multi/roles_string.rb +5 -4
- data/lib/roles_generic/strategy/single/admin_flag.rb +1 -5
- data/lib/roles_generic/strategy/single/one_role.rb +3 -7
- data/lib/roles_generic/strategy/single/role_string.rb +4 -6
- data/lib/roles_generic/strategy.rb +24 -12
- data/lib/roles_generic.rb +1 -1
- data/roles_generic.gemspec +1 -1
- metadata +2 -2
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.2.
|
|
1
|
+
0.2.2
|
|
@@ -1,17 +1,10 @@
|
|
|
1
|
-
require 'set'
|
|
2
|
-
require 'roles_generic/role'
|
|
3
|
-
|
|
4
1
|
module RoleStrategy::Generic
|
|
5
2
|
module ManyRoles
|
|
6
3
|
def self.default_role_attribute
|
|
7
4
|
:many_roles
|
|
8
5
|
end
|
|
9
6
|
|
|
10
|
-
module Implementation
|
|
11
|
-
def role_attribute
|
|
12
|
-
strategy_class.roles_attribute_name
|
|
13
|
-
end
|
|
14
|
-
|
|
7
|
+
module Implementation
|
|
15
8
|
# assign roles
|
|
16
9
|
def roles=(*roles)
|
|
17
10
|
raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
|
|
@@ -1,21 +1,19 @@
|
|
|
1
|
-
require 'set'
|
|
2
|
-
|
|
3
1
|
module RoleStrategy::Generic
|
|
4
2
|
module RoleStrings
|
|
5
3
|
def self.default_role_attribute
|
|
6
4
|
:role_strings
|
|
7
5
|
end
|
|
8
6
|
|
|
9
|
-
module Implementation
|
|
7
|
+
module Implementation
|
|
10
8
|
# assign roles
|
|
11
9
|
def roles=(*roles)
|
|
12
10
|
new_roles = roles.flatten.map{|r| r.to_s if valid_role?(r)}.compact
|
|
13
|
-
self.send("#{
|
|
11
|
+
self.send("#{role_attribute}=", ::Set.new(new_roles)) if new_roles && new_roles.not.empty?
|
|
14
12
|
end
|
|
15
13
|
|
|
16
14
|
# query assigned roles
|
|
17
15
|
def roles
|
|
18
|
-
self.send(
|
|
16
|
+
self.send(role_attribute).map{|r| r.to_sym}
|
|
19
17
|
end
|
|
20
18
|
|
|
21
19
|
def roles_list
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
require 'set'
|
|
2
|
-
|
|
3
1
|
module RoleStrategy::Generic
|
|
4
2
|
module RolesMask
|
|
5
3
|
def self.default_role_attribute
|
|
@@ -23,12 +21,13 @@ module RoleStrategy::Generic
|
|
|
23
21
|
|
|
24
22
|
# assign roles
|
|
25
23
|
def roles=(*roles)
|
|
26
|
-
self.send("#{
|
|
24
|
+
self.send("#{role_attribute}=", (roles.flatten.map { |r| r.to_sym } & strategy_class.valid_roles).map { |r| calc_index(r) }.inject { |sum, bitvalue| sum + bitvalue })
|
|
27
25
|
end
|
|
26
|
+
alias_method :role=, :roles=
|
|
28
27
|
|
|
29
28
|
# query assigned roles
|
|
30
29
|
def roles
|
|
31
|
-
strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((self.send(
|
|
30
|
+
strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((self.send(role_attribute) || 0) & calc_index(r)).zero? })
|
|
32
31
|
end
|
|
33
32
|
|
|
34
33
|
def roles_list
|
|
@@ -4,16 +4,17 @@ module RoleStrategy::Generic
|
|
|
4
4
|
:roles_string
|
|
5
5
|
end
|
|
6
6
|
|
|
7
|
-
module Implementation
|
|
7
|
+
module Implementation
|
|
8
8
|
# assign roles
|
|
9
9
|
def roles=(*roles)
|
|
10
10
|
roles_str = roles.flatten.map{|r| r.to_s if valid_role?(r)}.compact.uniq.join(',')
|
|
11
|
-
self.send("#{
|
|
12
|
-
end
|
|
11
|
+
self.send("#{role_attribute}=", roles_str) if roles_str && roles_str.not.empty?
|
|
12
|
+
end
|
|
13
|
+
alias_method :role=, :roles=
|
|
13
14
|
|
|
14
15
|
# query assigned roles
|
|
15
16
|
def roles
|
|
16
|
-
self.send(
|
|
17
|
+
self.send(role_attribute).split(',').uniq.map{|r| r.to_sym}
|
|
17
18
|
end
|
|
18
19
|
alias_method :roles_list, :roles
|
|
19
20
|
end
|
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
require 'set'
|
|
2
|
-
require 'roles_generic/role'
|
|
3
|
-
|
|
4
1
|
module RoleStrategy::Generic
|
|
5
2
|
module OneRole
|
|
6
3
|
def self.default_role_attribute
|
|
7
4
|
:one_role
|
|
8
5
|
end
|
|
9
6
|
|
|
10
|
-
module Implementation
|
|
7
|
+
module Implementation
|
|
11
8
|
# assign roles
|
|
12
9
|
def roles=(*roles)
|
|
13
10
|
raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
|
|
@@ -15,14 +12,13 @@ module RoleStrategy::Generic
|
|
|
15
12
|
first_role = roles.flatten.first
|
|
16
13
|
role_relation = role_class.find_role(first_role)
|
|
17
14
|
if role_relation && role_relation.kind_of?(role_class)
|
|
18
|
-
self.send("#{
|
|
15
|
+
self.send("#{role_attribute}=", role_relation)
|
|
19
16
|
end
|
|
20
17
|
end
|
|
21
18
|
|
|
22
19
|
# query assigned roles
|
|
23
20
|
def roles
|
|
24
|
-
|
|
25
|
-
[role]
|
|
21
|
+
[self.send(role_attribute).name.to_sym]
|
|
26
22
|
end
|
|
27
23
|
|
|
28
24
|
def roles_list
|
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
require 'set'
|
|
2
|
-
|
|
3
1
|
module RoleStrategy::Generic
|
|
4
2
|
module RoleString
|
|
5
3
|
def self.default_role_attribute
|
|
6
4
|
:role_string
|
|
7
5
|
end
|
|
8
6
|
|
|
9
|
-
module Implementation
|
|
7
|
+
module Implementation
|
|
10
8
|
# assign roles
|
|
11
9
|
def roles=(*roles)
|
|
12
10
|
first_role = roles.flatten.first.to_s
|
|
13
11
|
if valid_role? first_role
|
|
14
|
-
self.send("#{
|
|
12
|
+
self.send("#{role_attribute}=", first_role)
|
|
15
13
|
end
|
|
16
14
|
end
|
|
15
|
+
alias_method :role=, :roles=
|
|
17
16
|
|
|
18
17
|
# query assigned roles
|
|
19
18
|
def roles
|
|
20
|
-
|
|
21
|
-
[role.to_sym]
|
|
19
|
+
[self.send(role_attribute).to_sym]
|
|
22
20
|
end
|
|
23
21
|
alias_method :roles_list, :roles
|
|
24
22
|
end
|
|
@@ -2,25 +2,37 @@ require 'sugar-high/file'
|
|
|
2
2
|
require 'sugar-high/array'
|
|
3
3
|
|
|
4
4
|
module Roles::Strategy
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
class << self
|
|
6
|
+
NON_INLINE_STRATEGIES = [:one_role, :many_roles]
|
|
7
|
+
|
|
8
|
+
def role_strategies cardinality
|
|
9
|
+
pattern = File.dirname(__FILE__) + "/strategy/#{cardinality}/*.rb"
|
|
10
|
+
Dir.glob(pattern).file_names(:rb).to_symbols
|
|
11
|
+
end
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
def has_strategy? cardinality, strategy
|
|
14
|
+
role_strategies(cardinality).include?(strategy)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def inline_strategy? strategy
|
|
18
|
+
!NON_INLINE_STRATEGIES.include? strategy.to_sym
|
|
19
|
+
end
|
|
13
20
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
21
|
+
def cardinality strategy
|
|
22
|
+
[:single, :multi].each do |cardinality|
|
|
23
|
+
return cardinality if has_strategy?(cardinality, strategy)
|
|
24
|
+
end
|
|
25
|
+
raise ArgumentError, "Strategy #{strategy} is not registered as either a single or multi cardinality role strategy"
|
|
17
26
|
end
|
|
18
|
-
raise ArgumentError, "Strategy #{strategy} is not registered as either a single or multi cardinality role strategy"
|
|
19
27
|
end
|
|
20
28
|
end
|
|
21
29
|
|
|
22
30
|
def use_roles_strategy strategy
|
|
23
|
-
cardinality = Roles::Strategy.cardinality(strategy)
|
|
31
|
+
cardinality = Roles::Strategy.cardinality(strategy)
|
|
32
|
+
require "roles_generic/strategy/#{cardinality}/#{strategy}"
|
|
33
|
+
|
|
34
|
+
require 'roles_generic/role' if !Roles::Strategy.inline_strategy? strategy
|
|
24
35
|
require "roles_generic/strategy/#{cardinality}/#{strategy}"
|
|
25
36
|
end
|
|
26
37
|
|
|
38
|
+
|
data/lib/roles_generic.rb
CHANGED
data/roles_generic.gemspec
CHANGED