Module: Genealogy::UtilMethods

Extended by:
ActiveSupport::Concern
Includes:
Constants
Defined in:
lib/genealogy/util_methods.rb

Overview

Module UtilMethods provides methods to run utility methods. It's included by the genealogy enabled AR model

Defined Under Namespace

Modules: ClassMethods

Constant Summary

Constants included from Constants

Constants::AKA, Constants::DEFAULTS, Constants::LINEAGE2PARENT, Constants::OFF, Constants::OPPOSITESEX, Constants::PARENT2LINEAGE, Constants::PARENT2SEX, Constants::PEDIGREE, Constants::PEDIGREE_AND_DATES, Constants::SEX2PARENT

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_method_parent_birth_range(parent) ⇒ Object


80
81
82
83
84
85
86
87
88
# File 'lib/genealogy/util_methods.rb', line 80

def self.generate_method_parent_birth_range(parent)
  define_method "#{parent}_birth_range" do
    if birth
      (birth - gclass.send("max_#{PARENT2SEX[parent]}_procreation_age").years)..(birth - gclass.send("min_#{PARENT2SEX[parent]}_procreation_age").years)
    elsif life_range
      (life_range.begin - gclass.send("max_#{PARENT2SEX[parent]}_procreation_age").years)..(life_range.end - gclass.send("min_#{PARENT2SEX[parent]}_procreation_age").years)
    end
  end
end

.generate_method_parent_fertility_range(parent) ⇒ Object


96
97
98
99
100
101
102
# File 'lib/genealogy/util_methods.rb', line 96

def self.generate_method_parent_fertility_range(parent)
  define_method "#{parent}_fertility_range" do
    if parent_birth_range = send("#{parent}_birth_range")
      (parent_birth_range.begin + gclass.send("min_#{PARENT2SEX[parent]}_procreation_age").years)..(parent_birth_range.end + gclass.send("max_#{PARENT2SEX[parent]}_procreation_age").years)
    end
  end
end

Instance Method Details

#birthDate

Genealogy thinks time in term of Date, not DateTime


10
11
12
# File 'lib/genealogy/util_methods.rb', line 10

def birth
  birth_date.try(:to_date)
end

#birth_rangeRange

optimistic (longest) estimation of birth date. Calculation is based on available dates and max life expectancy


36
37
38
39
40
41
42
# File 'lib/genealogy/util_methods.rb', line 36

def birth_range
  if birth
    birth..birth # exact
  elsif death
    (death - max_le)..death # estimation based on death
  end
end

#can_procreate_during?(period) ⇒ Boolean

It tests whether fertility range overlaps specified period


72
73
74
# File 'lib/genealogy/util_methods.rb', line 72

def can_procreate_during?(period)
  fertility_range.overlaps? period if (period and fertility_range)
end

#can_procreate_on?(date) ⇒ Boolean

It tests whether fertility range covers specified date


65
66
67
# File 'lib/genealogy/util_methods.rb', line 65

def can_procreate_on?(date)
  fertility_range.cover? date if (date and fertility_range)
end

#deathDate

Genealogy thinks time in term of Date, not DateTime


16
17
18
# File 'lib/genealogy/util_methods.rb', line 16

def death
  death_date.try(:to_date)
end

#father_birth_rangeRange

optimistic (longest) estimation of father's birth date. Calculation is based on receiver's birth or #life_range, max life expectancy and min and max fertility procreation ages


89
# File 'lib/genealogy/util_methods.rb', line 89

generate_method_parent_birth_range(:father)

#father_fertility_rangeRange

optimistic (longest) estimation of father's fertility range. Calculation is based on receiver's #father_birth_range, min and max fertility procreation ages


103
# File 'lib/genealogy/util_methods.rb', line 103

generate_method_parent_fertility_range(:father)

#fertility_rangeRange

optimistic (longest) estimation of fertility period. Calculation is based on available dates, max life expectancy and min and max fertility procreation ages


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/genealogy/util_methods.rb', line 46

def fertility_range
  if birth
    if death
      if death > birth + min_fpa
        (birth + min_fpa)..([(birth + max_fpa), death].min) # best estimation
      else
        false # died before reaching min fertility age
      end
    else
      (birth + min_fpa)..(birth + max_fpa) # estimation based on birth
    end
  elsif death
    (death - max_le + min_fpa)..death # estimation based on death
  end
end

#is_female?Boolean


126
127
128
129
# File 'lib/genealogy/util_methods.rb', line 126

def is_female?
  return female? if respond_to?(:female?)
  sex == gclass.sex_female_value
end

#is_male?Boolean


132
133
134
135
# File 'lib/genealogy/util_methods.rb', line 132

def is_male?
  return male? if respond_to?(:male?)
  sex == gclass.sex_male_value
end

#life_rangeRange

optimistic (longest) estimation of life period. Calculation is based on available dates and max life expectancy


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/genealogy/util_methods.rb', line 22

def life_range
  if birth
    if death
      birth..death # exact
    else
      birth..(birth + max_le) # estimation based on birth
    end
  elsif death
    (death - max_le)..death # estimation based on death
  end
end

#max_fpaInteger

max fertility procreation age in terms of years. It depends on sex


145
146
147
# File 'lib/genealogy/util_methods.rb', line 145

def max_fpa
  gclass.send("max_#{ssex}_procreation_age").years
end

#max_leInteger

max life expectancy in terms of years. It depends on sex


139
140
141
# File 'lib/genealogy/util_methods.rb', line 139

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

#min_fpaInteger

min fertility procreation age in terms of years. It depends on sex


151
152
153
# File 'lib/genealogy/util_methods.rb', line 151

def min_fpa
  gclass.send("min_#{ssex}_procreation_age").years
end

#mother_birth_rangeRange

optimistic (longest) estimation of mother's birth date. Calculation is based on receiver's birth or #life_range, max life expectancy and min and max fertility procreation ages


90
# File 'lib/genealogy/util_methods.rb', line 90

generate_method_parent_birth_range(:mother)

#mother_fertility_rangeRange

optimistic (longest) estimation of mother's fertility range. Calculation is based on receiver's #mother_birth_range, min and max fertility procreation ages


104
# File 'lib/genealogy/util_methods.rb', line 104

generate_method_parent_fertility_range(:mother)

#opposite_ssexSymbol

opposite sex in terms of :male or :female


121
122
123
# File 'lib/genealogy/util_methods.rb', line 121

def opposite_ssex
  OPPOSITESEX[ssex]
end

#ssexSymbol

sex in terms of :male or :female


108
109
110
111
112
113
114
115
116
117
# File 'lib/genealogy/util_methods.rb', line 108

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