roles_mongo_mapper 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -1,47 +1,75 @@
1
1
  # Roles for Mongo Mapper
2
2
 
3
- A *Mongo Mapper* implementation of [roles generic](http://github.com/kristianmandrup/roles_generic)
3
+ A Mongo Mapper implementation of [Roles Generic](http://github.com/kristianmandrup/roles_generic)
4
+ See the Roles [wiki](http://github.com/kristianmandrup/roles_generic/wiki) for an overview of the API and how to use it.
4
5
 
5
- ## Intro
6
+ ## Role strategies
6
7
 
7
- Implements the [roles generic](http://github.com/kristianmandrup/roles_generic) Roles API
8
+ Role strategies implemented:
8
9
 
9
- Generic Role strategies implemented:
10
+ Inline attribute on User:
10
11
 
11
12
  * admin_flag
12
- * many_roles
13
- * one_role
14
13
  * roles_mask
15
14
  * role_string
16
15
  * role_strings
17
16
 
18
- *Update Nov 24, 2010*
19
- Version 0.3.1 and up is a major refactoring to support Roles Generic 0.3 and above with a new and improved architecture and testing framework.
20
- Since 0.3.2 development of some basic infrastructure to support embedded role strategies in the future has been initiated.
17
+ Reference to Role:
18
+
19
+ * many_roles
20
+ * one_role
21
+
22
+ *Update Dec 24, 2010*
23
+
24
+ Embedded role:
21
25
 
22
26
  * embed_one_role
23
27
  * embed_many_roles
24
28
 
25
- Please join in the effort to implement and add these strategies to the mix.
29
+ Please join in the effort to implement and add these embedded strategies to the mix.
30
+ They have so far been implemented successfully for Mongoid.
26
31
 
27
- ## Install
32
+ ## Install as system gem
28
33
 
29
34
  <code>gem install roles_mongo_mapper</code>
30
35
 
31
- ## Rails generator
36
+ ## Install in Rails app
32
37
 
33
- The library comes with a Rails 3 generator that lets you populate a user model with a role strategy of your choice.
38
+ Insert in Gemfile:
34
39
 
35
- The following role strategies are included by default. Add your own by adding extra files inside the strategy folder, one file for each role strategy is recommended.
40
+ <code>gem 'roles_mongo_mapper'</code>
36
41
 
37
- * admin_flag
38
- * many_roles
39
- * one_role
40
- * roles_mask
41
- * role_string
42
- * role_strings
42
+ Run <code>$ bundle install</code> from terminal
43
+
44
+ Alternatively install using [Cream](http://github.com/kristianmandrup/cream)
45
+
46
+ ## Strategy and roles configuration
47
+
48
+ Example: _role_string_ strategy
49
+
50
+ <pre>class User
51
+ include MongoMapper::Document
52
+ include Roles::MongoMapper
53
+
54
+ strategy :one_role
55
+ valid_roles_are :admin, :guest, :user
56
+ end
57
+ </pre>
58
+
59
+ Example: _one_role_ strategy
43
60
 
44
- *Roles generator*
61
+ <pre>class User
62
+ include MongoMapper::Document
63
+ include Roles::MongoMapper
64
+
65
+ strategy :one_role
66
+ valid_roles_are :admin, :guest, :user
67
+ end
68
+ </pre>
69
+
70
+ ## Rails generator
71
+
72
+ The library comes with a Rails 3 generator that lets you populate a user model with a role strategy of your choice.
45
73
 
46
74
  Apply :admin_flag Role strategy to User model using default roles :admin and :guest (default)
47
75
 
@@ -55,7 +83,6 @@ Apply :one_role Role strategy to User model without default roles, only with rol
55
83
 
56
84
  <code>$ rails g mongo_mapper:roles_migration User --strategy one_role --roles user special editor --no-default-roles</code>
57
85
 
58
-
59
86
  ## Note on Patches/Pull Requests
60
87
 
61
88
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.0
@@ -26,26 +26,77 @@ module Roles::MongoMapper
26
26
  :role_strings => "key :role_strings, Array",
27
27
  :roles_string => "key :roles_string, String"
28
28
  }
29
+
30
+ def valid_single_strategies
31
+ [:admin_flag, :one_role, :role_string]
32
+ end
33
+
34
+ def valid_multi_strategies
35
+ [:many_roles, :roles_mask, :role_strings]
36
+ end
37
+
38
+ def valid_strategies
39
+ valid_single_strategies + valid_multi_strategies
40
+ end
29
41
 
30
42
  def strategy name, options = {}
31
- if (options == :default || options[:config] == :default) && MAP[name]
32
- instance_eval MAP[name]
33
- end
43
+ strategy_name = name.to_sym
44
+ raise ArgumentError, "Unknown role strategy #{strategy_name}" if !valid_strategies.include? strategy_name
45
+ use_roles_strategy strategy_name
34
46
 
35
- if !options.kind_of? Symbol
36
- role_class = options[:role_class] ? options[:role_class].to_s.camelize.constantize : (Role if defined? Role)
47
+ if strategies_with_role_class.include? strategy_name
48
+ if !options.kind_of? Symbol
49
+ @role_class_name = get_role_class(strategy_name, options)
50
+ else
51
+ @role_class_name = default_role_class(strategy_name)
52
+ end
37
53
  end
54
+
55
+ if default_options?(options) && MAP[strategy_name]
56
+ instance_eval statement(MAP[strategy_name])
57
+ end
38
58
 
39
59
  # case name
40
60
  # when :embed_one_role
41
- # raise ArgumentError, "#strategy class method must take :role_class option when using an embedded role strategy" if !role_class
42
- # role_class.embedded_in :user, :inverse_of => :one_role
61
+ # raise ArgumentError, "#strategy class method must take :role_class option when using an embedded role strategy" if !@role_class_name
62
+ # @role_class_name.embedded_in :user, :inverse_of => :one_role
43
63
  # when :embed_many_roles
44
- # raise ArgumentError, "#strategy class method must take :role_class option when using an embedded role strategy" if !role_class
45
- # role_class.embedded_in :user, :inverse_of => :many_roles
64
+ # raise ArgumentError, "#strategy class method must take :role_class option when using an embedded role strategy" if !@role_class_name
65
+ # @role_class_name.embedded_in :user, :inverse_of => :many_roles
46
66
  # end
47
-
48
- set_role_strategy name, options
67
+
68
+ set_role_strategy strategy_name, options
49
69
  end
70
+
71
+ private
72
+
73
+ def default_options? options = {}
74
+ return true if options == :default
75
+ if options.kind_of? Hash
76
+ return true # if options[:config] == :default || options == {}
77
+ end
78
+ false
79
+ end
80
+
81
+ def statement code_str
82
+ code_str.gsub /Role/, @role_class_name.to_s
83
+ end
84
+
85
+ def default_role_class strategy_name
86
+ if defined? ::Role
87
+ require "roles_mongo_mapper/role"
88
+ return ::Role
89
+ end
90
+ raise "Default Role class not defined"
91
+ end
92
+
93
+ def strategies_with_role_class
94
+ # :embed_one_role, :embed_many_roles
95
+ [:one_role, :many_roles]
96
+ end
97
+
98
+ def get_role_class strategy_name, options
99
+ options[:role_class] ? options[:role_class].to_s.camelize.constantize : default_role_class(strategy_name)
100
+ end
50
101
  end
51
102
  end
@@ -9,6 +9,8 @@ module RoleStrategy::MongoMapper
9
9
  def self.included base
10
10
  base.extend Roles::Generic::Role::ClassMethods
11
11
  base.extend ClassMethods
12
+
13
+ # TODO - refactor into strategy method in base.rb
12
14
  base.key :many_roles_ids, Array, :typecast => 'ObjectId'
13
15
  base.many :many_roles, :class_name => 'Role', :in => :many_roles_ids
14
16
  base.ensure_index :many_role_ids
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{roles_mongo_mapper}
8
- s.version = "0.2.2"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = %q{2010-12-21}
12
+ s.date = %q{2010-12-27}
13
13
  s.description = %q{Makes it easy to set a role strategy on your User model in MongoMapper}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -1,12 +1,10 @@
1
1
  require 'spec_helper'
2
- use_roles_strategy :many_roles
3
2
 
4
3
  class User
5
4
  include MongoMapper::Document
6
5
  include Roles::MongoMapper
7
6
 
8
- strategy :many_roles, :default
9
- role_class :role
7
+ strategy :many_roles
10
8
  valid_roles_are :admin, :guest, :user
11
9
 
12
10
  key :name, :type => String
@@ -1,11 +1,9 @@
1
1
  require 'spec_helper'
2
- use_roles_strategy :role_strings
3
-
4
2
  class User
5
3
  include MongoMapper::Document
6
4
  include Roles::MongoMapper
7
5
 
8
- strategy :role_strings, :default
6
+ strategy :role_strings
9
7
  valid_roles_are :admin, :guest, :user
10
8
 
11
9
  key :name, :type => String
@@ -1,11 +1,9 @@
1
1
  require 'spec_helper'
2
- use_roles_strategy :roles_mask
3
-
4
2
  class User
5
3
  include MongoMapper::Document
6
4
  include Roles::MongoMapper
7
5
 
8
- strategy :roles_mask, :default
6
+ strategy :roles_mask
9
7
  valid_roles_are :admin, :guest, :user
10
8
 
11
9
  key :name, :type => String
@@ -1,11 +1,9 @@
1
1
  require 'spec_helper'
2
- use_roles_strategy :admin_flag
3
-
4
2
  class User
5
3
  include MongoMapper::Document
6
4
  include Roles::MongoMapper
7
5
 
8
- strategy :admin_flag, :default
6
+ strategy :admin_flag
9
7
  valid_roles_are :admin, :guest
10
8
 
11
9
  key :name, :type => String
@@ -1,12 +1,9 @@
1
1
  require 'spec_helper'
2
- use_roles_strategy :one_role
3
-
4
2
  class User
5
3
  include MongoMapper::Document
6
4
  include Roles::MongoMapper
7
5
 
8
- strategy :one_role, :default
9
- role_class :role
6
+ strategy :one_role
10
7
  valid_roles_are :admin, :guest
11
8
 
12
9
  key :name, :type => String
@@ -1,11 +1,9 @@
1
1
  require 'spec_helper'
2
- use_roles_strategy :role_string
3
-
4
2
  class User
5
3
  include MongoMapper::Document
6
4
  include Roles::MongoMapper
7
5
 
8
- strategy :role_string, :default
6
+ strategy :role_string
9
7
  valid_roles_are :admin, :guest
10
8
 
11
9
  key :name, :type => String
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 2
9
- version: 0.2.2
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-21 00:00:00 +01:00
17
+ date: 2010-12-27 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency