<commit_before>module Unrollable
  # Class representing a list of columns providing methods to convert them.
  class ColumnList
    include Toolbelt

    DEFAULT_COLUMNS = %i(title label full_name name code number reference_number).freeze

    def initialize(list, model)
      @model = model
      columns = deep_compact(list)

      available_methods = @model.column_names.map(&:to_sym)
      fallback = DEFAULT_COLUMNS & available_methods

      no_cols = "No column available to unroll #{@model.name} records."
      @columns = if_there(columns) || if_there(fallback)
      @columns || raise(no_cols)
    end

    def to_filters(object: nil, model: nil, parents: [])
      object ||= @columns
      model  ||= @model

      case object
      when Array then object.map { |o| to_filters(object: o, model: model, parents: parents) }
                            .flatten
      when Hash  then
        a = object.symbolize_keys.map do |k, v|
          to_filters(
            object: v,
            model: reflection_class(k, model),
            parents: parents + [k]
          )
        end
        a.flatten
      when Symbol, String then Filter.new(object, model, parents)
      else raise "Don't know how to handle object: #{object.inspect}."
      end
    end

    def to_includes(object: nil)
      object ||= @columns

      case object
      when Array then first_if_alone(object.map { |o| to_includes(object: o) }.compact)
      when Hash
        object.map do |k, v|
          a = [k, to_includes(object: v)]
          a.last.nil? ? k : a.to_h
        end
      when Symbol, String then nil
      else raise "Don't know how to handle object: #{object.inspect}."
      end
    end

    protected

    def reflection_class(name, model)
      reflection = model.reflect_on_association(name)
      raise "Cannot find a reflection #{name} for #{model.name}" unless reflection

      reflection.class_name.constantize
    end
  end<commit_msg>Fixed to_include because it messed up on CapStatementController.<commit_after>module Unrollable
  # Class representing a list of columns providing methods to convert them.
  class ColumnList
    include Toolbelt

    DEFAULT_COLUMNS = %i(title label full_name name code number reference_number).freeze

    def initialize(list, model)
      @model = model
      columns = deep_compact(list)

      available_methods = @model.column_names.map(&:to_sym)
      fallback = DEFAULT_COLUMNS & available_methods

      no_cols = "No column available to unroll #{@model.name} records."
      @columns = if_there(columns) || if_there(fallback)
      @columns || raise(no_cols)
    end

    def to_filters(object: nil, model: nil, parents: [])
      object ||= @columns
      model  ||= @model

      case object
      when Array then object.map { |o| to_filters(object: o, model: model, parents: parents) }
                            .flatten
      when Hash  then
        a = object.symbolize_keys.map do |k, v|
          to_filters(
            object: v,
            model: reflection_class(k, model),
            parents: parents + [k]
          )
        end
        a.flatten
      when Symbol, String then Filter.new(object, model, parents)
      else raise "Don't know how to handle object: #{object.inspect}."
      end
    end

    def to_includes(object: nil)
      object ||= @columns

      case object
      when Array then first_if_alone(object.map { |o| to_includes(object: o) }.compact)
      when Hash
        object.map do |k, v|
          a = [k, to_includes(object: v)]
          a.last.nil? ? k : [a].to_h
        end
      when Symbol, String then nil
      else raise "Don't know how to handle object: #{object.inspect}."
      end
    end

    protected

    def reflection_class(name, model)
      reflection = model.reflect_on_association(name)
      raise "Cannot find a reflection #{name} for #{model.name}" unless reflection

      reflection.class_name.constantize
    end
  end
{'fraction_non_alphanumeric': 0.09090909090909091, 'fraction_numerical': 0.0, 'mean_word_length': 2.3617021276595747, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2645375', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1134, 'n_words': 360}