<commit_before>require 'json'

module MontageRails
  class Relation < Montage::Query
    attr_reader :klass, :response, :loaded

    alias_method :loaded?, :loaded

    delegate :connection, to: MontageRails
    delegate :cache, to: :klass
    delegate :count, :length, :last, :[], :any?, :each_with_index, to: :to_a
    delegate :map, :select, :each, :sort_by, :find, :each_slice, to: :to_a

    def initialize(klass)
      super()
      @klass = klass
      @loaded = false
      @response = {}
    end

    # Support for WillPaginate, if it is defined. If not, returns self
    #
    def paginate(page = 1, per_page = 25)
      if Object.const_defined?("WillPaginate")
        WillPaginate::Collection.create(page, per_page, count) do |pager|
          pager.replace(self[pager.offset, pager.per_page]).to_a
        end
      elsif Object.const_defined?("Kaminari")
        Kaminari.paginate_array(self).page(page).per(per_page)
      else
        self
      end
    end

    # Just adds a limit of 1 to the query, and forces it to return a singular
    # resource
    #
    def first
      limit(1).to_a.first
    end

    # Utility method to allow viewing of the result set in a console
    #
    def inspect
      to_a.inspect
    end

    # Returns the set of records if they have already been fetched, otherwise
    # gets the records and returns them
    #
    def to_a
      return @records if loaded?

      @response = cache.get_or_set_query(klass, query) do
        connection.documents(klass.table_name, query)
      end

      @records = []

      if @response.success?
        @response.members.each do |member|
          @records << klass.new(member.attributes.merge(persisted: true))
        end

        @loaded = true
      end

      @records
    end

    # Force reload of the record
    #
    def reload
      reset
      to_a
      self
    end

    # Reset the whole shebang
    #
    def reset
      cache.remove("#{klass}/#{query}")
      @records = []
      @loaded = nil
      self
    end

    # Parses the current query hash and returns a JSON string
    #
    def to_json
      @query.to_json
    end
  end
end<commit_msg>Add debugging to relation method<commit_after>require 'json'

module MontageRails
  class Relation < Montage::Query
    attr_reader :klass, :response, :loaded

    alias_method :loaded?, :loaded

    delegate :connection, to: MontageRails
    delegate :cache, to: :klass
    delegate :count, :length, :last, :[], :any?, :each_with_index, to: :to_a
    delegate :map, :select, :each, :sort_by, :find, :each_slice, to: :to_a

    def initialize(klass)
      super()
      @klass = klass
      @loaded = false
      @response = {}
    end

    # Support for WillPaginate, if it is defined. If not, returns self
    #
    def paginate(page = 1, per_page = 25)
      if Object.const_defined?("WillPaginate")
        WillPaginate::Collection.create(page, per_page, count) do |pager|
          pager.replace(self[pager.offset, pager.per_page]).to_a
        end
      elsif Object.const_defined?("Kaminari")
        Kaminari.paginate_array(self).page(page).per(per_page)
      else
        self
      end
    end

    # Just adds a limit of 1 to the query, and forces it to return a singular
    # resource
    #
    def first
      limit(1).to_a.first
    end

    # Utility method to allow viewing of the result set in a console
    #
    def inspect
      to_a.inspect
    end

    # Returns the set of records if they have already been fetched, otherwise
    # gets the records and returns them
    #
    def to_a
      return @records if loaded?

      @response = cache.get_or_set_query(klass, query) do
        puts "Query object in to_a method: #{query.inspect}"
        resp = connection.documents(klass.table_name, query)
        puts "Raw Montage reponse in to_a method: #{resp.inspect}"
        resp
      end

      @records = []

      if @response.success?
        @response.members.each do |member|
          @records << klass.new(member.attributes.merge(persisted: true))
        end

        @loaded = true
      end

      @records
    end

    # Force reload of the record
    #
    def reload
      reset
      to_a
      self
    end

    # Reset the whole shebang
    #
    def reset
      cache.remove("#{klass}/#{query}")
      @records = []
      @loaded = nil
      self
    end
  end
end
{'fraction_non_alphanumeric': 0.09240999770694795, 'fraction_numerical': 0.002293052052281587, 'mean_word_length': 2.2527964205816553, 'pattern_counts': {'":': 0, '<': 9, '<?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': '5499225', 'n_tokens_mistral': 1373, 'n_tokens_neox': 1358, 'n_words': 412}