grognard 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +31 -1
- data/Guardfile +24 -0
- data/README.md +12 -6
- data/Rakefile +14 -1
- data/grognard.gemspec +1 -0
- data/lib/grognard/guard.rb +40 -7
- data/lib/grognard/version.rb +1 -1
- data/spec/guard_spec.rb +60 -0
- metadata +18 -4
- data/spec/grognard_spec.rb +0 -37
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf2f4012530f7e971dde4367be8d7c7add80351d
|
4
|
+
data.tar.gz: a32e519959cc53f9b637017ff8ca9470ac2755ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9a60938baf53b906e0fda41de35b2e7c0e5a2b9a225c2894e789aebf18da84e174cd3f35278ef68c97b0ea7ff5039f2ce4b7658fc96e8045fc1b27875fd1872
|
7
|
+
data.tar.gz: 15b4248d5d7924cbff5626ff24e085290c77ba15278af9af95019aa18039751d7a1f8903a48a220e1123f1794658fe64d01e5800c93df3b1668196a808078b4d
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
grognard (0.0.
|
4
|
+
grognard (0.0.2)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -14,16 +14,43 @@ GEM
|
|
14
14
|
tzinfo (~> 0.3.37)
|
15
15
|
ast (1.1.0)
|
16
16
|
atomic (1.1.14)
|
17
|
+
celluloid (0.15.2)
|
18
|
+
timers (~> 1.1.0)
|
19
|
+
coderay (1.1.0)
|
17
20
|
diff-lcs (1.2.5)
|
21
|
+
ffi (1.9.3)
|
22
|
+
formatador (0.2.4)
|
23
|
+
guard (2.2.3)
|
24
|
+
formatador (>= 0.2.4)
|
25
|
+
listen (~> 2.1)
|
26
|
+
lumberjack (~> 1.0)
|
27
|
+
pry (>= 0.9.12)
|
28
|
+
thor (>= 0.18.1)
|
29
|
+
guard-rspec (4.0.4)
|
30
|
+
guard (>= 2.1.1)
|
31
|
+
rspec (~> 2.14)
|
18
32
|
i18n (0.6.5)
|
33
|
+
listen (2.2.0)
|
34
|
+
celluloid (>= 0.15.2)
|
35
|
+
rb-fsevent (>= 0.9.3)
|
36
|
+
rb-inotify (>= 0.9)
|
37
|
+
lumberjack (1.0.4)
|
38
|
+
method_source (0.8.2)
|
19
39
|
minitest (4.7.5)
|
20
40
|
multi_json (1.8.2)
|
21
41
|
parser (2.0.0)
|
22
42
|
ast (~> 1.1)
|
23
43
|
slop (~> 3.4, >= 3.4.5)
|
24
44
|
powerpack (0.0.9)
|
45
|
+
pry (0.9.12.3)
|
46
|
+
coderay (~> 1.0)
|
47
|
+
method_source (~> 0.8)
|
48
|
+
slop (~> 3.4)
|
25
49
|
rainbow (1.1.4)
|
26
50
|
rake (10.1.0)
|
51
|
+
rb-fsevent (0.9.3)
|
52
|
+
rb-inotify (0.9.2)
|
53
|
+
ffi (>= 0.5.0)
|
27
54
|
rspec (2.14.1)
|
28
55
|
rspec-core (~> 2.14.0)
|
29
56
|
rspec-expectations (~> 2.14.0)
|
@@ -37,8 +64,10 @@ GEM
|
|
37
64
|
powerpack (~> 0.0.6)
|
38
65
|
rainbow (>= 1.1.4)
|
39
66
|
slop (3.4.7)
|
67
|
+
thor (0.18.1)
|
40
68
|
thread_safe (0.1.3)
|
41
69
|
atomic
|
70
|
+
timers (1.1.0)
|
42
71
|
tzinfo (0.3.38)
|
43
72
|
|
44
73
|
PLATFORMS
|
@@ -48,6 +77,7 @@ DEPENDENCIES
|
|
48
77
|
activesupport (~> 4.0.1)
|
49
78
|
bundler (~> 1.3)
|
50
79
|
grognard!
|
80
|
+
guard-rspec (~> 4.0.4)
|
51
81
|
rake
|
52
82
|
rspec (~> 2.14.1)
|
53
83
|
rubocop (~> 0.15.0)
|
data/Guardfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard :rspec do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
# Rails example
|
10
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
11
|
+
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
12
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
13
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
14
|
+
watch('config/routes.rb') { "spec/routing" }
|
15
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
16
|
+
|
17
|
+
# Capybara features specs
|
18
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
19
|
+
|
20
|
+
# Turnip features and steps
|
21
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
22
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
23
|
+
end
|
24
|
+
|
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
-
|
1
|
+
Grognard
|
2
2
|
========
|
3
3
|
|
4
|
-
A lightweight authorization library
|
5
|
-
|
4
|
+
A lightweight cancan-like authorization library.
|
6
5
|
|
7
6
|
## Usage
|
8
7
|
|
@@ -34,18 +33,25 @@ end
|
|
34
33
|
|
35
34
|
# meanwhile in the console
|
36
35
|
|
37
|
-
guard = Grognard.new
|
36
|
+
guard = Grognard::Guard.new
|
38
37
|
|
39
38
|
guard << Customer # learn the abilities of a customer
|
40
39
|
guard << Attendant # learn the abilities of an attendant
|
41
40
|
|
42
|
-
guard.be_a_customer
|
41
|
+
guard.be_a_customer # to be a customer
|
43
42
|
guard.allowed_to_drink_scotch? @drunker, @scotch # => true
|
44
43
|
guard.allowed_to_grumble? :everyone, @shit # => true
|
45
44
|
guard.allowed_to_grumble? @drunker, @peri_peri # => false
|
46
45
|
|
47
|
-
guard.be_a_attendant
|
46
|
+
guard.be_a_attendant # to be a attendant
|
48
47
|
guard.allowed_to_free_deliver @attendant, @mussels # => true
|
49
48
|
guard.allowed_to_free_deliver @attendant, @perfume # => false
|
50
49
|
|
50
|
+
# role checking
|
51
|
+
guard.can_be_a_customer? # => true
|
52
|
+
guard.can_be_a_soldier? # => false
|
53
|
+
|
54
|
+
guard.remove_role? :customer # is not a customer anymore
|
55
|
+
guard.can_be_a_customer? # => false
|
56
|
+
|
51
57
|
```
|
data/Rakefile
CHANGED
@@ -1 +1,14 @@
|
|
1
|
-
require
|
1
|
+
require 'rspec/core/rake_task'
|
2
|
+
|
3
|
+
desc "Run an IRB session with Grognard preloaded"
|
4
|
+
task :console do
|
5
|
+
exec "irb -I lib -r grognard"
|
6
|
+
end
|
7
|
+
|
8
|
+
desc "Run the test suite"
|
9
|
+
RSpec::Core::RakeTask.new(:rspec) do |t|
|
10
|
+
t.pattern = FileList['spec/**/*_spec.rb']
|
11
|
+
t.rspec_opts = %w|--color --format doc|
|
12
|
+
end
|
13
|
+
|
14
|
+
task default: :spec
|
data/grognard.gemspec
CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
22
|
spec.add_development_dependency "rake"
|
23
23
|
spec.add_development_dependency "rspec", "~> 2.14.1"
|
24
|
+
spec.add_development_dependency "guard-rspec", "~> 4.0.4"
|
24
25
|
spec.add_development_dependency "rubocop", "~> 0.15.0"
|
25
26
|
spec.add_development_dependency "activesupport", "~> 4.0.1"
|
26
27
|
end
|
data/lib/grognard/guard.rb
CHANGED
@@ -6,18 +6,25 @@ module Grognard
|
|
6
6
|
class Guard
|
7
7
|
attr_reader :can_dos, :current_abilities
|
8
8
|
|
9
|
-
def initialize
|
9
|
+
def initialize(*arr_roles, hash_roles: {})
|
10
10
|
@can_dos = {}
|
11
11
|
@current_abilities = nil
|
12
|
+
|
13
|
+
insert_array_roles(arr_roles) ||
|
14
|
+
insert_hash_roles(hash_roles)
|
12
15
|
end
|
13
16
|
|
14
17
|
def be_a(name)
|
15
|
-
@current_abilities = @can_dos[name.to_sym] if role_defined
|
18
|
+
@current_abilities = @can_dos[name.to_sym] if role_defined! name
|
16
19
|
self
|
17
20
|
end
|
18
21
|
|
19
22
|
def add_role(name, object)
|
20
|
-
@can_dos[name.to_sym] = object if abilities_defined
|
23
|
+
@can_dos[name.to_sym] = object if abilities_defined!(object)
|
24
|
+
end
|
25
|
+
|
26
|
+
def remove_role(name)
|
27
|
+
@can_dos.delete name.to_sym
|
21
28
|
end
|
22
29
|
|
23
30
|
def <<(object)
|
@@ -30,13 +37,20 @@ module Grognard
|
|
30
37
|
end
|
31
38
|
|
32
39
|
def abilities_defined?(object)
|
33
|
-
object.respond_to?(:allowed_to)
|
40
|
+
object.respond_to?(:allowed_to)
|
41
|
+
end
|
42
|
+
|
43
|
+
def abilities_defined!(object)
|
44
|
+
abilities_defined?(object) ||
|
34
45
|
raise_no_such_abilites
|
35
46
|
end
|
36
47
|
|
37
48
|
def role_defined?(name)
|
38
|
-
@can_dos.key?(name.to_sym)
|
39
|
-
|
49
|
+
@can_dos.key?(name.to_sym)
|
50
|
+
end
|
51
|
+
|
52
|
+
def role_defined!(name)
|
53
|
+
role_defined?(name) || raise_no_such_role
|
40
54
|
end
|
41
55
|
|
42
56
|
def method_missing(meth, *args, &block)
|
@@ -44,6 +58,8 @@ module Grognard
|
|
44
58
|
case
|
45
59
|
when meth.match(/^be_a_(\w+)/)
|
46
60
|
be_a($1, *args, &block)
|
61
|
+
when meth.match(/^can_be_a_(\w+)?/)
|
62
|
+
can_be_a?($1, *args, &block)
|
47
63
|
when meth.match(/^can_(\w+)?/)
|
48
64
|
can?($1.to_sym, *args, &block)
|
49
65
|
else
|
@@ -66,6 +82,23 @@ module Grognard
|
|
66
82
|
def raise_no_such_role
|
67
83
|
fail RoleNotDefinedError.new
|
68
84
|
end
|
69
|
-
end
|
70
85
|
|
86
|
+
private
|
87
|
+
|
88
|
+
def insert_array_roles(roles)
|
89
|
+
return unless roles.kind_of? Array
|
90
|
+
roles.each do |role|
|
91
|
+
add_role(role.name.underscore, role)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def insert_hash_roles(roles)
|
96
|
+
return unless roles.kind_of? Hash
|
97
|
+
roles.each do |key, role|
|
98
|
+
add_role(key, role)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
alias_method :can_be_a?, :role_defined?
|
103
|
+
end
|
71
104
|
end
|
data/lib/grognard/version.rb
CHANGED
data/spec/guard_spec.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Grognard do
|
4
|
+
describe "guard" do
|
5
|
+
class OldGuard
|
6
|
+
def self.allowed_to(object, subject)
|
7
|
+
abilities = []
|
8
|
+
abilities << :eat if object == "man" && subject == "chicken"
|
9
|
+
abilities
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class YoungGuard
|
14
|
+
def self.allowed_to(object, subject)
|
15
|
+
[:serve, :cook]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "can?" do
|
20
|
+
before do
|
21
|
+
@guard = Grognard::Guard.new OldGuard, YoungGuard
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should respond to its roles" do
|
25
|
+
@guard.should respond_to(:be_a_old_guard)
|
26
|
+
@guard.should respond_to(:be_a_young_guard)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should respond if is old guard" do
|
30
|
+
@guard.be_a_old_guard
|
31
|
+
@guard.should respond_to(:can_eat?)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should respond if is a young guard" do
|
35
|
+
@guard.be_a_young_guard
|
36
|
+
@guard.should respond_to(:can_serve?)
|
37
|
+
@guard.should respond_to(:can_cook?)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "be a old guard" do
|
41
|
+
@guard.be_a_old_guard
|
42
|
+
@guard.can_eat?("man", "chicken").should be_true
|
43
|
+
@guard.can_eat?("man", "cat").should be_false
|
44
|
+
@guard.can_love?("soldier", "princess").should be_false
|
45
|
+
end
|
46
|
+
|
47
|
+
it "be a young guard" do
|
48
|
+
@guard.be_a_young_guard
|
49
|
+
@guard.can_cook?(:everyone, :everthing).should be_true
|
50
|
+
@guard.can_serve?(:everyone, :everthing).should be_true
|
51
|
+
@guard.can_rock?(:everyone, :everthing).should be_false
|
52
|
+
end
|
53
|
+
|
54
|
+
it "can delete role" do
|
55
|
+
@guard.remove_role :old_guard
|
56
|
+
@guard.can_be_a_guard?.should be_false
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grognard
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jingkai He
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 2.14.1
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard-rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 4.0.4
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 4.0.4
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rubocop
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -92,6 +106,7 @@ files:
|
|
92
106
|
- .rubocop.yml
|
93
107
|
- Gemfile
|
94
108
|
- Gemfile.lock
|
109
|
+
- Guardfile
|
95
110
|
- LICENSE
|
96
111
|
- README.md
|
97
112
|
- Rakefile
|
@@ -100,7 +115,7 @@ files:
|
|
100
115
|
- lib/grognard/exceptions.rb
|
101
116
|
- lib/grognard/guard.rb
|
102
117
|
- lib/grognard/version.rb
|
103
|
-
- spec/
|
118
|
+
- spec/guard_spec.rb
|
104
119
|
- spec/spec_helper.rb
|
105
120
|
homepage: https://github.com/jaxi/grognard
|
106
121
|
licenses:
|
@@ -127,6 +142,5 @@ signing_key:
|
|
127
142
|
specification_version: 4
|
128
143
|
summary: A lightweight authorization library, just like cancan
|
129
144
|
test_files:
|
130
|
-
- spec/
|
145
|
+
- spec/guard_spec.rb
|
131
146
|
- spec/spec_helper.rb
|
132
|
-
has_rdoc:
|
data/spec/grognard_spec.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Grognard do
|
4
|
-
describe "guard" do
|
5
|
-
class OldGuard
|
6
|
-
def self.allowed_to(object, subject)
|
7
|
-
abilities = []
|
8
|
-
abilities << :eat if object == "man" && subject == "chicken"
|
9
|
-
abilities
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
describe "can?" do
|
14
|
-
before do
|
15
|
-
@guard = Grognard::Guard.new
|
16
|
-
@guard << OldGuard
|
17
|
-
end
|
18
|
-
|
19
|
-
it "should respond to guard" do
|
20
|
-
@guard.should respond_to(:be_a_old_guard)
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should respond to eat" do
|
24
|
-
@guard.be_a_old_guard.should respond_to(:can_eat?)
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should can do" do
|
28
|
-
# puts @guard.be_a_guard
|
29
|
-
@guard.be_a_old_guard.can_eat?("man", "chicken").should be_true
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should cannot do" do
|
33
|
-
@guard.be_a_old_guard.can_eat?("man", "cat").should be_false
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|