six 0.0.3 → 0.1.0

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 +120 -122
  2. metadata +2 -2
data/lib/six.rb CHANGED
@@ -11,144 +11,142 @@ class Six
11
11
  end
12
12
  end
13
13
 
14
- class << self
15
- attr_accessor :rules_packs
16
- attr_accessor :current_rule_pack
14
+ attr_accessor :rules_packs
15
+ attr_accessor :current_rule_pack
17
16
 
18
- def rules_packs
19
- @rules_packs ||= {}
20
- end
17
+ def rules_packs
18
+ @rules_packs ||= {}
19
+ end
21
20
 
22
- # Set current pack from stored packs by key
23
- #
24
- # == Parameters:
25
- # name::
26
- # A Symbol declaring the key name of stored pack
27
- #
28
- # == Returns:
29
- # self or false
30
- #
31
- def use(name)
32
- if pack_exist?(name)
33
- @current_rule_pack = name.to_sym
34
- self
35
- end
21
+ # Set current pack from stored packs by key
22
+ #
23
+ # == Parameters:
24
+ # name::
25
+ # A Symbol declaring the key name of stored pack
26
+ #
27
+ # == Returns:
28
+ # self or false
29
+ #
30
+ def use(name)
31
+ if pack_exist?(name)
32
+ @current_rule_pack = name.to_sym
33
+ self
36
34
  end
35
+ end
37
36
 
38
- # Same as use but raise exception if no pack found
39
- def use!(name)
40
- use(name) ? self : raise_no_such_pack
41
- end
37
+ # Same as use but raise exception if no pack found
38
+ def use!(name)
39
+ use(name) ? self : raise_no_such_pack
40
+ end
42
41
 
43
- # Add pack to authorization class
44
- #
45
- # == Parameters:
46
- # name::
47
- # A Symbol declaring the key name of stored pack
48
- # pack::
49
- # Any kind of object responding to allowed method
50
- #
51
- # == Returns:
52
- # true or false
53
- #
54
- def add_pack(name, pack)
55
- rules_packs[name.to_sym] = pack if valid_rules_object?(pack)
56
- end
42
+ # Add pack to authorization class
43
+ #
44
+ # == Parameters:
45
+ # name::
46
+ # A Symbol declaring the key name of stored pack
47
+ # pack::
48
+ # Any kind of object responding to allowed method
49
+ #
50
+ # == Returns:
51
+ # true or false
52
+ #
53
+ def add_pack(name, pack)
54
+ rules_packs[name.to_sym] = pack if valid_rules_object?(pack)
55
+ end
57
56
 
58
- # Same as add_pack but raise exception if pack is invalid
59
- def add_pack!(name, pack)
60
- add_pack(name, pack) || raise_incorrect_pack_object
61
- end
57
+ # Same as add_pack but raise exception if pack is invalid
58
+ def add_pack!(name, pack)
59
+ add_pack(name, pack) || raise_incorrect_pack_object
60
+ end
62
61
 
63
- # Remove pack from authorization class
64
- #
65
- # == Parameters:
66
- # name::
67
- # A Symbol declaring the key name of stored pack
68
- #
69
- # == Returns:
70
- # true or false
71
- #
72
- def remove_pack(name)
73
- if pack_exist?(name)
74
- @current_rule_pack = nil if rules_packs[name.to_sym] == @current_rule_pack
75
- rules_packs.delete(name.to_sym)
76
- end
62
+ # Remove pack from authorization class
63
+ #
64
+ # == Parameters:
65
+ # name::
66
+ # A Symbol declaring the key name of stored pack
67
+ #
68
+ # == Returns:
69
+ # true or false
70
+ #
71
+ def remove_pack(name)
72
+ if pack_exist?(name)
73
+ @current_rule_pack = nil if rules_packs[name.to_sym] == @current_rule_pack
74
+ rules_packs.delete(name.to_sym)
77
75
  end
76
+ end
78
77
 
79
- # Same as remove_pack but raise exception if pack wasnt found
80
- def remove_pack!(name)
81
- remove_pack(name) || raise_no_such_pack
82
- end
78
+ # Same as remove_pack but raise exception if pack wasnt found
79
+ def remove_pack!(name)
80
+ remove_pack(name) || raise_no_such_pack
81
+ end
83
82
 
