<commit_before>require 'json'
require 'puppet/util/package'

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmqctl'))
Puppet::Type.type(:rabbitmq_policy).provide(:rabbitmqctl, :parent => Puppet::Provider::Rabbitmqctl) do

  defaultfor :feature => :posix

  # cache policies
  def self.policies(name, vhost)
    @policies = {} unless @policies
    unless @policies[vhost]
      @policies[vhost] = {}
      rabbitmqctl('list_policies', '-q', '-p', vhost).split(/\n/).each do |line|
        # rabbitmq<3.2 does not support the applyto field
        # 1 2      3?  4  5                                            6
        # / ha-all all .* {"ha-mode":"all","ha-sync-mode":"automatic"} 0
        if line =~ /^(\S+)\s+(\S+)\s+(all|exchanges|queues)?\s*(\S+)\s+(\S+)\s+(\d+)$/
          applyto = $3 || 'all'
          @policies[vhost][$2] = {
            :applyto    => applyto,
            :pattern    => $4,
            :definition => JSON.parse($5),
            :priority   => $6}
        else
          raise Puppet::Error, "cannot parse line from list_policies:#{line}"
        end
      end
    end
    @policies[vhost][name]
  end

  def policies(name, vhost)
    self.class.policies(vhost, name)
  end

  def should_policy
    if @should_policy
      @should_policy
    else
      @should_policy = resource[:name].rpartition('@').first
    end
  end

  def should_vhost
    if @should_vhost
      @should_vhost
    else
      @should_vhost = resource[:name].rpartition('@').last
    end
  end

  def create
    set_policy
  end

  def destroy
    rabbitmqctl('clear_policy', '-p', should_vhost, should_policy)
  end

  def exists?
    policies(should_vhost, should_policy)
  end

  def pattern
    policies(should_vhost, should_policy)[:pattern]
  end

  def pattern=(pattern)
    set_policy<commit_msg>Idiomatic syntax for should_policy and should_vhost<commit_after>require 'json'
require 'puppet/util/package'

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmqctl'))
Puppet::Type.type(:rabbitmq_policy).provide(:rabbitmqctl, :parent => Puppet::Provider::Rabbitmqctl) do

  defaultfor :feature => :posix

  # cache policies
  def self.policies(name, vhost)
    @policies = {} unless @policies
    unless @policies[vhost]
      @policies[vhost] = {}
      rabbitmqctl('list_policies', '-q', '-p', vhost).split(/\n/).each do |line|
        # rabbitmq<3.2 does not support the applyto field
        # 1 2      3?  4  5                                            6
        # / ha-all all .* {"ha-mode":"all","ha-sync-mode":"automatic"} 0
        if line =~ /^(\S+)\s+(\S+)\s+(all|exchanges|queues)?\s*(\S+)\s+(\S+)\s+(\d+)$/
          applyto = $3 || 'all'
          @policies[vhost][$2] = {
            :applyto    => applyto,
            :pattern    => $4,
            :definition => JSON.parse($5),
            :priority   => $6}
        else
          raise Puppet::Error, "cannot parse line from list_policies:#{line}"
        end
      end
    end
    @policies[vhost][name]
  end

  def policies(name, vhost)
    self.class.policies(vhost, name)
  end

  def should_policy
    @should_policy ||= resource[:name].rpartition('@').first
  end

  def should_vhost
    @should_vhost ||= resource[:name].rpartition('@').last
  end

  def create
    set_policy
  end

  def destroy
    rabbitmqctl('clear_policy', '-p', should_vhost, should_policy)
  end

  def exists?
    policies(should_vhost, should_policy)
  end

  def pattern
    policies(should_vhost, should_policy)[:pattern]
  end

  def pattern=(pattern)
    set_policy
{'fraction_non_alphanumeric': 0.14804469273743018, 'fraction_numerical': 0.00782122905027933, 'mean_word_length': 2.51078431372549, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1210377', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1222, 'n_words': 248}