easy_roles 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Rakefile +1 -1
  2. data/easy_roles.gemspec +3 -3
  3. data/lib/easy_roles.rb +86 -30
  4. metadata +3 -3
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('easy_roles', '0.3.0') do |p|
5
+ Echoe.new('easy_roles', '0.4.0') do |p|
6
6
  p.description = "Easy role authorization in rails"
7
7
  p.url = "http://github.com/platform45/easy_roles"
8
8
  p.author = "Platform45"
data/easy_roles.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{easy_roles}
5
- s.version = "0.3.0"
5
+ s.version = "0.4.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Platform45"]
9
- s.date = %q{2009-11-18}
9
+ s.date = %q{2009-12-08}
10
10
  s.description = %q{Easy role authorization in rails}
11
11
  s.email = %q{ryan@platform45.com}
12
12
  s.extra_rdoc_files = ["README.rdoc", "lib/easy_roles.rb"]
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Easy_roles", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{easy_roles}
18
- s.rubygems_version = %q{1.3.4}
18
+ s.rubygems_version = %q{1.3.5}
19
19
  s.summary = %q{Easy role authorization in rails}
20
20
 
21
21
  if s.respond_to? :specification_version then
data/lib/easy_roles.rb CHANGED
@@ -5,43 +5,99 @@ module EasyRoles
5
5
  end
6
6
 
7
7
  module ClassMethods
8
- def easy_roles(name)
9
- serialize name.to_sym, Array
10
- before_validation_on_create :make_default_roles
8
+ def easy_roles(name, options = {})
11
9
 
12
- class_eval <<-EOC
13
- def has_role?(role)
14
- #{name}.include?(role)
15
- end
10
+ options[:method] ||= :serialize
11
+
12
+ if options[:method] == :serialize
13
+ serialize name.to_sym, Array
14
+ before_validation_on_create :make_default_roles
15
+
16
+ class_eval <<-EOC
17
+ def has_role?(role)
18
+ #{name}.include?(role)
19
+ end
16
20
 
17
- def add_role(role)
18
- self.#{name} << role
19
- end
20
-
21
- def add_role!(role)
22
- add_role(role)
23
- self.save!
24
- end
21
+ def add_role(role)
22
+ self.#{name} << role
23
+ end
24
+
25
+ def add_role!(role)
26
+ add_role(role)
27
+ self.save!
28
+ end
25
29
 
26
- def remove_role(role)
27
- self.#{name}.delete(role)
28
- end
30
+ def remove_role(role)
31
+ self.#{name}.delete(role)
32
+ end
33
+
34
+ def remove_role!(role)
35
+ remove_role(role)
36
+ self.save!
37
+ end
29
38
 
30
- def remove_role!(role)
31
- remove_role(role)
32
- self.save!
33
- end
39
+ def clear_roles
40
+ self.#{name} = []
41
+ end
34
42
 
35
- def clear_roles
36
- self.#{name} = []
37
- end
43
+ def make_default_roles
44
+ clear_roles if #{name}.nil?
45
+ end
38
46
 
39
- def make_default_roles
40
- clear_roles if #{name}.nil?
41
- end
47
+ private :make_default_roles
48
+ EOC
49
+ elsif options[:method] == :bitmask
42
50
 
43
- private :make_default_roles
44
- EOC
51
+ def_name = (name == :roles) ? :easy_roles : :roles
52
+
53
+ class_eval <<-EOC
54
+ def self.list_roles
55
+ #{name.to_s.upcase}
56
+ end
57
+
58
+ def #{def_name}=(roles)
59
+ self.#{name} = (roles & #{name.to_s.upcase}).map { |r| 2**#{name.to_s.upcase}.index(r) }.sum
60
+ end
61
+
62
+ def #{def_name}
63
+ #{name.to_s.upcase}.reject { |r| ((#{name} || 0) & 2**#{name.to_s.upcase}.index(r)).zero? }
64
+ end
65
+
66
+ def has_role?(role)
67
+ #{def_name}.include?(role)
68
+ end
69
+
70
+ def add_role(role)
71
+ new_roles = #{def_name}.push(role)
72
+ self.#{def_name} = new_roles
73
+ end
74
+
75
+ def add_role!(role)
76
+ add_role(role)
77
+ self.save!
78
+ end
79
+
80
+ def remove_role(role)
81
+ new_roles = #{def_name}
82
+ new_roles.delete(role)
83
+ self.#{def_name} = new_roles
84
+ end
85
+
86
+ def remove_role!(role)
87
+ remove_role(role)
88
+ self.save!
89
+ end
90
+
91
+ def clear_roles
92
+ self.#{name} = 0
93
+ end
94
+
95
+ def clear_roles!
96
+ self.#{name} = 0
97
+ self.save!
98
+ end
99
+ EOC
100
+ end
45
101
  end
46
102
  end
47
103
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_roles
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Platform45
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-18 00:00:00 +02:00
12
+ date: 2009-12-08 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -61,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  requirements: []
62
62
 
63
63
  rubyforge_project: easy_roles
64
- rubygems_version: 1.3.4
64
+ rubygems_version: 1.3.5
65
65
  signing_key:
66
66
  specification_version: 3
67
67
  summary: Easy role authorization in rails