Module: Genealogy::UtilMethods

Extended by:
ActiveSupport::Concern
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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_method_parent_birth_range(parent) ⇒ Object



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

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

.generate_method_parent_fertility_range(parent) ⇒ Object



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

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_#{gclass::PARENT2SEX[parent]}_procreation_age").years)..(parent_birth_range.end + gclass.send("max_#{gclass::PARENT2SEX[parent]}_procreation_age").years)
    end
  end
end

Instance Method Details

#birthDate

Genealogy thinks time in term of Date, not DateTime

Returns:

  • (Date)


8
9
10
# File 'lib/genealogy/util_methods.rb', line 8

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

Returns:

  • (Range)

    longest possible birth date interval or nil if cannot be computable



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

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

Parameters:

  • period (Range)

Returns:

  • (Boolean)

    or nil if cannot be computable (#fertility_range returns nil)



70
71
72
# File 'lib/genealogy/util_methods.rb', line 70

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

Parameters:

  • date (Date)

Returns:

  • (Boolean)

    or nil if cannot be computable (#fertility_range returns nil)



63
64
65
# File 'lib/genealogy/util_methods.rb', line 63

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

Returns:

  • (Date)


14
15
16
# File 'lib/genealogy/util_methods.rb', line 14

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

Returns:

  • (Range)

    longest possible father's birth date interval or nil when is not computable that is when birth or life range are not available



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

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

Returns:

  • (Range)

    longest possible father's fertility period or nil when is not computable that is when father_birth_range is not computable



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

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

Returns:

  • (Range)

    fertility period, nil if cannot be computable, false if died before reaching min fertility age



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

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

Returns:

  • (Boolean)


124
125
126
127
# File 'lib/genealogy/util_methods.rb', line 124

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

#is_male?Boolean

Returns:

  • (Boolean)


130
131
132
133
# File 'lib/genealogy/util_methods.rb', line 130

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

#life_rangeRange

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

Returns:

  • (Range)

    life period or nil if cannot be computable



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

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

Returns:

  • (Integer)


143
144
145
# File 'lib/genealogy/util_methods.rb', line 143

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

#max_leInteger

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

Returns:

  • (Integer)


137
138
139
# File 'lib/genealogy/util_methods.rb', line 137

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

Returns:

  • (Integer)


149
150
151
# File 'lib/genealogy/util_methods.rb', line 149

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

Returns:

  • (Range)

    longest possible mother's birth date interval or nil when is not computable that is when birth or life range are not available



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

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

Returns:

  • (Range)

    longest possible mother's fertility period or nil when is not computable that is when mother_birth_range is not computable



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

generate_method_parent_fertility_range(:mother)

#opposite_ssexSymbol

opposite sex in terms of :male or :female

Returns:

  • (Symbol)


119
120
121
# File 'lib/genealogy/util_methods.rb', line 119

def opposite_ssex
  gclass::OPPOSITESEX[ssex]
end

#ssexSymbol

sex in terms of :male or :female

Returns:

  • (Symbol)


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

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