| Class | Authorization::Attribute |
| In: |
lib/authorization.rb
|
| Parent: | Object |
attr_conditions_hash of form { :object_attribute => [operator, value_block], … } { :object_attribute => { :attr => … } }
# File lib/authorization.rb, line 284
284: def initialize (conditions_hash)
285: @conditions_hash = conditions_hash
286: end
resolves all the values in condition_hash
# File lib/authorization.rb, line 318
318: def obligation (attr_validator, hash = nil)
319: hash = (hash || @conditions_hash).clone
320: hash.each do |attr, value|
321: if value.is_a?(Hash)
322: hash[attr] = obligation(attr_validator, value)
323: elsif value.is_a?(Array) and value.length == 2
324: hash[attr] = [value[0], attr_validator.evaluate(value[1])]
325: else
326: raise AuthorizationError, "Wrong conditions hash format"
327: end
328: end
329: hash
330: end
# File lib/authorization.rb, line 288
288: def validate? (attr_validator, object = nil, hash = nil)
289: object ||= attr_validator.object
290: return false unless object
291:
292: (hash || @conditions_hash).all? do |attr, value|
293: begin
294: attr_value = object.send(attr)
295: rescue ArgumentError, NoMethodError => e
296: raise AuthorizationUsageError, "Error when calling #{attr} on " +
297: "#{object.inspect} for validating attribute: #{e}"
298: end
299: if value.is_a?(Hash)
300: validate?(attr_validator, attr_value, value)
301: elsif value.is_a?(Array) and value.length == 2
302: evaluated = attr_validator.evaluate(value[1])
303: case value[0]
304: when :is
305: attr_value == evaluated
306: when :contains
307: attr_value.include?(evaluated)
308: else
309: raise AuthorizationError, "Unknown operator #{value[0]}"
310: end
311: else
312: raise AuthorizationError, "Wrong conditions hash format"
313: end
314: end
315: end