six 0.1.3 → 0.2.0

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.
Files changed (2) hide show
  1. data/lib/six.rb +38 -5
  2. metadata +2 -2
data/lib/six.rb CHANGED
@@ -20,6 +20,15 @@ class Six
20
20
  attr_reader :rules_packs
21
21
  attr_reader :current_rule_pack
22
22
 
23
+ # Initialize ability object
24
+ #
25
+ # == Parameters:
26
+ # packs::
27
+ # A Hash or rules to add with initializtion
28
+ #
29
+ # == Returns:
30
+ # self
31
+ #
23
32
  def initialize(packs={})
24
33
  raise InitializeArgumentError.new unless packs.kind_of?(Hash)
25
34
 
@@ -70,6 +79,19 @@ class Six
70
79
  add_pack(name, pack) || raise_incorrect_pack_object
71
80
  end
72
81
 
82
+ # Add pack to authorization class w/o key
83
+ #
84
+ # == Parameters:
85
+ # pack::
86
+ # Any kind of object responding to allowed method
87
+ #
88
+ # == Returns:
89
+ # true or raise exception
90
+ #
91
+ def <<(pack)
92
+ add_pack!(pack.object_id.to_s, pack)
93
+ end
94
+
73
95
  # Remove pack from authorization class
74
96
  #
75
97
  # == Parameters:
@@ -137,11 +159,14 @@ class Six
137
159
  # == Returns:
138
160
  # true or false
139
161
  #
140
- def allowed?(object, action, subject)
141
- if current_rule_pack
142
- rules_packs[current_rule_pack].allowed(object, subject).include?(action)
143
- else
144
- rules_packs.values.map { |rp| rp.allowed(object, subject) }.flatten.include?(action)
162
+ def allowed?(object, actions, subject)
163
+ # if multiple actions passed
164
+ # check all actions to be allowed
165
+ if actions.respond_to?(:each)
166
+ actions.all? { |action| action_included?(object, action, subject) }
167
+ else
168
+ # single action check
169
+ action_included?(object, actions, subject)
145
170
  end
146
171
  end
147
172
 
@@ -153,6 +178,14 @@ class Six
153
178
 
154
179
  protected
155
180
 
181
+ def action_included?(object, action, subject)
182
+ if current_rule_pack
183
+ rules_packs[current_rule_pack].allowed(object, subject).include?(action)
184
+ else
185
+ rules_packs.values.map { |rp| rp.allowed(object, subject) }.flatten.include?(action)
186
+ end
187
+ end
188
+
156
189
  def raise_no_such_pack
157
190
  raise Six::NoPackError.new
158
191
  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.1.3
4
+ version: 0.2.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-09-01 00:00:00.000000000Z
12
+ date: 2011-09-02 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Very simple authorization gem
15
15
  email: dmitriy.zaporozhets@gmail.com