<commit_before>require 'json'
require 'sequel'

module Thyme
  class Photo < Sequel::Model
    THUMBS_PATH = File.expand_path('../../../public/thumbs', __FILE__)
    BIG_THUMB_SIZE = [1000, 1000]
    SMALL_THUMB_SIZE = [200, 200]

    plugin :json_serializer
    plugin :serialization, :json, :exif

    many_to_one :_set, class: :Set # "set" is reserved by Sequel...
    one_to_one :prev_photo, key: :prev_photo_id, class: self
    one_to_one :next_photo, key: :next_photo_id, class: self

    dataset_module do
      def newest_first
        reverse_order(:taken_at).order_append(:path)
      end

      def oldest_first
        order(:taken_at).order_append(:path)
      end
    end

    def big_thumb_url
      thumb_url(:big)
    end

    def small_thumb_url
      thumb_url(:small)
    end

    def to_json(options = {})
      super(
        options.merge(include: [
          :aspect_ratio,
          :big_thumb_height,
          :big_thumb_url,
          :big_thumb_width,
          :filename,
          :lat,
          :lng,
          :orientation,
          :small_thumb_url,
        ])
      )
    end

    private

    def aspect_ratio
      gcd = width.gcd(height).to_f
      [width / gcd, height / gcd]
    end

    def big_thumb_height
      if portrait?
        [height, BIG_THUMB_SIZE.max].min
      else
        ((aspect_ratio[1] / aspect_ratio[0].to_f) * big_thumb_width).round(2)
      end
    end

    def big_thumb_width
      if portrait?
        ((aspect_ratio[0] / aspect_ratio[1].to_f) * big_thumb_height).round(2)
      else
        [width, BIG_THUMB_SIZE.max].min
      end
    end

    def filename<commit_msg>Round calculated big thumb width/height correctly<commit_after>require 'json'
require 'sequel'

module Thyme
  class Photo < Sequel::Model
    THUMBS_PATH = File.expand_path('../../../public/thumbs', __FILE__)
    BIG_THUMB_SIZE = [1000, 1000]
    SMALL_THUMB_SIZE = [200, 200]

    plugin :json_serializer
    plugin :serialization, :json, :exif

    many_to_one :_set, class: :Set # "set" is reserved by Sequel...
    one_to_one :prev_photo, key: :prev_photo_id, class: self
    one_to_one :next_photo, key: :next_photo_id, class: self

    dataset_module do
      def newest_first
        reverse_order(:taken_at).order_append(:path)
      end

      def oldest_first
        order(:taken_at).order_append(:path)
      end
    end

    def big_thumb_url
      thumb_url(:big)
    end

    def small_thumb_url
      thumb_url(:small)
    end

    def to_json(options = {})
      super(
        options.merge(include: [
          :aspect_ratio,
          :big_thumb_height,
          :big_thumb_url,
          :big_thumb_width,
          :filename,
          :lat,
          :lng,
          :orientation,
          :small_thumb_url,
        ])
      )
    end

    private

    def aspect_ratio
      gcd = width.gcd(height).to_f
      [width / gcd, height / gcd]
    end

    def big_thumb_height
      if portrait?
        [height, BIG_THUMB_SIZE.max].min
      else
        ((aspect_ratio[1] / aspect_ratio[0].to_f) * big_thumb_width).round
      end
    end

    def big_thumb_width
      if portrait?
        ((aspect_ratio[0] / aspect_ratio[1].to_f) * big_thumb_height).round
      else
        [width, BIG_THUMB_SIZE.max].min
      end
    end

    def filename
{'fraction_non_alphanumeric': 0.09824667472793229, 'fraction_numerical': 0.011487303506650543, 'mean_word_length': 2.148430066603235, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2596390', 'n_tokens_mistral': 1176, 'n_tokens_neox': 1119, 'n_words': 232}