Module: Roles::MongoMapper::ClassMethods

Defined in:
lib/roles_mongo_mapper/base.rb

Constant Summary collapse

MAP =
{
  :admin_flag   => "key :admin_flag, Boolean",
  
  # :embed_many_roles   => "many :many_roles, :class_name => 'Role'",
  # :embed_one_role     => "one :one_role, :class_name => 'Role'",

  :roles_mask   => "key :roles_mask,    Integer, :default => 0",
  :role_string  => "key :role_string,   String",
  :role_strings => "key :role_strings,  Array",
  :roles_string => "key :roles_string,  String"
}

Instance Method Summary collapse

Instance Method Details

#strategy(name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/roles_mongo_mapper/base.rb', line 40

def strategy name, options = {}
  strategy_name = name.to_sym
  raise ArgumentError, "Unknown role strategy #{strategy_name}" if !valid_strategies.include? strategy_name
  use_roles_strategy strategy_name

  set_role_class(strategy_name, options) if strategies_with_role_class.include? strategy_name
  
  if default_options?(options) && MAP[strategy_name]
    instance_eval statement(MAP[strategy_name])
  end       

  # one_role reference
  if strategy_name == :one_role
    key :one_role_id, ObjectId 
    key :one_role, @role_class_name
  end
  
  # many_roles references
  if strategy_name == :many_roles
    instance_eval %{
      key :many_roles_ids, Array, :typecast => 'ObjectId'
      many :many_roles, :class_name => '#{@role_class_name}', :in => :many_roles_ids 
      ensure_index :many_role_ids     
    }
  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_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_name
  #   @role_class_name.embedded_in :user, :inverse_of => :many_roles
  # end

  set_role_strategy strategy_name, options
end

#valid_multi_strategiesObject



32
33
34
# File 'lib/roles_mongo_mapper/base.rb', line 32

def valid_multi_strategies
  [:many_roles, :roles_mask, :role_strings]
end

#valid_single_strategiesObject



28
29
30
# File 'lib/roles_mongo_mapper/base.rb', line 28

def valid_single_strategies
  [:admin_flag, :one_role, :role_string]
end

#valid_strategiesObject



36
37
38
# File 'lib/roles_mongo_mapper/base.rb', line 36

def valid_strategies
  valid_single_strategies + valid_multi_strategies
end