six 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/six.rb +45 -0
  2. metadata +45 -0
@@ -0,0 +1,45 @@
1
+ require "singleton"
2
+
3
+ module Six
4
+ class Guard
5
+ include Singleton
6
+
7
+ attr_accessor :rules_packs
8
+ attr_accessor :current_rule_pack
9
+
10
+ def initialize
11
+ @rules_packs = {}
12
+ end
13
+
14
+ def use(name)
15
+ @current_rule_pack = rules_packs[name.to_sym] if pack_exist?(name)
16
+ self
17
+ end
18
+
19
+ def add_pack(name, pack)
20
+ rules_packs[name.to_sym] = pack if valid_rules_object?(pack)
21
+ end
22
+
23
+ def remove_pack(name)
24
+ rules_packs.delete(name.to_sym)
25
+ end
26
+
27
+ def valid_rules_object?(object)
28
+ object.respond_to?(:allowed) ||
29
+ raise("Wrong Rule Pack. You must provide 'allowed' method")
30
+ end
31
+
32
+ def pack_exist?(name)
33
+ rules_packs.has_key?(name.to_sym) ||
34
+ raise("No such pack")
35
+ end
36
+
37
+ def allowed?(action, object, subject)
38
+ if current_rule_pack
39
+ current_rule_pack.allowed(object, subject).include?(action)
40
+ else
41
+ rules_packs.values.map { |rp| rp.allowed(object, subject) }.flatten.include?(action)
42
+ end
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: six
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dmitriy Zaporozhets
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-13 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Very simple authorization gem
15
+ email: dmitriy.zaporozhets@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/six.rb
21
+ homepage: https://github.com/randx/six
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.6
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: six
45
+ test_files: []