lib/ddr/auth/roles/role_set_query.rb in ddr-models-3.0.0.alpha.4 vs lib/ddr/auth/roles/role_set_query.rb in ddr-models-3.0.0.beta.1

- old
+ new

@@ -6,20 +6,19 @@ # @api private # class RoleSetQuery include Enumerable - attr_reader :role_set + attr_reader :criteria, :role_set + delegate :each, :agents, :permissions, :empty?, to: :result + def initialize(role_set) @role_set = role_set + @criteria = {} end - def criteria - @criteria ||= {} - end - def where(conditions={}) criteria.merge!(conditions) self end @@ -43,30 +42,19 @@ def role_type(r) where(role_type: r) end alias_method :type, :role_type - def each(&block) - role_set.select { |role| matches_all?(role) }.each(&block) + def merge(other_query) + where(other_query.criteria) end - # Return the list of agents for the Roles matching the criteria. - # @return [Array] the agents - def agents - map { |role| role.agent.first } + def result + matching_roles = role_set.select { |role| matches_all?(role) } + RoleSet.new(roles: matching_roles) end - # Return a list of the permissions granted to the Roles matching the criteria. - # @return [Array<Symbol>] the permissions - def permissions - map(&:permissions).flatten.uniq - end - - def detach - DetachedRoleSet.new(self) - end - private # Return a list of the permissions granted to any of the agents in the given scope def permissions_for_agents_in_scope(agents, scope) agent(agents).scope(scope).permissions @@ -75,10 +63,10 @@ def matches_all?(role) criteria.all? { |key, value| matches_one?(role, key, value) } end def matches_one?(role, key, value) - Array(value).include?(role.send(key).first) + Array(value).include? role.send(key) end end end end