Class: Doorman::Rule
- Inherits:
-
Object
- Object
- Doorman::Rule
- Defined in:
- lib/rails_doorman/rule.rb
Instance Attribute Summary collapse
-
#limits ⇒ Object
Returns the value of attribute limits.
-
#method ⇒ Object
Returns the value of attribute method.
-
#type ⇒ Object
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
- .extract_limits(h) ⇒ Object
- .from_block(type, opts = nil, &block) ⇒ Object
- .from_hash(type, opts) ⇒ Object
Instance Method Summary collapse
- #deny? ⇒ Boolean
- #evaluate?(action_name) ⇒ Boolean
-
#initialize(type) ⇒ Rule
constructor
A new instance of Rule.
Constructor Details
#initialize(type) ⇒ Rule
Returns a new instance of Rule.
45 46 47 |
# File 'lib/rails_doorman/rule.rb', line 45 def initialize(type) @type, @limits = type, {} end |
Instance Attribute Details
#limits ⇒ Object
Returns the value of attribute limits.
43 44 45 |
# File 'lib/rails_doorman/rule.rb', line 43 def limits @limits end |
#method ⇒ Object
Returns the value of attribute method.
43 44 45 |
# File 'lib/rails_doorman/rule.rb', line 43 def method @method end |
#type ⇒ Object
Returns the value of attribute type.
43 44 45 |
# File 'lib/rails_doorman/rule.rb', line 43 def type @type end |
#value ⇒ Object
Returns the value of attribute value.
43 44 45 |
# File 'lib/rails_doorman/rule.rb', line 43 def value @value end |
Class Method Details
.extract_limits(h) ⇒ Object
37 38 39 40 41 |
# File 'lib/rails_doorman/rule.rb', line 37 def self.extract_limits(h) h.slice(:only, :exclude).inject({}) do |limits, kv| limits.merge!(kv.first => Array(kv.last).map {|limit| limit.to_sym }) end end |
.from_block(type, opts = nil, &block) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rails_doorman/rule.rb', line 25 def self.from_block(type, opts = nil, &block) unless block.arity == 1 raise InvalidRule.new(type, opts, block) end opts ||= {} rule = new(type) rule.method = :block rule.value = block rule.limits = extract_limits(opts) rule end |
.from_hash(type, opts) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rails_doorman/rule.rb', line 10 def self.from_hash(type, opts) rule = new(type) h = opts.except(:only, :exclude) if h.size > 1 raise InvalidRule.new(type, opts) end rule.method = h.keys.first.to_sym unless Doorman.supported_method?(rule.method) raise InvalidRule.new(type, opts) end rule.value = h.values.first rule.limits = extract_limits(opts) rule end |
Instance Method Details
#deny? ⇒ Boolean
49 50 51 |
# File 'lib/rails_doorman/rule.rb', line 49 def deny? type == :deny end |
#evaluate?(action_name) ⇒ Boolean
53 54 55 56 |
# File 'lib/rails_doorman/rule.rb', line 53 def evaluate?(action_name) (!limits.key?(:only) || limits[:only].include?(action_name.to_sym)) && (!limits.key?(:exclude) || !limits[:exclude].include?(action_name.to_sym)) end |