roles_mongo_mapper 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/README.markdown +11 -2
  2. data/Rakefile +7 -6
  3. data/VERSION +1 -1
  4. data/lib/roles_mongo_mapper/base.rb +21 -4
  5. data/lib/roles_mongo_mapper/embedded_role.rb +35 -0
  6. data/lib/roles_mongo_mapper/role.rb +7 -5
  7. data/lib/roles_mongo_mapper/strategy/multi.rb +59 -0
  8. data/lib/roles_mongo_mapper/strategy/multi/embed_many_roles.rb +71 -0
  9. data/lib/roles_mongo_mapper/strategy/multi/many_roles.rb +69 -23
  10. data/lib/roles_mongo_mapper/strategy/multi/role_strings.rb +44 -24
  11. data/lib/roles_mongo_mapper/strategy/multi/roles_mask.rb +67 -19
  12. data/lib/roles_mongo_mapper/strategy/shared.rb +29 -0
  13. data/lib/roles_mongo_mapper/strategy/single.rb +34 -0
  14. data/lib/roles_mongo_mapper/strategy/single/admin_flag.rb +50 -15
  15. data/lib/roles_mongo_mapper/strategy/single/embed_one_role.rb +79 -0
  16. data/lib/roles_mongo_mapper/strategy/single/one_role.rb +38 -30
  17. data/lib/roles_mongo_mapper/strategy/single/role_string.rb +20 -18
  18. data/roles_mongo_mapper.gemspec +51 -32
  19. data/sandbox/test.rb +13 -3
  20. data/spec/roles_mongo_mapper/strategy/api_examples.rb +199 -0
  21. data/spec/roles_mongo_mapper/strategy/hooks.rb +7 -0
  22. data/spec/roles_mongo_mapper/strategy/multi/embed_many_roles_spec.rb +19 -0
  23. data/spec/roles_mongo_mapper/strategy/multi/many_roles_spec.rb +19 -0
  24. data/spec/roles_mongo_mapper/strategy/multi/role_strings_spec.rb +19 -0
  25. data/spec/roles_mongo_mapper/strategy/multi/roles_mask_spec.rb +18 -0
  26. data/spec/roles_mongo_mapper/strategy/single/admin_flag_spec.rb +18 -0
  27. data/spec/roles_mongo_mapper/strategy/single/embed_one_role_spec.rb +19 -0
  28. data/spec/roles_mongo_mapper/strategy/single/one_role_spec.rb +20 -0
  29. data/spec/roles_mongo_mapper/strategy/single/role_string_spec.rb +19 -0
  30. data/spec/roles_mongo_mapper/strategy/user_setup.rb +13 -0
  31. data/spec/spec_helper.rb +11 -6
  32. metadata +60 -32
  33. data/spec/roles_mongo_mapper/strategy/admin_flag_spec.rb +0 -76
  34. data/spec/roles_mongo_mapper/strategy/many_roles_spec.rb +0 -69
  35. data/spec/roles_mongo_mapper/strategy/one_role_spec.rb +0 -66
  36. data/spec/roles_mongo_mapper/strategy/role_string_spec.rb +0 -68
  37. data/spec/roles_mongo_mapper/strategy/role_strings_spec.rb +0 -69
  38. data/spec/roles_mongo_mapper/strategy/roles_mask_spec.rb +0 -69
@@ -1,3 +1,5 @@
1
+ require 'roles_mongo_mapper/strategy/multi'
2
+
1
3
  module RoleStrategy::MongoMapper
2
4
  module RolesMask
3
5
  def self.default_role_attribute
@@ -20,7 +22,7 @@ module RoleStrategy::MongoMapper
20
22
  end
21
23
  end
22
24
 
23
- def in_roles(*roles)
25
+ def in_any_role(*roles)
24
26
  all.select do |user|
25
27
  roles.flatten.any? do |role|
26
28
  mask = calc_index(role.to_s)
