roles_generic 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -1,5 +1,8 @@
1
1
  module Roles::Generic::User
2
2
  module Implementation
3
+ def role_attribute
4
+ strategy_class.roles_attribute_name
5
+ end
3
6
 
4
7
  # check if a given role has been assigned
5
8
  # if a list of roles: check if ALL of the given roles have been assigned
@@ -1,17 +1,10 @@
1
- require 'set'
2
- require 'roles_generic/role'
3
-
4
1
  module RoleStrategy::Generic
5
2
  module ManyRoles
6
3
  def self.default_role_attribute
7
4
  :many_roles
8
5
  end
9
6
 
10
- module Implementation
11
- def role_attribute
12
- strategy_class.roles_attribute_name
13
- end
14
-
7
+ module Implementation
15
8
  # assign roles
16
9
  def roles=(*roles)
17
10
  raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
@@ -1,21 +1,19 @@
1
- require 'set'
2
-
3
1
  module RoleStrategy::Generic
4
2
  module RoleStrings
5
3
  def self.default_role_attribute
6
4
  :role_strings
7
5
  end
8
6
 
9
- module Implementation
7
+ module Implementation
10
8
  # assign roles
11
9
  def roles=(*roles)
12
10
  new_roles = roles.flatten.map{|r| r.to_s if valid_role?(r)}.compact
13
- self.send("#{strategy_class.roles_attribute_name}=", ::Set.new(new_roles)) if new_roles && new_roles.not.empty?
11
+ self.send("#{role_attribute}=", ::Set.new(new_roles)) if new_roles && new_roles.not.empty?
14
12
  end
15
13
 
16
14
  # query assigned roles
17
15
  def roles
18
- self.send(strategy_class.roles_attribute_name).map{|r| r.to_sym}
16
+ self.send(role_attribute).map{|r| r.to_sym}
19
17
  end
20
18
 
21
19
  def roles_list
@@ -1,5 +1,3 @@
1
- require 'set'
2
-
3
1
  module RoleStrategy::Generic
4
2
  module RolesMask
5
3
  def self.default_role_attribute
@@ -23,12 +21,13 @@ module RoleStrategy::Generic
23
21
 
24
22
  # assign roles
25
23
  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 })
24
+ self.send("#{role_attribute}=", (roles.flatten.map { |r| r.to_sym } & strategy_class.valid_roles).map { |r| calc_index(r) }.inject { |sum, bitvalue| sum + bitvalue })
27
25
  end
26
+ alias_method :role=, :roles=
28
27
 
29
28
  # query assigned roles
30
29
  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? })
30
+ strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((self.send(role_attribute) || 0) & calc_index(r)).zero? })
32
31
  end
33
32
 
34
33
  def roles_list
@@ -4,16 +4,17 @@ module RoleStrategy::Generic
4
4
  :roles_string
5
5
  end
6
6
 
7
- module Implementation
7
+ module Implementation
8
8
  # assign roles
9
9
  def roles=(*roles)
10
10
  roles_str = roles.flatten.map{|r| r.to_s if valid_role?(r)}.compact.uniq.join(',')
11
- self.send("#{strategy_class.roles_attribute_name}=", roles_str) if roles_str && roles_str.not.empty?
12
- end
11
+ self.send("#{role_attribute}=", roles_str) if roles_str && roles_str.not.empty?
12
+ end
13
+ alias_method :role=, :roles=
13
14
 
14
15
  # query assigned roles
15
16
  def roles
16
- self.send(strategy_class.roles_attribute_name).split(',').uniq.map{|r| r.to_sym}
17
+ self.send(role_attribute).split(',').uniq.map{|r| r.to_sym}
17
18
  end
18
19
  alias_method :roles_list, :roles
19
20
  end
@@ -4,11 +4,7 @@ module RoleStrategy::Generic
4
4
  :admin_flag
5
5
  end
6
6
 
7
- module Implementation
8
- def role_attribute
9
- strategy_class.roles_attribute_name
10
- end
11
-
7
+ module Implementation
12
8
  # assign roles
13
9
  def roles=(*new_roles)
14
10
  first_role = new_roles.flatten.first
@@ -1,13 +1,10 @@
1
- require 'set'
2
- require 'roles_generic/role'
3
-
4
1
  module RoleStrategy::Generic
5
2
  module OneRole
6
3
  def self.default_role_attribute
7
4
  :one_role
8
5
  end
9
6
 
10
- module Implementation
7
+ module Implementation
11
8
  # assign roles
