<commit_before>module Persistence
  class ArticleService
    include Pharrell::Constructor

    constructor Persistence::Database

    def initialize(db, published: nil, page: nil)
      @db = db
      @published = published
      @page = page
    end

    def published
      self.class.new(@db, :published => true, :page => @page)
    end

    def unpublished
      self.class.new(@db, :published => false, :page => @page)
    end

    def page(n)
      self.class.new(@db, :page => n, :published => @published)
    end

    def fetch(id)
      row = dataset.where(:id => id).first

      if row
        Model.new(row)
      else
        nil
      end
    end

    def fetch_all
      dataset.reverse_order(:published).all.map { |hash| Model.new(hash) }
    end

    def fetch_all_from_category(id)
      dataset.reverse_order(:published).where(:category_id => id).all.map { |hash| Model.new(hash) }
    end

    def create(author, title, category, content, image_url = nil)
      @db[:articles].insert(
        :author => author,
        :title => title,
        :category_id => category,
        :content => content,
        :image_url => image_url
      )
    end

    def update(id, author, title, category, content, image_url = nil)
      @db[:articles].where(:id => id).update(
        :author => author,
        :title => title,
        :category_id => category,
        :content => content,
        :image_url => image_url
      )
    end

    def delete(id)
      @db[:articles].where(:id => id).delete
    end

    def publish(id, time)
      @db[:articles].where(:id => id).update(:published => time)
    end

    def unpublish(id)
      @db[:articles].where(:id => id).update(:published => nil)
    end

    private

    def dataset
      dataset = if @published.nil?
        @db[:articles]
      elsif @published
        @db[:articles].where("published IS NOT NULL")
      else
        @db[:articles].where("published IS NULL")
      end

      if @page
        dataset.limit(9).offset(@page * 9)
      else
        dataset
      end
    end
  end
end<commit_msg>Why am I writing that code. There ain't no test<commit_after>module Persistence
  class ArticleService
    include Pharrell::Constructor

    constructor Persistence::Database

    def initialize(db, published: nil, page: nil)
      @db = db
      @published = published
      @page = page
    end

    def published
      self.class.new(@db, :published => true, :page => @page)
    end

    def unpublished
      self.class.new(@db, :published => false, :page => @page)
    end

    def page(n)
      self.class.new(@db, :page => n, :published => @published)
    end

    def fetch(id)
      row = dataset.where(:id => id).first

      if row
        Model.new(row)
      else
        nil
      end
    end

    def fetch_all
      dataset.reverse_order(:published).all.map { |hash| Model.new(hash) }
    end

    def fetch_all_from_category(id)
      dataset.reverse_order(:published).where(:category_id => id).all.map { |hash| Model.new(hash) }
    end

    def create(author, title, category, content, image_url = nil)
      @db[:articles].insert(
        :author => author,
        :title => title,
        :category_id => category,
        :content => content,
        :image_url => image_url
      )
    end

    def update(id, author, title, category, content, image_url = nil)
      @db[:articles].where(:id => id).update(
        :author => author,
        :title => title,
        :category_id => category,
        :content => content,
        :image_url => image_url
      )
    end

    def delete(id)
      @db[:articles].where(:id => id).delete
    end

    def publish(id, time)
      @db[:articles].where(:id => id).update(:published => time)
    end

    def unpublish(id)
      @db[:articles].where(:id => id).update(:published => nil)
    end

    private

    def dataset
      dataset = if @published.nil?
        @db[:articles]
      elsif @published
        @db[:articles].where("published IS NOT NULL")
      else
        @db[:articles].where("published IS NULL")
      end

      if @page
        dataset.limit(9)
      else
        dataset
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.1282358581016299, 'fraction_numerical': 0.0007190795781399808, 'mean_word_length': 2.1709726443768997, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 51, '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': '1538034', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1243, 'n_words': 319}