@@ -34,7 +36,9 @@ module RoleStrategy::MongoMapper
34
36
  end
35
37
  end
36
38
 
37
- module Implementation
39
+ module Implementation
40
+ include Roles::MongoMapper::Strategy::Multi
41
+
38
42
  class Roles < ::Set # :nodoc:
39
43
  attr_reader :model_instance
40
44
 
@@ -49,30 +53,74 @@ module RoleStrategy::MongoMapper
49
53
  end
50
54
  end
51
55
 
52
- def role_attribute
53
- strategy_class.roles_attribute_name
54
- end
56
+ protected
57
+
58
+ def calc_index(r)
59
+ 2**strategy_class.valid_roles.index(r)
60
+ end
55
61
 
56
- # assign roles
57
- def roles=(*roles)
58
- 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 })
62
+ def get_roles
63
+ strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((get_role || 0) & calc_index(r)).zero? })
59
64
  end
60
- alias_method :role=, :roles=
61
65
 
62
- # query assigned roles
63
- def roles
64
- strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((self.send(role_attribute) || 0) & calc_index(r)).zero? })
66
+ def new_roles *role_names
67
+ role_names = role_names.flatten.map{ |r| r.to_sym } & strategy_class.valid_roles
68
+ role_names.map { |r| calc_index(r) }.inject { |sum, bitvalue| sum + bitvalue }
65
69
  end
66
70
 
67
- def roles_list
68
- roles.to_a
69
- end
70
-
71
- protected
71
+ def set_empty_roles
72
+ self.send("#{role_attribute}=", 0)
73
+ end
74
+
75
+ def present_roles *role_names
76
+ role_names.to_a.to_symbols
77
+ end
72
78
 
73
- def calc_index(r)
74
- 2**strategy_class.valid_roles.index(r)
79
+ def set_roles *roles
80
+ roles = roles.flat_uniq
81
+ roles = roles.first if self.class.role_strategy.multiplicity == :single
82
+ vrs = new_roles roles
83
+ self.send("#{role_attribute}=", vrs)
75
84
  end
85
+
86
+ # class Roles < ::Set # :nodoc:
87
+ # attr_reader :model_instance
88
+ #
89
+ # def initialize(sender, *roles)
90
+ # super(*roles)
91
+ # @model_instance = sender
92
+ # end
93
+ #
94
+ # def <<(role)
95
+ # model_instance.roles = super.to_a
96
+ # self
97
+ # end
98
+ # end
99
+ #
100
+ # def role_attribute
101
+ # strategy_class.roles_attribute_name
102
+ # end
103
+ #
104
+ # # assign roles
105
+ # def roles=(*roles)
106
+ # 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 })
107
+ # end
108
+ # alias_method :role=, :roles=
109
+ #
110
+ # # query assigned roles
111
+ # def roles
112
+ # strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((self.send(role_attribute) || 0) & calc_index(r)).zero? })
113
+ # end
114
+ #
115
+ # def roles_list
116
+ # roles.to_a
117
+ # end
118
+ #
119
+ # protected
120
+ #
121
+ # def calc_index(r)
122
+ # 2**strategy_class.valid_roles.index(r)
123
+ # end
76
124
  end
77
125
 
78
126
  extend Roles::Generic::User::Configuration
