Class: Checkin::Rule
- Inherits:
-
Object
- Object
- Checkin::Rule
- Defined in:
- lib/checkin/rule.rb
Overview
- Author
-
Maurizio Casimirri ([email protected])
- Copyright
-
Copyright © 2012 Maurizio Casimirri
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Instance Attribute Summary collapse
-
#actions_or_attributes ⇒ Object
Returns the value of attribute actions_or_attributes.
-
#kind ⇒ Object
Returns the value of attribute kind.
-
#resources ⇒ Object
Returns the value of attribute resources.
-
#roles ⇒ Object
Returns the value of attribute roles.
-
#scope ⇒ Object
Returns the value of attribute scope.
Instance Method Summary collapse
- #check(subject, action_or_attribute, object_or_resource) ⇒ Object
-
#initialize(kind, *args) ⇒ Rule
constructor
A new instance of Rule.
- #to_s ⇒ Object
Constructor Details
#initialize(kind, *args) ⇒ Rule
Returns a new instance of Rule.
26 27 28 29 30 31 32 33 |
# File 'lib/checkin/rule.rb', line 26 def initialize(kind, *args) opts = args. @kind = kind @actions_or_attributes = (opts[:actions_or_attributes] || []).map {|a| "#{a}".to_sym } @roles = (opts[:roles] || []).map {|r| "#{r}".to_sym } @resources = (opts[:resources] || []).map {|r| "#{r}".singularize.to_sym } @scope = opts[:scope].present? ? opts[:scope].to_sym : nil end |
Instance Attribute Details
#actions_or_attributes ⇒ Object
Returns the value of attribute actions_or_attributes.
24 25 26 |
# File 'lib/checkin/rule.rb', line 24 def actions_or_attributes @actions_or_attributes end |
#kind ⇒ Object
Returns the value of attribute kind.
24 25 26 |
# File 'lib/checkin/rule.rb', line 24 def kind @kind end |
#resources ⇒ Object
Returns the value of attribute resources.
24 25 26 |
# File 'lib/checkin/rule.rb', line 24 def resources @resources end |
#roles ⇒ Object
Returns the value of attribute roles.
24 25 26 |
# File 'lib/checkin/rule.rb', line 24 def roles @roles end |
#scope ⇒ Object
Returns the value of attribute scope.
24 25 26 |
# File 'lib/checkin/rule.rb', line 24 def scope @scope end |
Instance Method Details
#check(subject, action_or_attribute, object_or_resource) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/checkin/rule.rb', line 35 def check(subject, action_or_attribute, object_or_resource) scope = subject.scope action = "#{action}".to_sym object_or_resource = object_or_resource[:for] if object_or_resource.is_a?(Hash) object = ( object_or_resource.is_a?(Symbol) || object_or_resource.is_a?(String)) ? nil : object_or_resource resource = object ? object.class.name.demodulize.underscore.to_sym : "#{object_or_resource}".singularize.to_sym if affect_scope?(scope) && affect_role?(subject, object) && affect_action_or_attribute?(action_or_attribute) && affect_resource?(resource) deny? ? :denied : :allowed else :skip end end |
#to_s ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/checkin/rule.rb', line 50 def to_s formatted_roles = roles.map {|r| ":#{r}" }.join(", ") if roles.any? formatted_actions = ":to => [" + actions_or_attributes.map {|a| ":#{a}" }.join(", ") + "]" if actions_or_attributes.any? formatted_scope = ":scope => :#{scope}" if scope formatted_resources = ":resources => [" + actions_or_attributes.map {|r| ":#{r}" }.join(", ") + "]" if resources.any? args = [formatted_roles, formatted_actions, formatted_resources, formatted_scope].compact.join(", ") "#{kind}(#{args})" end |