Created
January 12, 2012 20:01
-
-
Save dariocravero/1602762 to your computer and use it in GitHub Desktop.
if_lambda support for declarative authorization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Authorization | |
module Reader | |
class AuthorizationRulesReader | |
def if_lambda (lambda_fn) | |
raise DSLError, "if_lambda only in has_permission blocks" if @current_rule.nil? | |
@current_rule.append_attribute AttributeLambda.new(lambda_fn) | |
end | |
end | |
end | |
class AttributeLambda | |
# lambda_fn of form | |
# ->() { true } | |
def initialize (lambda_fn) | |
@lambda_fn = lambda_fn | |
end | |
def initialize_copy (from) | |
@lambda_fn = @lambda_fn.clone | |
end | |
def validate? (attr_validator, object = nil, hash = nil) | |
object ||= attr_validator.object | |
@lambda_fn.parameters.empty? ? @lambda_fn.call : @lambda_fn.call(object) | |
end | |
# resolves all the values in condition_hash | |
def obligation (attr_validator, hash = nil) | |
{} | |
end | |
def to_long_s (hash = nil) | |
"if_lambda #{to_long_s(@lambda_fn).inspect}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment