roles_generic 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/.rspec +1 -0
  4. data/LICENSE +20 -0
  5. data/README.markdown +308 -0
  6. data/Rakefile +45 -0
  7. data/VERSION +1 -0
  8. data/lib/generators/roles_model/roles/roles_generator.rb +65 -0
  9. data/lib/roles_generic/admin_flag.rb +40 -0
  10. data/lib/roles_generic/base.rb +45 -0
  11. data/lib/roles_generic/extensions/core_ext.rb +19 -0
  12. data/lib/roles_generic/generic/class_methods.rb +33 -0
  13. data/lib/roles_generic/generic/implementation.rb +26 -0
  14. data/lib/roles_generic/generic/util.rb +39 -0
  15. data/lib/roles_generic/generic.rb +77 -0
  16. data/lib/roles_generic/many_roles.rb +39 -0
  17. data/lib/roles_generic/one_role.rb +40 -0
  18. data/lib/roles_generic/role/class_methods.rb +20 -0
  19. data/lib/roles_generic/role_string.rb +29 -0
  20. data/lib/roles_generic/role_strings.rb +23 -0
  21. data/lib/roles_generic/roles_mask.rb +48 -0
  22. data/lib/roles_generic/roles_string.rb +26 -0
  23. data/lib/roles_generic.rb +2 -0
  24. data/spec/generator_spec_helper.rb +12 -0
  25. data/spec/generators/admin_flag_generator_spec.rb +77 -0
  26. data/spec/generators/many_roles_generator_spec.rb +39 -0
  27. data/spec/generators/one_role_generator_spec.rb +39 -0
  28. data/spec/generators/role_string_generator_spec.rb +39 -0
  29. data/spec/generators/role_strings_generator_spec.rb +39 -0
  30. data/spec/generators/roles_mask_generator_spec.rb +39 -0
  31. data/spec/generators/roles_string_generator_spec.rb +39 -0
  32. data/spec/model/role.rb +26 -0
  33. data/spec/roles_generic/admin_flag_spec.rb +66 -0
  34. data/spec/roles_generic/many_roles_spec.rb +63 -0
  35. data/spec/roles_generic/one_role_spec.rb +70 -0
  36. data/spec/roles_generic/role_string_spec.rb +65 -0
  37. data/spec/roles_generic/role_strings_spec.rb +58 -0
  38. data/spec/roles_generic/roles_mask_spec.rb +57 -0
  39. data/spec/roles_generic/roles_string_spec.rb +58 -0
  40. data/spec/spec_helper.rb +9 -0
  41. metadata +182 -0