@@ -0,0 +1,29 @@
1
+ module Roles::MongoMapper
2
+ module Strategy
3
+ module Shared
4
+ def set_role role
5
+ raise ArgumentError, "#set_role only takes a single role as argument, not #{role}" if role.kind_of?(Array)
6
+ self.send("#{role_attribute}=", new_role(role))
7
+ end
8
+
9
+ def set_roles *roles
10
+ roles = roles.flat_uniq
11
+ roles = roles.first if self.class.role_strategy.multiplicity == :single
12
+ self.send("#{role_attribute}=", roles)
13
+ end
14
+
15
+ def get_role
16
+ r = self.send(role_attribute)
17
+ end
18
+
19
+ def get_roles
20
+ r = self.send(role_attribute)
21
+ end
22
+
23
+ def select_valid_roles *roles
24
+ roles = roles.flat_uniq.select{|role| valid_role? role }
25
+ has_role_class? ? role_class.find_roles(roles).to_a.flat_uniq : roles.flat_uniq
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,34 @@
1
+ require 'roles_mongo_mapper/strategy/shared'
2
+
3
+ class Symbol
4
+ def admin?
5
+ self == :admin
6
+ end
7
+ end
8
+
9
+ module Roles::MongoMapper
10
+ module Strategy
11
+ module Single
12
+ include Shared
13
+ # assigns first valid role from list of roles
14
+ def add_roles *role_names
15
+ new_roles = select_valid_roles(role_names)
16
+ the_role = new_roles.first if !new_roles.empty?
17
+ set_role new_role(the_role)
18
+ end
19
+
20
+ # should remove the current single role (set = nil)
21
+ # only if it is contained in the list of roles to be removed
22
+ def remove_roles *role_names
23
+ roles = role_names.flat_uniq
24
+ set_empty_role if roles_diff(roles).empty?
25
+ true
26
+ end
27
+
28
+ def roles_list
29
+ raise 'the method #roles should be present' if !respond_to? :roles
30
+ present_roles(roles)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,3 +1,5 @@
1
+ require 'roles_mongo_mapper/strategy/single'
2
+
1
3
  module RoleStrategy::MongoMapper
2
4
  module AdminFlag
3
5
 
@@ -15,7 +17,7 @@ module RoleStrategy::MongoMapper
15
17
  end
16
18
 
17
19
  def in_role(role_name)
18
- case role_name.downcase.to_sym
20
+ case role_name.downcase.to_sym
19
21
  when :admin
20
22
  where(role_attribute => true)
21
23
  else
@@ -24,23 +26,56 @@ module RoleStrategy::MongoMapper
24
26
  end
25
27
  end
26
28
 
27
- module Implementation
28
- # assign roles
29
- def roles=(*new_roles)
30
- first_role = new_roles.flatten.first
31
- if valid_role?(first_role)
32
- self.send("#{role_attribute}=", new_roles.flatten.first.admin?)
33
- else
34
- raise ArgumentError, "The role #{first_role} is not a valid role"
35
- end
29
+ module Implementation
30
+ include Roles::MongoMapper::Strategy::Single
31
+
32
+ # assign multiple roles
33
+ def roles=(*role_names)
34
+ role_names = role_names.flat_uniq
35
+ role_names = extract_roles(role_names)
36
+ return nil if role_names.empty?
37
+ vrs = select_valid_roles role_names
38
+ set_roles(vrs)
36
39
  end
37
40
 
38
- # query assigned roles
39
- def roles
40
- role = self.send(role_attribute) ? strategy_class.admin_role_key : strategy_class.default_role_key
41
- [role]
41
+ def set_roles *roles
42
+ set_role roles.flatten.first
43
+ end
44
+
45
+ def new_role role
46
+ role = role.kind_of?(Array) ? role.flatten.first : role
47
+ role.kind_of?(Symbol) ? role.admin? : role
48
+ end
49
+
50
+ def get_role
51
+ self.send(role_attribute) ? strategy_class.admin_role_key : strategy_class.default_role_key
52
+ end
53
+
54
+ def present_roles *roles
55
+ roles = roles.flat_uniq
56
+ roles.map{|role| role ? :admin : :guest}
57
+ end
58
+
59
+ def set_empty_role
60
+ self.send("#{role_attribute}=", false)
42
61
  end
43
- alias_method :roles_list, :roles
62
+ #
63
+ # # assign roles
64
+ # def roles=(*new_roles)
65
+ # first_role = new_roles.flatten.first
66
+ # if valid_role?(first_role)
67
+ # self.send("#{role_attribute}=", new_roles.flatten.first.admin?)
68
+ # else
69
+ # raise ArgumentError, "The role #{first_role} is not a valid role"
70
+ # end
71
+ # end
72
+ #
73
+ # # query assigned roles
74
+ # def roles
75
+ # role = self.send(role_attribute) ? strategy_class.admin_role_key : strategy_class.default_role_key
76
+ # [role]
77
+ # end
78
+ # alias_method :roles_list, :roles
44
79
 