12
9
  def roles=(*roles)
13
10
  raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
@@ -15,14 +12,13 @@ module RoleStrategy::Generic
15
12
  first_role = roles.flatten.first
16
13
  role_relation = role_class.find_role(first_role)
17
14
  if role_relation && role_relation.kind_of?(role_class)
18
- self.send("#{strategy_class.roles_attribute_name}=", role_relation)
15
+ self.send("#{role_attribute}=", role_relation)
19
16
  end
20
17
  end
21
18
 
22
19
  # query assigned roles
23
20
  def roles
24
- role = self.send(strategy_class.roles_attribute_name).name.to_sym
25
- [role]
21
+ [self.send(role_attribute).name.to_sym]
26
22
  end
27
23
 
28
24
  def roles_list
@@ -1,24 +1,22 @@
1
- require 'set'
2
-
3
1
  module RoleStrategy::Generic
4
2
  module RoleString
5
3
  def self.default_role_attribute
6
4
  :role_string
7
5
  end
8
6
 
9
- module Implementation
7
+ module Implementation
10
8
  # assign roles
11
9
  def roles=(*roles)
12
10
  first_role = roles.flatten.first.to_s
13
11
  if valid_role? first_role
14
- self.send("#{strategy_class.roles_attribute_name}=", first_role)
12
+ self.send("#{role_attribute}=", first_role)
15
13
  end
16
14
  end
15
+ alias_method :role=, :roles=
17
16
 
18
17
  # query assigned roles
19
18
  def roles
20
- role = self.send(strategy_class.roles_attribute_name)
21
- [role.to_sym]
19
+ [self.send(role_attribute).to_sym]
22
20
  end
23
21
  alias_method :roles_list, :roles
24
22
  end
@@ -2,25 +2,37 @@ require 'sugar-high/file'
2
2
  require 'sugar-high/array'
3
3
 
4
4
  module Roles::Strategy
5
- def self.role_strategies cardinality
6
- pattern = File.dirname(__FILE__) + "/strategy/#{cardinality}/*.rb"
7
- Dir.glob(pattern).file_names(:rb).to_symbols
8
- end
5
+ class << self
6
+ NON_INLINE_STRATEGIES = [:one_role, :many_roles]
7
+
8
+ def role_strategies cardinality
9
+ pattern = File.dirname(__FILE__) + "/strategy/#{cardinality}/*.rb"
10
+ Dir.glob(pattern).file_names(:rb).to_symbols
11
+ end
9
12
 
10
- def self.has_strategy? cardinality, strategy
11
- role_strategies(cardinality).include?(strategy)
12
- end
13
+ def has_strategy? cardinality, strategy
14
+ role_strategies(cardinality).include?(strategy)
15
+ end
16
+
17
+ def inline_strategy? strategy
18
+ !NON_INLINE_STRATEGIES.include? strategy.to_sym
19
+ end
13
20
 
14
- def self.cardinality strategy
15
- [:single, :multi].each do |cardinality|
16
- return cardinality if has_strategy?(cardinality, strategy)
21
+ def cardinality strategy
22
+ [:single, :multi].each do |cardinality|
23
+ return cardinality if has_strategy?(cardinality, strategy)
24
+ end
25
+ raise ArgumentError, "Strategy #{strategy} is not registered as either a single or multi cardinality role strategy"
17
26
  end
18
- raise ArgumentError, "Strategy #{strategy} is not registered as either a single or multi cardinality role strategy"
19
27
  end
20
28
  end
21
29
 
22
30
  def use_roles_strategy strategy
23
- cardinality = Roles::Strategy.cardinality(strategy)
31
+ cardinality = Roles::Strategy.cardinality(strategy)
32
+ require "roles_generic/strategy/#{cardinality}/#{strategy}"
33
+
34
+ require 'roles_generic/role' if !Roles::Strategy.inline_strategy? strategy
24
35
  require "roles_generic/strategy/#{cardinality}/#{strategy}"
25
36
  end
26
37
 
38
+
data/lib/roles_generic.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'require_all'
2
2
  require 'active_support/inflector'
3
3
  require 'sugar-high/not'
4
-
4
+ require 'set'
5
5
  require 'roles_generic/namespaces'
6
6
  require 'roles_generic/base'
7
7
  require 'roles_generic/generic'
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{roles_generic}
8
- s.version = "0.2.1"
8
+ s.version = "0.2.2"
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"]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 1
9
- version: 0.2.1
8
+ - 2
9
+ version: 0.2.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup