<commit_before>require 'puppet/util/execution'

Puppet::Type.type(:ruby_gem).provide(:rubygems) do
  include Puppet::Util::Execution
  desc ""

  def self.ruby_versions
    Dir["/opt/rubies/*"].map do |ruby|
      File.basename(ruby)
    end
  end

  def ruby_versions
    self.class.ruby_versions
  end

  def self.gemlist
    return @gemlist if defined?(@gemlist)

    mapping = Hash.new

    Dir["/opt/rubies/*"].each do |ruby|
      v = File.basename(ruby)
      mapping[v] = Array.new

      Dir["#{ruby}/lib/ruby/gems/*/gems/*"].each do |g|
        mapping[v] << File.basename(g)
      end
    end

    @gemlist = mapping
  end

  def self.instances
    return @instances if defined?(@instances)

    all_gems = Array.new

    gemlist.each do |r, gems|

      gems.each do |g|

        gem_name, _, gem_version = g.rpartition("-")

        all_gems << new({
          :name         => "#{gem_name} for #{r}",
          :gem          => gem_name,
          :ensure       => :installed,
          :version      => gem_version,
          :ruby_version => r,
          :provider     => :rubygems,
        })
      end
    end

    @instances = all_gems
  end

  def gemlist
    self.class.gemlist
  end

  def instances
    self.class.instances
  end

  def query
    gems = Array.new

    if @resource[:ruby_version] == "*"
      gems = ruby_versions.map do |ruby|
        gemlist[ruby].map do |g|
          gem_name, _, gem_version = g.rpartition("-")
          {
            :gem          => gem_name,
            :version      => gem_version,
            :ruby_version => ruby,
          }
        end
      end.flatten
    else
      if gemlist.has_key?(@resource[:ruby_version])
        gems = gemlist[@resource[:ruby_version]].map { |g|
          gem_name, _, gem_version = g.rpartition("-")
          {
            :gem          => gem_name,
            :version      => gem_version,
            :ruby_version => @resource[:ruby_version],
          }
        }
      end
    end

    if @resource[:ruby_version] = "*"

      ruby_versions.each do |ruby|
        inst = gems.detect { |g|
          g[:gem] == @resource[:gem] \
            && requirement.satisfied_by?(version(g[:version])) \
            && g[:ruby_version] == ruby
        }

        unless inst
          return {
            :ensure       => :absent,
            :name         => "#{@resource[:gem]} for #{@resource[:ruby_version]}",
            :gem          => @resource[:gem],
            :ruby_version => @resource[:ruby_version],
          }
        end
      end

      {
        :name         => "#{inst[:gem]} for all rubies",
        :gem          => inst[:gem],
        :ensure       => :present,
        :version      => inst[:version],
        :ruby_version => "*",
      }
    else
      if inst = gems.detect { |g|
          g[:gem] == @resource[:gem] && requirement.satisfied_by?(version(g[:version]))
        }

        {<commit_msg>Assignment is not comparison lol<commit_after>require 'puppet/util/execution'

Puppet::Type.type(:ruby_gem).provide(:rubygems) do
  include Puppet::Util::Execution
  desc ""

  def self.ruby_versions
    Dir["/opt/rubies/*"].map do |ruby|
      File.basename(ruby)
    end
  end

  def ruby_versions
    self.class.ruby_versions
  end

  def self.gemlist
    return @gemlist if defined?(@gemlist)

    mapping = Hash.new

    Dir["/opt/rubies/*"].each do |ruby|
      v = File.basename(ruby)
      mapping[v] = Array.new

      Dir["#{ruby}/lib/ruby/gems/*/gems/*"].each do |g|
        mapping[v] << File.basename(g)
      end
    end

    @gemlist = mapping
  end

  def self.instances
    return @instances if defined?(@instances)

    all_gems = Array.new

    gemlist.each do |r, gems|

      gems.each do |g|

        gem_name, _, gem_version = g.rpartition("-")

        all_gems << new({
          :name         => "#{gem_name} for #{r}",
          :gem          => gem_name,
          :ensure       => :installed,
          :version      => gem_version,
          :ruby_version => r,
          :provider     => :rubygems,
        })
      end
    end

    @instances = all_gems
  end

  def gemlist
    self.class.gemlist
  end

  def instances
    self.class.instances
  end

  def query
    gems = Array.new

    if @resource[:ruby_version] == "*"
      gems = ruby_versions.map do |ruby|
        gemlist[ruby].map do |g|
          gem_name, _, gem_version = g.rpartition("-")
          {
            :gem          => gem_name,
            :version      => gem_version,
            :ruby_version => ruby,
          }
        end
      end.flatten
    else
      if gemlist.has_key?(@resource[:ruby_version])
        gems = gemlist[@resource[:ruby_version]].map { |g|
          gem_name, _, gem_version = g.rpartition("-")
          {
            :gem          => gem_name,
            :version      => gem_version,
            :ruby_version => @resource[:ruby_version],
          }
        }
      end
    end

    if @resource[:ruby_version] == "*"

      ruby_versions.each do |ruby|
        inst = gems.detect { |g|
          g[:gem] == @resource[:gem] \
            && requirement.satisfied_by?(version(g[:version])) \
            && g[:ruby_version] == ruby
        }

        unless inst
          return {
            :ensure       => :absent,
            :name         => "#{@resource[:gem]} for #{@resource[:ruby_version]}",
            :gem          => @resource[:gem],
            :ruby_version => @resource[:ruby_version],
          }
        end
      end

      {
        :name         => "#{inst[:gem]} for all rubies",
        :gem          => inst[:gem],
        :ensure       => :present,
        :version      => inst[:version],
        :ruby_version => "*",
      }
    else
      if inst = gems.detect { |g|
          g[:gem] == @resource[:gem] && requirement.satisfied_by?(version(g[:version]))
        }

        {
{'fraction_non_alphanumeric': 0.1401089547156963, 'fraction_numerical': 0.0, 'mean_word_length': 1.652370203160271, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 45, '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': '2636581', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1807, 'n_words': 364}