45
80
  end # Implementation
46
81
 
@@ -0,0 +1,79 @@
1
+ require 'roles_mongo_mapper/strategy/single'
2
+
3
+ module RoleStrategy::MongoMapper
4
+ module EmbedOneRole
5
+ def self.default_role_attribute
6
+ :embed_one_role
7
+ end
8
+
9
+ def self.included base
10
+ base.extend Roles::Generic::Role::ClassMethods
11
+ base.extend ClassMethods
12
+ base.one :one_role, :class_name => 'Role'
13
+ end
14
+
15
+ module ClassMethods
16
+ def role_attribute
17
+ strategy_class.roles_attribute_name.to_sym
18
+ end
19
+
20
+ def role_id_attribute
21
+ "#{role_attribute}_id".to_sym
22
+ end
23
+
24
+ def in_role(role_name)
25
+ in_any_role(role_name)
26
+ end
27
+
28
+ def in_any_role(*role_names)
29
+ begin
30
+ role_ids = Role.find_roles(role_names).map(&:id)
31
+ all("#{role_attribute}._id" => role_ids)
32
+ rescue
33
+ return []
34
+ end
35
+ end
36
+ end
37
+
38
+ module Implementation
39
+ include Roles::MongoMapper::Strategy::Single
40
+
41
+ def add_roles *role_names
42
+ new_roles = select_valid_roles(role_names)
43
+ the_role = new_roles.first if !new_roles.empty?
44
+ set_role new_role(the_role)
45
+ end
46
+
47
+ def set_role role
48
+ raise ArgumentError, "#set_role only takes a single role as argument, not #{role}" if role.kind_of?(Array)
49
+ the_role = new_role(role)
50
+ self.send("#{role_attribute}=", the_role)
51
+ # self.one_role_id = the_role.id
52
+ end
53
+
54
+ def new_role role
55
+ role_class.find_role(extract_role role)
56
+ end
57
+
58
+ def new_roles *roles
59
+ new_role roles.flatten.first
60
+ end
61
+
62
+ def present_roles *roles
63
+ roles.map{|role| extract_role role}
64
+ end
65
+
66
+ def set_empty_role
67
+ self.send("#{role_attribute}=", nil)
68
+ end
69
+
70
+ def select_valid_roles *role_names
71
+ role_names = role_names.flat_uniq.select{|role| valid_role? role }
72
+ has_role_class? ? role_class.find_roles(role_names).to_a : role_names
73
+ end
74
+ end
75
+
76
+ extend Roles::Generic::User::Configuration
77
+ configure :num => :single, :type => :role_class
78
+ end
79
+ end
@@ -1,3 +1,5 @@
1
+ require 'roles_mongo_mapper/strategy/single'
2
+
1
3
  module RoleStrategy::MongoMapper
2
4
  module OneRole
3
5
  def self.default_role_attribute
@@ -21,19 +23,14 @@ module RoleStrategy::MongoMapper
21
23
  end
22
24
 
23
25
  def in_role(role_name)
24
- begin
25
- role = Role.find_role(role_name)
26
- all(role_id_attribute => role.id)
27
- rescue
28
- return []
29
- end
26
+ in_any_role(role_name)
30
27
  end
31
28
 
32
- def in_roles(*role_names)
29
+ def in_any_role(*role_names)
33
30
  begin
34
- role_ids = Role.find_roles(role_names)
35
- my_id = send(role_attribute).id
36
- all(role_id_attribute.in => role_ids)
31
+ role_ids = Role.find_roles(role_names).map(&:id)
32
+ all("#{role_attribute}._id" => role_ids)
33
+ # all(role_id_attribute.in => role_ids)
37
34
  rescue
