<commit_before>module Trailblazer
  class NotAuthorizedError < RuntimeError
  end

  # Adds #evaluate_policy to #setup!, and ::policy.
  module Operation::Policy
    require "trailblazer/operation/policy/guard"

    def self.included(includer)
      includer.extend DSL
    end

    module DSL
      def self.extended(extender)
        extender.inheritable_attr :policy_config
        extender.policy_config = Guard::Permission.new { true } # return true per default.
      end

      def policy(*args, &block)
        self.policy_config = permission_class.new(*args, &block)
      end

      def permission_class
        Permission
      end
    end

    attr_reader :policy

  private
    module Setup
      def setup!(params)
        super
        evaluate_policy(params)
      end
    end
    include Setup


    private
    def evaluate_policy(params)
      user = params[:current_user]

      @policy = self.class.policy_config.(user, model, @policy) do |policy, action|
        raise policy_exception(policy, action, model)
      end
    end

    def policy_exception(policy, action, model)
      NotAuthorizedError.new(query: action, record: model, policy: policy)
    end

    # Encapsulate building the Policy object and calling the defined query action.
    # This assumes the policy class is "pundit-style", as in Policy.new(user, model).edit?.
    class Permission
      def initialize(policy_class, action)
        @policy_class, @action = policy_class, action
      end
<commit_msg>remove double private<commit_after>module Trailblazer
  class NotAuthorizedError < RuntimeError
  end

  # Adds #evaluate_policy to #setup!, and ::policy.
  module Operation::Policy
    require "trailblazer/operation/policy/guard"

    def self.included(includer)
      includer.extend DSL
    end

    module DSL
      def self.extended(extender)
        extender.inheritable_attr :policy_config
        extender.policy_config = Guard::Permission.new { true } # return true per default.
      end

      def policy(*args, &block)
        self.policy_config = permission_class.new(*args, &block)
      end

      def permission_class
        Permission
      end
    end

    attr_reader :policy

  private
    module Setup
      def setup!(params)
        super
        evaluate_policy(params)
      end
    end
    include Setup

    def evaluate_policy(params)
      user = params[:current_user]

      @policy = self.class.policy_config.(user, model, @policy) do |policy, action|
        raise policy_exception(policy, action, model)
      end
    end

    def policy_exception(policy, action, model)
      NotAuthorizedError.new(query: action, record: model, policy: policy)
    end

    # Encapsulate building the Policy object and calling the defined query action.
    # This assumes the policy class is "pundit-style", as in Policy.new(user, model).edit?.
    class Permission
      def initialize(policy_class, action)
        @policy_class, @action = policy_class, action
      end

{'fraction_non_alphanumeric': 0.07561057209769154, 'fraction_numerical': 0.0, 'mean_word_length': 2.761006289308176, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2869627', 'n_tokens_mistral': 861, 'n_tokens_neox': 819, 'n_words': 243}