Sha256: 8bd92d1634fb671914844f81c1309b21f6cd8eaa9299ac96b23d36d1b183d2e4

Contents?: true

Size: 1.77 KB

Versions: 38

Compression:

Stored size: 1.77 KB

Contents

module Graphiti
  class Scoping::FilterGroupValidator
    VALID_REQUIRED_VALUES = %i[all any]

    def self.raise_unless_filter_group_requirement_valid!(resource, requirement)
      unless VALID_REQUIRED_VALUES.include?(requirement)
        raise Errors::FilterGroupInvalidRequirement.new(
          resource,
          VALID_REQUIRED_VALUES
        )
      end

      true
    end

    def initialize(resource, query_hash)
      @resource = resource
      @query_hash = query_hash
    end

    def raise_unless_filter_group_requirements_met!
      return if grouped_filters.empty?

      case filter_group_requirement
      when :all
        raise_unless_all_requirements_met!
      when :any
        raise_unless_any_requirements_met!
      end

      true
    end

    private

    attr_reader :resource, :query_hash

    def raise_unless_all_requirements_met!
      met = filter_group_names.all? { |filter_name| filter_group_filter_param.key?(filter_name) }

      unless met
        raise Errors::FilterGroupMissingRequiredFilters.new(
          resource,
          filter_group_names,
          filter_group_requirement
        )
      end
    end

    def raise_unless_any_requirements_met!
      met = filter_group_names.any? { |filter_name| filter_group_filter_param.key?(filter_name) }

      unless met
        raise Errors::FilterGroupMissingRequiredFilters.new(
          resource,
          filter_group_names,
          filter_group_requirement
        )
      end
    end

    def filter_group_names
      grouped_filters.fetch(:names, [])
    end

    def filter_group_requirement
      grouped_filters.fetch(:required, :invalid)
    end

    def grouped_filters
      resource.grouped_filters
    end

    def filter_group_filter_param
      query_hash.fetch(:filter, {})
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
graphiti-1.7.6 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.7.5 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.7.4 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.7.3 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.7.2 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.7.1 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.7.0 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.6.4 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.6.3 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.6.2 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.6.1 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.6.0 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.5.3 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.5.2 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.5.1 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.5.0 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.4.0 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.3.9 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.3.8 lib/graphiti/scoping/filter_group_validator.rb
graphiti-1.3.7 lib/graphiti/scoping/filter_group_validator.rb