38
35
  return []
39
36
  end
@@ -41,30 +38,41 @@ module RoleStrategy::MongoMapper
41
38
  end
42
39
 
43
40
  module Implementation
44
- def role_id_attribute
45
- "#{role_attribute}_id".to_sym
46
- end
47
-
48
- # assign roles
49
- def roles=(*_roles)
50
- _roles = get_roles(_roles)
51
- return nil if _roles.none?
41
+ include Roles::MongoMapper::Strategy::Single
52
42
 
53
- role_relation = role_class.find_role(_roles.first)
54
- if role_relation
55
- self.send("#{role_attribute}=", role_relation)
56
- self.send("#{role_id_attribute}=", role_relation.id)
57
- self.save
58
- end
43
+ def add_roles *role_names
44
+ new_roles = select_valid_roles(role_names)
45
+ the_role = new_roles.first if !new_roles.empty?
46
+ set_role new_role(the_role)
59
47
  end
60
- alias_method :role=, :roles=
61
-
62
- # query assigned roles
63
- def roles
64
- [self.send(role_attribute).name.to_sym]
48
+
49
+ def set_role role
50
+ raise ArgumentError, "#set_role only takes a single role as argument, not #{role}" if role.kind_of?(Array)
51
+ the_role = new_role(role)
52
+ self.send("#{role_attribute}=", the_role)
53
+ # self.one_role_id = the_role.id
65
54
  end
66
- alias_method :roles_list, :roles
67
55
 
56
+ def new_role role
57
+ role_class.find_role(extract_role role)
58
+ end
59
+
60
+ def new_roles *roles
61
+ new_role roles.flatten.first
62
+ end
63
+
64
+ def present_roles *roles
65
+ roles.map{|role| extract_role role}
66
+ end
67
+
68
+ def set_empty_role
69
+ self.send("#{role_attribute}=", nil)
70
+ end
71
+
72
+ def select_valid_roles *role_names
73
+ role_names = role_names.flat_uniq.select{|role| valid_role? role }
74
+ has_role_class? ? role_class.find_roles(role_names).to_a : role_names
75
+ end
68
76
  end
69
77
 
70
78
  extend Roles::Generic::User::Configuration
@@ -1,3 +1,5 @@
1
+ require 'roles_mongo_mapper/strategy/single'
2
+
1
3
  module RoleStrategy::MongoMapper
2
4
  module RoleString
3
5
  def self.default_role_attribute
@@ -19,28 +21,28 @@ module RoleStrategy::MongoMapper
19
21
  end
20
22
 
21
23
 
22
- module Implementation
23
- def role_attribute
24
- strategy_class.roles_attribute_name
25
- end
26
-
27
- # assign roles
28
- def roles=(*roles)
29
- self.role = roles.select_labels.first.to_s
30
- end
24
+ module Implementation
25
+ include Roles::MongoMapper::Strategy::Single
26
+
27
+ def new_role role
28
+ role = role.kind_of?(Array) ? role.first : role
29
+ role.to_s
30
+ end
31
+
32
+ def new_roles *roles
33
+ new_role roles.flatten.first
34
+ end
31
35
 
32
- def role= role_name
33
- if role_name.kind_of_label? && valid_role?(role_name)
34
- self.send("#{role_attribute}=", role_name.to_s)
35
- end
36
+ def present_role role
37
+ return role if role.kind_of? Array
38
+ role.split(',').map(&:to_sym)
36
39
  end
37
40
 
38
- # query assigned roles
39
- def roles
40
- role = self.send(role_attribute)
41
- [role.to_sym]
41
+ def set_empty_role
42
+ self.send("#{role_attribute}=", "")
42
43
  end
43
- alias_method :roles_list, :roles
44
+
45
+ alias_method :present_roles, :present_role
44
46
  end
45
47
 
46
48
  extend Roles::Generic::User::Configuration