Sha256: 60e16fc7385295bbae3dc8069c9bdc79782c46e348a76ed54e62badc0269a25e

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

require "delegate"

module Ddr
  module Auth
    module Roles
      class Query < SimpleDelegator

        def initialize(role_set)
          super(role_set.to_a)
        end

        def where(criteria)
          matching = select do |role|
            criteria.all? do |key, value|
              send("any_#{key}?", value, role)
            end
          end
          __setobj__(matching)
          self
        end

        private

        def any_scope?(scopes, role)
          Array(scopes).any? { |scope| scope == role.scope.first }
        end

        def any_type?(types, role)
          Array(types).any? { |t| t == role.role_type }
        end

        def any_person?(persons, role)
          role.person_agent? ? any_agent?(persons, role) : false
        end

        def any_group?(groups, role)
          role.group_agent? ? any_agent?(groups, role) : false
        end

        def any_agent?(agents, role)
          unless agents.is_a?(Array)
            agents = [agents]
          end
          agents.any? do |agent|
            agent == (agent.is_a?(Ddr::Auth::Agent) ? role.get_agent : role.agent_name)
          end
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ddr-models-1.13.1 lib/ddr/auth/roles/query.rb
ddr-models-1.13.0 lib/ddr/auth/roles/query.rb