Permalink
Browse files

optimized API

  • Loading branch information...
1 parent 1602216 commit 0c6764b32b7ed8507dbab4106e067eec08721ec6 @kristianmandrup committed Dec 26, 2010
@@ -1,9 +1,9 @@
-# Roles for Mongoid
+h1. Roles for Mongoid
A Mongoid implementation of [Roles Generic](http://github.com/kristianmandrup/roles_generic).
See the Roles [wiki](http://github.com/kristianmandrup/roles_generic/wiki) for an overview of the API and how to use it.
-## Role strategies
+h2. Role strategies
Role strategies implemented:
@@ -24,11 +24,11 @@ Embedded Role
* embed_many_roles
* embed_one_role
-## Install
+h2. Install
<code>gem install roles_mongoid</code>
-## Install in Rails app
+h3. Install in Rails app
Insert in Gemfile:
@@ -38,7 +38,31 @@ Run <code>$ bundle install</code> from terminal
Alternatively install using [Cream](http://github.com/kristianmandrup/cream)
-## Rails generators
+h2. Strategy and roles configuration
+
+Example: _role_string_ strategy
+
+<pre>class User
+ include Mongoid::Document
+ include Roles::Mongoid
+
+ strategy :one_role
+ valid_roles_are :admin, :guest, :user
+end
+</pre>
+
+Example: _one_role_ strategy
+
+<pre>class User
+ include Mongoid::Document
+ include Roles::Mongoid
+
+ strategy :one_role
+ valid_roles_are :admin, :guest, :user
+end
+</pre>
+
+h2. Rails generators
The library comes with a Rails 3 generator that lets you populate a user model with a role strategy of your choice.
@@ -56,7 +80,7 @@ Apply :one_role Role strategy to User model without default roles, only with rol
<code>$ rails g mongoid:roles_migration User --strategy one_role --roles user special editor --no-default-roles</code>
-## Note on Patches/Pull Requests
+h2. Note on Patches/Pull Requests
* Fork the project.
* Make your feature addition or bug fix.
@@ -66,6 +90,6 @@ Apply :one_role Role strategy to User model without default roles, only with rol
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
* Send me a pull request. Bonus points for topic branches.
-## Copyright
+h2. Copyright
Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
View
@@ -28,26 +28,65 @@ module ClassMethods
:role_strings => "field :role_strings, :type => Array",
:roles_string => "field :roles_string, :type => String"
}
-
+
def strategy name, options = {}
- if (options == :default || options[:config] == :default) && MAP[name]
- instance_eval MAP[name]
- end
- if !options.kind_of? Symbol
- role_class = options[:role_class] ? options[:role_class].to_s.camelize.constantize : (Role if defined? Role)
+ strategy_name = name.to_sym
+ raise ArgumentError, "Unknown role strategy #{strategy_name}" if !MAP.keys.include? strategy_name
+ use_roles_strategy strategy_name
+
+ if strategies_with_role_class.include? strategy_name
+ if !options.kind_of? Symbol
+ @role_class_name = get_role_class(strategy_name, options)
+ else
+ @role_class_name = default_role_class(strategy_name)
+ end
end
+
+ if default_options?(options) && MAP[strategy_name]
+ instance_eval statement(MAP[strategy_name])
+ end
case name
when :embed_one_role
- raise ArgumentError, "#strategy class method must take :role_class option when using an embedded role strategy" if !role_class
- role_class.embedded_in :user, :inverse_of => :one_role
+ raise ArgumentError, "#strategy class method must take :role_class option when using an embedded role strategy" if !@role_class_name
+ @role_class_name.embedded_in :user, :inverse_of => :one_role
when :embed_many_roles
- raise ArgumentError, "#strategy class method must take :role_class option when using an embedded role strategy" if !role_class
- role_class.embedded_in :user, :inverse_of => :many_roles
+ raise ArgumentError, "#strategy class method must take :role_class option when using an embedded role strategy" if !@role_class_name
+ @role_class_name.embedded_in :user, :inverse_of => :many_roles
end
-
- set_role_strategy name, options
+
+ set_role_strategy strategy_name, options
end
+
+ private
+
+ def default_options? options = {}
+ return true if options == :default
+ if options.kind_of? Hash
+ return true # if options[:config] == :default || options == {}
+ end
+ false
+ end
+
+ def statement code_str
+ code_str.gsub /Role/, @role_class_name.to_s
+ end
+
+ def default_role_class strategy_name
+ if defined? ::Role
+ require "roles_mongoid/role"
+ return ::Role
+ end
+ raise "Default Role class not defined"
+ end
+
+ def strategies_with_role_class
+ [:one_role, :embed_one_role, :many_roles,:embed_many_roles]
+ end
+
+ def get_role_class strategy_name, options
+ options[:role_class] ? options[:role_class].to_s.camelize.constantize : default_role_class(strategy_name)
+ end
end
end
end
@@ -0,0 +1,13 @@
+module Roles::Base
+ def valid_roles_are(*role_list)
+ strategy_class.valid_roles = role_list.to_symbols
+ if role_class_name
+ role_list.each do |name|
+ begin
+ role_class_name.create(:name => name.to_s)
+ rescue
+ end
+ end
+ end
+ end
+end
@@ -1,5 +1,3 @@
-puts "Embedded Role"
-
module Roles::Base
def valid_roles_are(*role_list)
strategy_class.valid_roles = role_list.to_symbols
View
@@ -1,16 +1,4 @@
-module Roles::Base
- def valid_roles_are(*role_list)
- strategy_class.valid_roles = role_list.to_symbols
- if role_class_name
- role_list.each do |name|
- begin
- role_class_name.create(:name => name.to_s)
- rescue
- end
- end
- end
- end
-end
+require 'roles_mongoid/base_role'
class Role
include Mongoid::Document
@@ -3,7 +3,6 @@ module Strategy
module Shared
def set_role role
raise ArgumentError, "#set_role only takes a single role as argument, not #{role}" if role.kind_of?(Array)
- puts "role: #{role.inspect}, attrib: #{role_attribute.inspect}"
self.send("#{role_attribute}=", role)
end
Oops, something went wrong.

0 comments on commit 0c6764b

Please sign in to comment.