lib/declarative_authorization/authorization.rb
# may return an array of obligations to be OR'ed
may return an array of obligations to be OR'ed
may return an array of obligations to be OR ' ed
def obligation (attr_validator, hash_or_attr = nil)
      hash_or_attr ||= @attr_hash
      case hash_or_attr
      when Symbol
        obligations = attr_validator.engine.obligations(@privilege,
                          :context => @context || hash_or_attr.to_s.pluralize.to_sym,
                          :user    => attr_validator.user)
        obligations.collect {|obl| {hash_or_attr => obl} }
      when Hash
        obligations_array_attrs = []
        obligations =
            hash_or_attr.inject({}) do |all, pair|
              attr, sub_hash = pair
              all[attr] = obligation(attr_validator, sub_hash)
              if all[attr].length > 1
                obligations_array_attrs << attr
              else
                all[attr] = all[attr].first
              end
              all
            end
        obligations = [obligations]
        obligations_array_attrs.each do |attr|
          next_array_size = obligations.first[attr].length
          obligations = obligations.collect do |obls|
            (0...next_array_size).collect do |idx|
              obls_wo_array = obls.clone
              obls_wo_array[attr] = obls_wo_array[attr][idx]
              obls_wo_array
            end
          end.flatten
        end
        obligations
      when NilClass
        attr_validator.engine.obligations(@privilege,
            :context => attr_validator.context,
            :user    => attr_validator.user)
      else
        raise AuthorizationError, "Wrong conditions hash format: #{hash_or_attr.inspect}"
      end
    end
def obligation ( attr_validator , hash_or_attr = nil ) hash_or_attr ||= @attr_hash case hash_or_attr when Symbol obligations = attr_validator . engine . obligations ( @privilege , :context => @context || hash_or_attr . to_s . pluralize . to_sym , :user => attr_validator . user ) obligations . collect { | obl | { hash_or_attr => obl } } when Hash obligations_array_attrs = [ ] obligations = hash_or_attr . inject ( { } ) do | all , pair | attr , sub_hash = pair all [ attr ] = obligation ( attr_validator , sub_hash ) if all [ attr ] . length > 1 obligations_array_attrs << attr else all [ attr ] = all [ attr ] . first end all end obligations = [ obligations ] obligations_array_attrs . each do | attr | next_array_size = obligations . first [ attr ] . length obligations = obligations . collect do | obls | ( 0 ... next_array_size ) . collect do | idx | obls_wo_array = obls . clone obls_wo_array [ attr ] = obls_wo_array [ attr ] [ idx ] obls_wo_array end end . flatten end obligations when NilClass attr_validator . engine . obligations ( @privilege , :context => attr_validator . context , :user => attr_validator . user ) else raise AuthorizationError , "Wrong conditions hash format: #{hash_or_attr.inspect}" end end
may return an array of obligations to be OR'ed
may return an array of obligations to be OR ' ed
attr_validator
<not_specific>
hash_or_attr
<not_specific>