grognard 0.0.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.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.rubocop.yml +5 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +53 -0
- data/LICENSE +20 -0
- data/README.md +51 -0
- data/Rakefile +1 -0
- data/grognard.gemspec +26 -0
- data/lib/grognard.rb +4 -0
- data/lib/grognard/exceptions.rb +16 -0
- data/lib/grognard/guard.rb +71 -0
- data/lib/grognard/version.rb +3 -0
- data/spec/grognard_spec.rb +37 -0
- data/spec/spec_helper.rb +9 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e6be4df2e711acaf1e7918b261c4326c514acc7c
|
4
|
+
data.tar.gz: 998ff4de07b596219d0bff1cadb06204296bf80d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4ff290b5e02f2b554c06541ee09846c28a1e31cb8715b3e858b90127caa606a514d85aa67215338069016feaf1b30b8a964374efe8222c7f88eca661ee8d418a
|
7
|
+
data.tar.gz: 75d69370f13fe4a2174c07453d46dddbe0ab2e0e0cc827ea5a191f2b2508cea63ed470d5e62145bf350b16813a07e123c5bda9fda7912853b2640d16b594f7a6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
grognard (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activesupport (4.0.1)
|
10
|
+
i18n (~> 0.6, >= 0.6.4)
|
11
|
+
minitest (~> 4.2)
|
12
|
+
multi_json (~> 1.3)
|
13
|
+
thread_safe (~> 0.1)
|
14
|
+
tzinfo (~> 0.3.37)
|
15
|
+
ast (1.1.0)
|
16
|
+
atomic (1.1.14)
|
17
|
+
diff-lcs (1.2.5)
|
18
|
+
i18n (0.6.5)
|
19
|
+
minitest (4.7.5)
|
20
|
+
multi_json (1.8.2)
|
21
|
+
parser (2.0.0)
|
22
|
+
ast (~> 1.1)
|
23
|
+
slop (~> 3.4, >= 3.4.5)
|
24
|
+
powerpack (0.0.9)
|
25
|
+
rainbow (1.1.4)
|
26
|
+
rake (10.1.0)
|
27
|
+
rspec (2.14.1)
|
28
|
+
rspec-core (~> 2.14.0)
|
29
|
+
rspec-expectations (~> 2.14.0)
|
30
|
+
rspec-mocks (~> 2.14.0)
|
31
|
+
rspec-core (2.14.7)
|
32
|
+
rspec-expectations (2.14.4)
|
33
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
34
|
+
rspec-mocks (2.14.4)
|
35
|
+
rubocop (0.15.0)
|
36
|
+
parser (~> 2.0)
|
37
|
+
powerpack (~> 0.0.6)
|
38
|
+
rainbow (>= 1.1.4)
|
39
|
+
slop (3.4.7)
|
40
|
+
thread_safe (0.1.3)
|
41
|
+
atomic
|
42
|
+
tzinfo (0.3.38)
|
43
|
+
|
44
|
+
PLATFORMS
|
45
|
+
ruby
|
46
|
+
|
47
|
+
DEPENDENCIES
|
48
|
+
activesupport (~> 4.0.1)
|
49
|
+
bundler (~> 1.3)
|
50
|
+
grognard!
|
51
|
+
rake
|
52
|
+
rspec (~> 2.14.1)
|
53
|
+
rubocop (~> 0.15.0)
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 jaxi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
grognard
|
2
|
+
========
|
3
|
+
|
4
|
+
A lightweight authorization library, just like cancan
|
5
|
+
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
|
11
|
+
# person.rb
|
12
|
+
|
13
|
+
class Customer
|
14
|
+
def self.allowed_to(customer, food)
|
15
|
+
abilities = []
|
16
|
+
abilities << :drink_scotch if customer.has_paid_for_scotch and food.type == "scotch"
|
17
|
+
abilities << :grumble unless food.is_ok
|
18
|
+
|
19
|
+
ability
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# attendant.rb
|
24
|
+
|
25
|
+
class Attendant
|
26
|
+
def self.allowed_to(attendant, food)
|
27
|
+
abilities = []
|
28
|
+
abilities << :free_deliver if food.kind_of? Food
|
29
|
+
|
30
|
+
abilities
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
# meanwhile in the console
|
36
|
+
|
37
|
+
guard = Grognard.new
|
38
|
+
|
39
|
+
guard << Customer # learn the abilities of a customer
|
40
|
+
guard << Attendant # learn the abilities of an attendant
|
41
|
+
|
42
|
+
guard.be_a_customer! # to be a customer
|
43
|
+
guard.allowed_to_drink_scotch? @drunker, @scotch # => true
|
44
|
+
guard.allowed_to_grumble? :everyone, @shit # => true
|
45
|
+
guard.allowed_to_grumble? @drunker, @peri_peri # => false
|
46
|
+
|
47
|
+
guard.be_a_attendant! # to be a attendant
|
48
|
+
guard.allowed_to_free_deliver @attendant, @mussels # => true
|
49
|
+
guard.allowed_to_free_deliver @attendant, @perfume # => false
|
50
|
+
|
51
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/grognard.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'grognard/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "grognard"
|
8
|
+
spec.version = Grognard::VERSION
|
9
|
+
spec.authors = ["Jingkai He"]
|
10
|
+
spec.email = ["jaxihe@gmail.com"]
|
11
|
+
spec.description = %q{A lightweight authorization library, just like cancan}
|
12
|
+
spec.summary = spec.description
|
13
|
+
spec.homepage = "https://github.com/jaxi/grognard"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.14.1"
|
24
|
+
spec.add_development_dependency "rubocop", "~> 0.15.0"
|
25
|
+
spec.add_development_dependency "activesupport", "~> 4.0.1"
|
26
|
+
end
|
data/lib/grognard.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module Grognard
|
2
|
+
class NoObjectError < StandardError; end
|
3
|
+
class NoSubjectError < StandardError; end
|
4
|
+
|
5
|
+
class AbilitiesNotDefinedError < StandardError
|
6
|
+
def message
|
7
|
+
"ability has not been defined"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class RoleNotDefinedError < StandardError
|
12
|
+
def message
|
13
|
+
"role has not been defined"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require "grognard/version"
|
2
|
+
require "grognard/exceptions"
|
3
|
+
require "active_support/core_ext/string"
|
4
|
+
|
5
|
+
module Grognard
|
6
|
+
class Guard
|
7
|
+
attr_reader :can_dos, :current_abilities
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@can_dos = {}
|
11
|
+
@current_abilities = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def be_a(name)
|
15
|
+
@current_abilities = @can_dos[name.to_sym] if role_defined? name
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_role(name, object)
|
20
|
+
@can_dos[name.to_sym] = object if abilities_defined?(object)
|
21
|
+
end
|
22
|
+
|
23
|
+
def <<(object)
|
24
|
+
add_role(object.name.underscore, object)
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def can?(action, object, subject)
|
29
|
+
@current_abilities.allowed_to(object, subject).include? action
|
30
|
+
end
|
31
|
+
|
32
|
+
def abilities_defined?(object)
|
33
|
+
object.respond_to?(:allowed_to) ||
|
34
|
+
raise_no_such_abilites
|
35
|
+
end
|
36
|
+
|
37
|
+
def role_defined?(name)
|
38
|
+
@can_dos.key?(name.to_sym) ||
|
39
|
+
raise_no_such_role
|
40
|
+
end
|
41
|
+
|
42
|
+
def method_missing(meth, *args, &block)
|
43
|
+
meth = meth.to_s
|
44
|
+
case
|
45
|
+
when meth.match(/^be_a_(\w+)/)
|
46
|
+
be_a($1, *args, &block)
|
47
|
+
when meth.match(/^can_(\w+)?/)
|
48
|
+
can?($1.to_sym, *args, &block)
|
49
|
+
else
|
50
|
+
super
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def respond_to?(meth, include_private = false)
|
55
|
+
meth.to_s.start_with?("be_a_") ||
|
56
|
+
meth.to_s.start_with?("can_") ||
|
57
|
+
super
|
58
|
+
end
|
59
|
+
|
60
|
+
protected
|
61
|
+
|
62
|
+
def raise_no_such_abilites
|
63
|
+
fail AbilitiesNotDefinedError.new
|
64
|
+
end
|
65
|
+
|
66
|
+
def raise_no_such_role
|
67
|
+
fail RoleNotDefinedError.new
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,37 @@
|
|
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
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grognard
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jingkai He
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.14.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.14.1
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.15.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.15.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 4.0.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 4.0.1
|
83
|
+
description: A lightweight authorization library, just like cancan
|
84
|
+
email:
|
85
|
+
- jaxihe@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
|
+
- .rubocop.yml
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- LICENSE
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- grognard.gemspec
|
99
|
+
- lib/grognard.rb
|
100
|
+
- lib/grognard/exceptions.rb
|
101
|
+
- lib/grognard/guard.rb
|
102
|
+
- lib/grognard/version.rb
|
103
|
+
- spec/grognard_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
homepage: https://github.com/jaxi/grognard
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.0.6
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: A lightweight authorization library, just like cancan
|
129
|
+
test_files:
|
130
|
+
- spec/grognard_spec.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
has_rdoc:
|