roleup 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 63b885b52339e6ce02adf525f8aa38357c264e74
4
- data.tar.gz: 942bf4261de0d5c678ba77f3b6c372154772d2a3
3
+ metadata.gz: 5788b1e970d2996a5a2ed6a9d16cabd9d99c25b3
4
+ data.tar.gz: 4b26ad508625ff3c48f39c5a82ac4b39d8b33df2
5
5
  SHA512:
6
- metadata.gz: c3e23e86e3e8685bca7f3a70efa7c54aa7405f686df75ed46119baa81c880e03dcc2029c8435f1f7673772891adbaca3f6c70702611b35cca06bb37f33b1c962
7
- data.tar.gz: 1b6c743b65b409c48e7ad2088d40e2d92e3b554cd0951b62d802fb9392af84b852f6220ba4bd5dc285e518ce1ffb8eba385764d73f659330e9d36a88ed7d26ea
6
+ metadata.gz: 4cc8c5aa7f4630c489aecc332c2d8f5be52655d19a33e625dfa8a8ad9bfd08d8f9634994e5599feca8a6a67593ac823acc96859aa758f10ea5b4e48c02d672c6
7
+ data.tar.gz: 75c9efe1849081f76c38b239d6457bab025e7e7eb24158fbe5e3fdecc84739a6a46475b7ec5a3a38e6b4278f5bcdeec475ff6144a59fd03a6dfbde9b99e99827
@@ -11,5 +11,13 @@ module Roleup
11
11
  def has_role?(role)
12
12
  roles.include? role
13
13
  end
14
+
15
+ def has_all_roles?(*roles)
16
+ (self.roles & roles).size == roles.size
17
+ end
18
+
19
+ def has_one_role?(*roles)
20
+ !(self.roles & roles).empty?
21
+ end
14
22
  end
15
23
  end
data/lib/roleup/list.rb CHANGED
@@ -7,30 +7,39 @@ module Roleup
7
7
  def_delegators :inner_list, :each, :length, :size
8
8
 
9
9
  def initialize(*values)
10
- @inner_list = values.flatten.map { |value| standardize(value) }.uniq.sort
10
+ @inner_list = standardize_values(*values)
11
11
  end
12
12
 
13
13
  def include?(value)
14
- inner_list.include? standardize(value)
14
+ inner_list.include? standardize_value(value)
15
15
  end
16
16
 
17
17
  def push(value)
18
18
  return self if include?(value)
19
- inner_list << standardize(value)
19
+ inner_list << standardize_value(value)
20
20
  end
21
21
 
22
22
  alias_method :<<, :push
23
23
 
24
24
  def delete(value)
25
- inner_list.delete standardize(value)
25
+ inner_list.delete standardize_value(value)
26
+ end
27
+
28
+ def &(*values)
29
+ inner_list & standardize_values(*values)
26
30
  end
27
31
 
28
32
  protected
29
33
 
30
34
  attr_reader :inner_list
31
35
 
32
- def standardize(value)
36
+ def standardize_value(value)
33
37
  value.to_s.downcase.to_sym
34
38
  end
39
+
40
+ def standardize_values(*values)
41
+ values.flatten.map { |value| standardize_value(value) }.uniq.sort
42
+ end
43
+
35
44
  end
36
45
  end
@@ -1,3 +1,3 @@
1
1
  module Roleup
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -2,23 +2,32 @@ require_relative "test_helper"
2
2
 
3
3
  module Roleup
4
4
  class HasRolesTest < MicroTest::Test
5
+ class User
6
+ include HasRoles
7
+ end
5
8
 
6
9
  before do
7
- class_with_roles = Class.new do
8
- include HasRoles
9
- end
10
-
11
- @obj_with_roles = class_with_roles.new
12
- @obj_with_roles.roles = :admin, :editor
10
+ @user = User.new
11
+ @user.roles = :admin, :editor
13
12
  end
14
13
 
15
14
  test "assign roles" do
16
- assert @obj_with_roles.roles.to_a == [:admin, :editor]
15
+ assert @user.roles.to_a == [:admin, :editor]
17
16
  end
18
17
 
19
18
  test "has_role?" do
20
- assert @obj_with_roles.has_role?(:admin)
21
- assert @obj_with_roles.has_role?(:editor)
19
+ assert @user.has_role?(:admin)
20
+ assert @user.has_role?(:editor)
21
+ end
22
+
23
+ test "has_all_roles?" do
24
+ assert @user.has_all_roles?(:admin, :EDITOR)
25
+ assert !@user.has_all_roles?(:admin, :editor, :other)
26
+ end
27
+
28
+ test "has_one_role?" do
29
+ assert @user.has_one_role?(:other, :editor, :VIEWER)
30
+ assert !@user.has_one_role?(:other, :viewer)
22
31
  end
23
32
 
24
33
  end
data/test/list_test.rb CHANGED
@@ -40,5 +40,10 @@ module Roleup
40
40
  assert @roles.size == @roles.to_a.uniq.size
41
41
  end
42
42
 
43
+ test "intersection" do
44
+ list = @roles & [:editor, :viewer, :other]
45
+ assert list == [:editor, :viewer]
46
+ end
47
+
43
48
  end
44
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roleup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Hopkins