84
- # Check if object for rule pack is valid
85
- #
86
- # == Parameters:
87
- # pack::
88
- # Any kind of object responding to allowed method
89
- #
90
- # == Returns:
91
- # true or false
92
- #
93
- def valid_rules_object?(object)
94
- object.respond_to?(:allowed) &&
95
- object.send(:allowed, nil, nil).kind_of?(Array)
96
- rescue
97
- false
98
- end
83
+ # Check if object for rule pack is valid
84
+ #
85
+ # == Parameters:
86
+ # pack::
87
+ # Any kind of object responding to allowed method
88
+ #
89
+ # == Returns:
90
+ # true or false
91
+ #
92
+ def valid_rules_object?(object)
93
+ object.respond_to?(:allowed) &&
94
+ object.send(:allowed, nil, nil).kind_of?(Array)
95
+ rescue
96
+ false
97
+ end
99
98
 
100
- # Check if authorization class has pack with such name
101
- #
102
- # == Parameters:
103
- # name::
104
- # A Symbol declaring the key name of stored pack
105
- #
106
- # == Returns:
107
- # true or false
108
- #
109
- def pack_exist?(name)
110
- rules_packs.has_key?(name.to_sym)
111
- end
99
+ # Check if authorization class has pack with such name
100
+ #
101
+ # == Parameters:
102
+ # name::
103
+ # A Symbol declaring the key name of stored pack
104
+ #
105
+ # == Returns:
106
+ # true or false
107
+ #
108
+ def pack_exist?(name)
109
+ rules_packs.has_key?(name.to_sym)
110
+ end
112
111
 
113
- # Check if authorization class allow access for object to subject
114
- # using selected pack or all stored.
115
- # Basically this method
116
- # 1. send :allowed for every stored object in packs and pass object & subject
117
- # 2. check if any of results include allowed action
118
- #
119
- # == Parameters:
120
- # action::
121
- # Action name to check for access
122
- # object::
123
- # object trying to access resource
124
- # subject::
125
- # resource
126
- #
127
- # == Returns:
128
- # true or false
129
- #
130
- def allowed?(action, object, subject)
131
- if current_rule_pack
132
- rules_packs[current_rule_pack].allowed(object, subject).include?(action)
133
- else
134
- rules_packs.values.map { |rp| rp.allowed(object, subject) }.flatten.include?(action)
135
- end
112
+ # Check if authorization class allow access for object to subject
113
+ # using selected pack or all stored.
114
+ # Basically this method
115
+ # 1. send :allowed for every stored object in packs and pass object & subject
116
+ # 2. check if any of results include allowed action
117
+ #
118
+ # == Parameters:
119
+ # action::
120
+ # Action name to check for access
121
+ # object::
122
+ # object trying to access resource
123
+ # subject::
124
+ # resource
125
+ #
126
+ # == Returns:
127
+ # true or false
128
+ #
129
+ def allowed?(action, object, subject)
130
+ if current_rule_pack
131
+ rules_packs[current_rule_pack].allowed(object, subject).include?(action)
132
+ else
133
+ rules_packs.values.map { |rp| rp.allowed(object, subject) }.flatten.include?(action)
136
134
  end
135
+ end
137
136
 
138
- # Reset current used rule pack so auth class use
139
- # global allowed? for new request
140
- def reset_use
141
- @current_rule_pack = nil
142
- end
137
+ # Reset current used rule pack so auth class use
138
+ # global allowed? for new request
139
+ def reset_use
140
+ @current_rule_pack = nil
141
+ end
143
142
 
144
- protected
143
+ protected
145
144
 
146
- def raise_no_such_pack
147
- raise Six::NoPackError.new
148
- end
145
+ def raise_no_such_pack
146
+ raise Six::NoPackError.new
147
+ end
149
148
 
150
- def raise_incorrect_pack_object
151
- raise Six::InvalidPackPassed.new
152
- end
149
+ def raise_incorrect_pack_object
150
+ raise Six::InvalidPackPassed.new
153
151
  end
154
152
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: six
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-17 00:00:00.000000000Z
12
+ date: 2011-08-20 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Very simple authorization gem
15
15
  email: dmitriy.zaporozhets@gmail.com