@@ -0,0 +1,77 @@
1
+ require 'require_all'
2
+ require 'active_support/inflector'
3
+ require_all File.dirname(__FILE__) + '/extensions'
4
+
5
+ module RoleModels
6
+ module Base
7
+ attr_accessor :orm_name
8
+
9
+ def valid_roles
10
+ strategy_class.valid_roles
11
+ end
12
+
13
+ def roles(*roles)
14
+ strategy_class.valid_roles = Array[*roles].flatten.map { |r| r.to_sym }
15
+ end
16
+
17
+ def role_strategy strategy, options=nil
18
+ include_strategy orm_name, strategy, options
19
+ end
20
+
21
+ def include_strategy orm, strategy, options=nil
22
+ begin
23
+ constant = "RoleModels::#{orm_name.to_s.camelize}::#{strategy.to_s.camelize}".constantize
24
+
25
+ strategy_class_method = %Q{
26
+ def strategy_class
27
+ #{constant}
28
+ end
29
+ }
30
+
31
+ class_eval do
32
+ eval strategy_class_method
33
+ end
34
+
35
+ instance_eval do
36
+ eval strategy_class_method
37
+ include constant
38
+ end
39
+ rescue
40
+ raise "No Role strategy module for ORM #{orm} found for strategy #{strategy}"
41
+ end
42
+ constant.apply_options(options) if constant.respond_to? :apply_options
43
+ end
44
+ end
45
+ end
46
+
47
+ module RoleModels
48
+ module Generic
49
+ def self.included(base)
50
+ base.extend RoleModels::Base
51
+ base.orm_name = :generic
52
+ end
53
+ end
54
+ end
55
+
56
+ require_all File.dirname(__FILE__) +'/generic'
57
+
58
+ module RoleModels::Generic
59
+ module Base
60
+ INHERITABLE_CLASS_ATTRIBUTES = [:roles_attribute_name, :valid_roles]
61
+
62
+ include Implementation
63
+
64
+ def self.included(base) # :nodoc:
65
+ base.extend ClassMethods
66
+ base.class_eval do
67
+ class << self
68
+ attr_accessor(*::RoleModels::Generic::Base::INHERITABLE_CLASS_ATTRIBUTES)
69
+
70
+ def apply_options options
71
+ roles_attribute default_role_attribute if options == :default
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,39 @@
1
+ require 'roles_generic/role/class_methods'
2
+ require 'set'
3
+
4
+ module RoleModels::Generic
5
+ module ManyRoles
6
+ def self.default_role_attribute
7
+ :many_roles
8
+ end
9
+
10
+ module Implementation
11
+ # assign roles
12
+ def roles=(*roles)
13
+ raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
14
+
15
+ role_relations = role_class.find_roles(*roles)
16
+
17
+ role_relations.each do |role_relation|
18
+ self.send("#{strategy_class.roles_attribute_name}=", role_relation) if role_relation.kind_of?(role_class)
19
+ end
20
+ end
21
+
22
+ # query assigned roles
23
+ def roles
24
+ self.send(strategy_class.roles_attribute_name)
25
+ end
26
+
27
+ def roles_list
28
+ [roles].flatten.map{|r| r.name }.compact
29
+ end
30
+ end
31
+
32
+ extend RoleModels::Generic::Base::Configuration
33
+ configure :type => :role_class
34
+
35
+ def self.included(base)
36
+ base.extend RoleModels::Generic::Base::RoleClass::ClassMethods
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,40 @@
1
+ require 'roles_generic/role/class_methods'
2
+ require 'set'
3
+
4
+ module RoleModels::Generic
5
+ module OneRole
6
+ def self.default_role_attribute
7
+ :one_role
8
+ end
9
+
10
+ module Implementation
11
+ # assign roles
12
+ def roles=(*roles)
13
+ raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
14
+
15
+ first_role = roles.flatten.first
16
+ role_relation = role_class.find_role(first_role)
17
+ if role_relation && role_relation.kind_of?(role_class)
18
+ self.send("#{strategy_class.roles_attribute_name}=", role_relation)
19
+ end
20
+ end
21
+
22
+ # query assigned roles
23
+ def roles
24
+ role = self.send(strategy_class.roles_attribute_name).name.to_sym
25
+ [role]
26
+ end
27
+
28
+ def roles_list
29
+ self.roles.to_a
30
+ end
31
+ end
32
+
33
+ extend RoleModels::Generic::Base::Configuration
34
+ configure :num => :single, :type => :role_class
35
+
36
+ def self.included(base)
37
+ base.extend RoleModels::Generic::Base::RoleClass::ClassMethods
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,20 @@
1
+ module RoleModels::Generic::Base
2
+ module RoleClass
3
+ module InstanceMethods
4
+ def role_class
5
+ self.class.role_class_name
6
+ end
7
+ end
8
+
9
+ module ClassMethods
10
+ def role_class_name
11
+ @role_class_name
12
+ end
13
+
14
+ def role_class class_constant
15
+ const = class_constant.to_s.camelize
16
+ @role_class_name = "#{const}".constantize
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ require 'set'
2
+
3
+ module RoleModels::Generic
4
+ module RoleString
5
+ def self.default_role_attribute
6
+ :role_string
7
+ end
8
+
9
+ module Implementation
10
+ # assign roles
11
+ def roles=(*roles)
12
+ first_role = roles.flatten.first.to_s
13
+ if valid_role? first_role
14
+ self.send("#{strategy_class.roles_attribute_name}=", first_role)
15
+ end
16
+ end
17
+
18
+ # query assigned roles
19
+ def roles
20
+ role = self.send(strategy_class.roles_attribute_name)
21
+ [role.to_sym]
22
+ end
23
+ alias_method :roles_list, :roles
24
+ end
25
+
26
+ extend RoleModels::Generic::Base::Configuration
27
+ configure :num => :single
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ module RoleModels::Generic
2
+ module RoleStrings
3
+ def self.default_role_attribute
4
+ :role_strings
5
+ end
6
+
7
+ module Implementation
8
+ # assign roles
9
+ def roles=(*roles)
10
+ new_roles = roles.flatten.map{|r| r.to_s if valid_role?(r)}.compact
11
+ self.send("#{strategy_class.roles_attribute_name}=", Set.new(new_roles)) if new_roles && new_roles.not.empty?
12
+ end
13
+
14
+ # query assigned roles
15
+ def roles
16
+ self.send(strategy_class.roles_attribute_name).map{|r| r.to_sym}
17
+ end
18
+ end
19
+
20
+ extend RoleModels::Generic::Base::Configuration
21
+ configure
22
+ end
23
+ end
@@ -0,0 +1,48 @@
1
+ require 'set'
2
+
3
+ module RoleModels::Generic
4
+ module RolesMask
5
+ def self.default_role_attribute
6
+ :roles_mask
7
+ end
8
+
9
+ module Implementation
10
+ class Roles < ::Set # :nodoc:
11
+ attr_reader :model_instance
12
+
13
+ def initialize(sender, *roles)
14
+ super(*roles)
15
+ @model_instance = sender
16
+ end
17
+
18
+ def <<(role)
19
+ model_instance.roles = super.to_a
20
+ self
21
+ end
22
+ end
23
+
24
+ # assign roles
25
+ def roles=(*roles)
26
+ self.send("#{strategy_class.roles_attribute_name}=", (roles.flatten.map { |r| r.to_sym } & strategy_class.valid_roles).map { |r| calc_index(r) }.inject { |sum, bitvalue| sum + bitvalue })
27
+ end
28
+
29
+ # query assigned roles
30
+ def roles
31
+ strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((self.send(strategy_class.roles_attribute_name) || 0) & calc_index(r)).zero? })
32
+ end
33
+
34
+ def roles_list
35
+ roles.to_a
36
+ end
37
+
38
+ protected
39
+
40
+ def calc_index(r)
41
+ 2**strategy_class.valid_roles.index(r)
42
+ end
43
+ end
44
+
45
+ extend RoleModels::Generic::Base::Configuration
46
+ configure
47
+ end
48
+ end
@@ -0,0 +1,26 @@
1
+ require_all File.dirname(__FILE__) +'/roles_string'
2
+
3
+ module RoleModels::Generic
4
+ module RolesString
5
+ def self.default_role_attribute
6
+ :roles_string
7
+ end
8
+
9
+ module Implementation
10
+ # assign roles
11
+ def roles=(*roles)
12
+ roles_str = roles.flatten.map{|r| r.to_s if valid_role?(r)}.compact.uniq.join(',')
13
+ self.send("#{strategy_class.roles_attribute_name}=", roles_str) if roles_str && roles_str.not.empty?
14
+ end
15
+
16
+ # query assigned roles
17
+ def roles
18
+ self.send(strategy_class.roles_attribute_name).split(',').uniq.map{|r| r.to_sym}
19
+ end
20
+ alias_method :roles_list, :roles
21
+ end
22
+
23
+ extend RoleModels::Generic::Base::Configuration
24
+ configure
25
+ end
26
+ end
@@ -0,0 +1,2 @@
1
+ require 'roles_generic/base'
2
+ require 'roles_generic/generic'
@@ -0,0 +1,12 @@
1
+ require 'rspec'
2
+ require 'rspec/autorun'
3
+ require 'generator_spec'
4
+ require 'roles_generic'
5
+
6
+ RSpec::Generator.configure do |config|
7
+ config.debug = false
8
+ config.remove_temp_dir = true
9
+ config.default_rails_root(__FILE__)
10
+ config.lib = File.dirname(__FILE__) + '/../lib'
11
+ config.logger = :stdout
12
+ end
@@ -0,0 +1,77 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../generator_spec_helper')
2
+ require_generator :roles_model => :roles
3
+
4
+ describe 'role strategy generator: admin_flag' do
5
+ let(:strategy) { :admin_flag }
6
+
7
+ describe 'ORM: none' do
8
+ let(:orm) { :none }
9
+
10
+ use_orm :none
11
+ helpers :model
12
+
13
+ before :each do
14
+ setup_generator 'roles_generator' do
15
+ tests RolesModel::Generators::RolesGenerator
16
+ end
17
+ remove_model 'user'
18
+ end
19
+
20
+ after :each do
21
+ remove_model 'user'
22
+ end
23
+
24
+ it "should configure 'admin_flag' strategy" do
25
+ create_model :user do
26
+ '# content'
27
+ end
28
+ with_generator do |g|
29
+ arguments = "User --strategy #{strategy} --roles admin user --orm #{orm}"
30
+ puts "arguments: #{arguments}"
31
+ g.run_generator arguments.args
32
+ g.should generate_model :user do |clazz|
33
+ clazz.should include_module 'RoleModels::Generic'
34
+ puts "clazz: #{clazz}"
35
+ clazz.should have_call :roles, :args => ':admin, :user'
36
+ clazz.should have_call :role_strategy, :args => ":#{strategy}"
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ describe 'ORM: active_record' do
43
+ let(:orm) { :active_record }
44
+
45
+ use_orm :active_record
46
+ helpers :model
47
+
48
+ before :each do
49
+ setup_generator 'roles_generator' do
50
+ tests RolesModel::Generators::RolesGenerator
51
+ end
52
+ remove_model 'user'
53
+ end
54
+
55
+ after :each do
56
+ # remove_model 'user'
57
+ end
58
+
59
+ it "should configure 'admin_flag' strategy" do
60
+ create_model :user do
61
+ '# content'
62
+ end
63
+ with_generator do |g|
64
+ arguments = "User --strategy #{strategy} --roles admin user --orm #{orm}"
65
+ puts "arguments: #{arguments}"
66
+ g.run_generator arguments.args
67
+ g.should generate_model :user do |clazz|
68
+ clazz.should include_module 'RoleModels::Generic'
69
+ puts "clazz: #{clazz}"
70
+ clazz.should have_call :roles, :args => ':admin, :user'
71
+ clazz.should have_call :role_strategy, :args => ":#{strategy}"
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../generator_spec_helper')
2
+ require_generator :roles_model => :roles
3
+
4
+ describe 'helper_generator' do
5
+ let(:strategy) { 'many_roles' }
6
+
7
+ use_orm :active_record
8
+ helpers :model
9
+
10
+ before :each do
11
+ setup_generator 'roles_generator' do
12
+ tests RolesModel::Generators::RolesGenerator
13
+ end
14
+ remove_model 'user'
15
+ end
16
+
17
+ after :each do
18
+ remove_model 'user'
19
+ end
20
+
21
+ it "should configure 'many_roles' strategy" do
22
+ create_model :user do
23
+ '# content'
24
+ end
25
+ with_generator do |g|
26
+ arguments = "User --strategy #{strategy} --roles admin user --orm active_record"
27
+ puts "arguments: #{arguments}"
28
+ g.run_generator arguments.args
29
+ g.should generate_model :user do |clazz|
30
+ clazz.should include_module 'RoleModels::Generic'
31
+ clazz.should have_call :roles, :args => ':admin, :user'
32
+ clazz.should have_call :role_strategy, :args => ":#{strategy}"
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+
39
+
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../generator_spec_helper')
2
+ require_generator :roles_model => :roles
3
+
4
+ describe 'helper_generator' do
5
+ let(:strategy) { 'one_role' }
6
+
7
+ use_orm :active_record
8
+ helpers :model
9
+
10
+ before :each do
11
+ setup_generator 'roles_generator' do
12
+ tests RolesModel::Generators::RolesGenerator
13
+ end
14
+ remove_model 'user'
15
+ end
16
+
17
+ after :each do
18
+ remove_model 'user'
19
+ end
20
+
21
+ it "should configure 'one_role' strategy" do
22
+ create_model :user do
23
+ '# content'
24
+ end
25
+ with_generator do |g|
26
+ arguments = "User --strategy #{strategy} --roles admin user --orm active_record"
27
+ puts "arguments: #{arguments}"
28
+ g.run_generator arguments.args
29
+ g.should generate_model :user do |clazz|
30
+ clazz.should include_module 'RoleModels::Generic'
31
+ clazz.should have_call :roles, :args => ':admin, :user'
32
+ clazz.should have_call :role_strategy, :args => ":#{strategy}"
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+
39
+
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../generator_spec_helper')
2
+ require_generator :roles_model => :roles
3
+
4
+ describe 'helper_generator' do
5
+ let(:strategy) { 'role_string' }
6
+
7
+ use_orm :active_record
8
+ helpers :model
9
+
10
+ before :each do
11
+ setup_generator 'roles_generator' do
12
+ tests RolesModel::Generators::RolesGenerator
13
+ end
14
+ remove_model 'user'
15
+ end
16
+
17
+ after :each do
18
+ remove_model 'user'
19
+ end
20
+
21
+ it "should configure 'role_string' strategy" do
22
+ create_model :user do
23
+ '# content'
24
+ end
25
+ with_generator do |g|
26
+ arguments = "User --strategy #{strategy} --roles admin user --orm active_record"
27
+ puts "arguments: #{arguments}"
28
+ g.run_generator arguments.args
29
+ g.should generate_model :user do |clazz|
30
+ clazz.should include_module 'RoleModels::Generic'
31
+ clazz.should have_call :roles, :args => ':admin, :user'
32
+ clazz.should have_call :role_strategy, :args => ":#{strategy}"
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+
39
+
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../generator_spec_helper')
2
+ require_generator :roles_model => :roles
3
+
4
+ describe 'helper_generator' do
5
+ let(:strategy) { 'role_strings' }
6
+
7
+ use_orm :active_record
8
+ helpers :model
9
+
10
+ before :each do
11
+ setup_generator 'roles_generator' do
12
+ tests RolesModel::Generators::RolesGenerator
13
+ end
14
+ remove_model 'user'
15
+ end
16
+
17
+ after :each do
18
+ remove_model 'user'
19
+ end
20
+
21
+ it "should configure 'role_strings' strategy" do
22
+ create_model :user do
23
+ '# content'
24
+ end
25
+ with_generator do |g|
26
+ arguments = "User --strategy #{strategy} --roles admin user --orm active_record"
27
+ puts "arguments: #{arguments}"
28
+ g.run_generator arguments.args
29
+ g.should generate_model :user do |clazz|
30
+ clazz.should include_module 'RoleModels::Generic'
31
+ clazz.should have_call :roles, :args => ':admin, :user'
32
+ clazz.should have_call :role_strategy, :args => ":#{strategy}"
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+
39
+
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../generator_spec_helper')
2
+ require_generator :roles_model => :roles
3
+
4
+ describe 'helper_generator' do
5
+ let(:strategy) { 'roles_mask' }
6
+
7
+ use_orm :active_record
8
+ helpers :model
9
+
10
+ before :each do
11
+ setup_generator 'roles_generator' do
12
+ tests RolesModel::Generators::RolesGenerator
13
+ end
14
+ remove_model 'user'
15
+ end
16
+
17
+ after :each do
18
+ remove_model 'user'
19
+ end
20
+
21
+ it "should configure 'roles_mask' strategy" do
22
+ create_model :user do
23
+ '# content'
24
+ end
25
+ with_generator do |g|
26
+ arguments = "User --strategy #{strategy} --roles admin user --orm active_record"
27
+ puts "arguments: #{arguments}"
28
+ g.run_generator arguments.args
29
+ g.should generate_model :user do |clazz|
30
+ clazz.should include_module 'RoleModels::Generic'
31
+ clazz.should have_call :roles, :args => ':admin, :user'
32
+ clazz.should have_call :role_strategy, :args => ":#{strategy}"
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+
39
+
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../generator_spec_helper')
2
+ require_generator :roles_model => :roles
3
+
4
+ describe 'helper_generator' do
5
+ let(:strategy) { 'roles_string' }
6
+
7
+ use_orm :active_record
8
+ helpers :model
9
+
10
+ before :each do
11
+ setup_generator 'roles_generator' do
12
+ tests RolesModel::Generators::RolesGenerator
13
+ end
14
+ remove_model 'user'
15
+ end
16
+
17
+ after :each do
18
+ remove_model 'user'
19
+ end
20
+
21
+ it "should configure 'roles_string' strategy" do
22
+ create_model :user do
23
+ '# content'
24
+ end
25
+ with_generator do |g|
26
+ arguments = "User --strategy #{strategy} --roles admin user --orm active_record"
27
+ puts "arguments: #{arguments}"
28
+ g.run_generator arguments.args
29
+ g.should generate_model :user do |clazz|
30
+ clazz.should include_module 'RoleModels::Generic'
31
+ clazz.should have_call :roles, :args => ':admin, :user'
32
+ clazz.should have_call :role_strategy, :args => ":#{strategy}"
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+
39
+
@@ -0,0 +1,26 @@
1
+ class Role
2
+ attr_accessor :name
3
+
4
+ def self.find_role role_name
5
+ roles.to_a.select{|r| r.name == role_name}.first
6
+ end
7
+
8
+ def self.find_roles *role_names
9
+ result = Set.new
10
+ role_names.flatten.each do |role_name|
11
+ found_role = find_role(role_name)
12
+ result << found_role if found_role
13
+ end
14
+ result
15
+ end
16
+
17
+ class << self
18
+ attr_accessor :roles
19
+ end
20
+
21
+ def initialize name
22
+ @name = name
23
+ self.class.roles ||= Set.new
24
+ self.class.roles << self
25
+ end
26
+ end