<commit_before>module Genealogy
  # Module UtilMethods provides methods to run utility methods. It's included by the genealogy enabled AR model
  module UtilMethods
    extend ActiveSupport::Concern

    include Constants

    # @return [Boolean] 
    def is_female?
      return female? if respond_to?(:female?)
      sex == gclass.sex_female_value
    end

    # @return [Boolean] 
    def is_male?
      return male? if respond_to?(:male?)
      sex == gclass.sex_male_value
    end

    # Genealogy thinks time in term of Date, not DateTime
    # @return [Date]
    def birth
      birth_date.try(:to_date)
    end

    # Genealogy thinks time in term of Date, not DateTime
    # @return [Date]
    def death
      death_date.try(:to_date)
    end

    # According to procreation ages says if self can procreate at specified time
    # @param [Date] date
    # @return [Boolean] 
    def can_procreate_on?(date)
      fertility_range.cover? date if fertility_range
    end

    # According to procreation ages says if self can procreate at specified time
    # @param [Range] period is a range of dates
    # @return [Boolean] 
    def can_procreate_during?(period)
      fertility_range.overlaps? period if fertility_range
    end

    def died_at
      death - birth 
    end

    # If either birth or death are present it's possible to estimate a life range using the life max expectancy
    # @return [Range] 
    def life_range
      case [birth.present?,death.present?]
      when [true,true]
        birth..death
      when [true,false]
        birth..(birth + max_le)
      when [false,true]
        (death - max_le)..death
      end
    end


    # If either birth or death are present it's possible to estimate a life range using the min and max procreation ages
    # @return [Range] 
    def fertility_range
      case [birth.present?,death.present?]
      when [true,true]
        (birth + min_fpa)..([(birth + max_fpa), death].min)
      when [true,false]
        (birth + min_fpa)..(birth + max_fpa)
      when [false,true]
        (death - max_le + min_fpa)..death
      end
    end


    # @!macro [attach] generate
    #   @method $1_birth_range
    #   If life range is definable than it's possible to estimate the $1 life range using the min and max procreation ages
    #   @return [Range]
    def self.generate_method_parent_birth_range(parent)
      define_method "#{parent}_birth_range" do
        if birth
          (birth - max_fpa(PARENT2SEX[parent]))..(birth - min_fpa(PARENT2SEX[parent]))
        elsif life_range
          (life_range.begin - max_fpa(PARENT2SEX[parent]))..(life_range.end - min_fpa(PARENT2SEX[parent]))
        end
      end
    end
    generate_method_parent_birth_range(:father)
    generate_method_parent_birth_range(:mother)

    def ssex
      case sex
      when gclass.sex_male_value
        :male
      when gclass.sex_female_value
        :female
      else
        raise SexError, "Sex value not valid for #{self}"
      end
    end

    def opposite_ssex
      OPPOSITESEX[ssex]
    end

    private

    def max_le(arg=nil)
      gclass.send("max_#{arg or ssex}_life_expectancy").years
    end

    def max_fpa(arg=nil)
      gclass.send("max_#{arg or ssex}_procreation_age").years<commit_msg>no message<commit_after>module Genealogy
  # Module UtilMethods provides methods to run utility methods. It's included by the genealogy enabled AR model
  module UtilMethods
    extend ActiveSupport::Concern

    include Constants

    # @return [Boolean] 
    def is_female?
      return female? if respond_to?(:female?)
      sex == gclass.sex_female_value
    end

    # @return [Boolean] 
    def is_male?
      return male? if respond_to?(:male?)
      sex == gclass.sex_male_value
    end

    # Genealogy thinks time in term of Date, not DateTime
    # @return [Date]
    def birth
      birth_date.try(:to_date)
    end

    # Genealogy thinks time in term of Date, not DateTime
    # @return [Date]
    def death
      death_date.try(:to_date)
    end

    # According to procreation ages says if self can procreate at specified time
    # @param [Date] date
    # @return [Boolean] 
    def can_procreate_on?(date)
      fertility_range.cover? date if fertility_range
    end

    # According to procreation ages says if self can procreate at specified time
    # @param [Range] period is a range of dates
    # @return [Boolean] 
    def can_procreate_during?(period)
      fertility_range.overlaps? period if fertility_range
    end

    def died_at
      death - birth 
    end

    # If either birth or death are present it's possible to estimate a life range using the life max expectancy
    # @return [Range] 
    def life_range
      case [birth.present?,death.present?]
      when [true,true]
        birth..death
      when [true,false]
        birth..(birth + max_le)
      when [false,true]
        (death - max_le)..death
      end
    end


    # If either birth or death are present it's possible to estimate a life range using the min and max procreation ages
    # @return [Range] 
    def fertility_range
      case [birth.present?,death.present?]
      when [true,true]
        (birth + min_fpa)..([(birth + max_fpa), death].min)
      when [true,false]
        (birth + min_fpa)..(birth + max_fpa)
      when [false,true]
        (death - max_le + min_fpa)..death
      end
    end


    # @!macro [attach] generate
    #   @method $1_birth_range
    #   If life range is definable than it's possible to estimate the $1 life range using the min and max procreation ages
    #   @return [Range]
    def self.generate_method_parent_birth_range(parent)
      define_method "#{parent}_birth_range" do
        if birth
          (birth - max_fpa(PARENT2SEX[parent]))..(birth - min_fpa(PARENT2SEX[parent]))
        elsif life_range
          (life_range.begin - max_fpa(PARENT2SEX[parent]))..(life_range.end - min_fpa(PARENT2SEX[parent]))
        end
      end
    end
    generate_method_parent_birth_range(:father)
    generate_method_parent_birth_range(:mother)

    # sex in terms of :male or :female
    # @return [Symbol] 
    def ssex
      case sex
      when gclass.sex_male_value
        :male
      when gclass.sex_female_value
        :female
      else
        raise SexError, "Sex value not valid for #{self}"
      end
    end

    # opposite sex in terms of :male or :female
    # @return [Symbol] 
    def opposite_ssex
      OPPOSITESEX[ssex]
    end

    private

    def max_le(arg=nil)
      gclass.send("max_#{arg or ssex}_life_expectancy").years
    end

    def max_fpa(arg=nil)
      gclass.send("max_#{arg or ssex}_procreation_age").years
{'fraction_non_alphanumeric': 0.08278145695364239, 'fraction_numerical': 0.001806140878988561, 'mean_word_length': 2.551576696953501, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7365041', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2030, 'n_words': 623}