Module: Genealogy::AlterMethods

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

Overview

Module AlterMethods provides methods to alter genealogy. It's included by the genealogy enabled AR model

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_add_grandparent(lineage, grandparent) ⇒ Object


76
77
78
79
80
81
82
83
84
# File 'lib/genealogy/alter_methods.rb', line 76

def self.generate_method_add_grandparent(lineage,grandparent)
  relationship = "#{lineage}_grand#{grandparent}"
  define_method "add_#{relationship}" do |gp|
    parent = LINEAGE2PARENT[lineage]
    raise_if_gap_on(parent)
    check_incompatible_relationship(relationship,gp)
    send(parent).send("add_#{grandparent}",gp)
  end
end

.generate_method_add_grandparents_by_lineage(lineage) ⇒ Object


115
116
117
118
119
120
121
122
# File 'lib/genealogy/alter_methods.rb', line 115

def self.(lineage)
  relationship = "#{lineage}_grandparents"
  define_method "add_#{relationship}" do |gf,gm|
    parent = LINEAGE2PARENT[lineage]
    raise_if_gap_on(parent)
    send(parent).send("add_parents",gf,gm)
  end
end

.generate_method_add_parent(parent) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/genealogy/alter_methods.rb', line 14

def self.generate_method_add_parent(parent)
  define_method "add_#{parent}" do |relative|
    check_incompatible_relationship(parent,relative)
    if gclass.perform_validation_enabled
      self.send("#{parent}=",relative)
      save!
    else
      self.update_attribute(parent,relative)
    end
  end
end

.generate_method_remove_grandparent(lineage, grandparent) ⇒ Object


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

def self.generate_method_remove_grandparent(lineage,grandparent)
  relationship = "#{lineage}_grand#{grandparent}"
  define_method "remove_#{relationship}" do
    parent = LINEAGE2PARENT[lineage]
    raise_if_gap_on(parent)
    send(parent).send("remove_#{grandparent}")
  end
end

.generate_method_remove_grandparents_by_lineage(lineage) ⇒ Object


131
132
133
134
135
136
137
138
# File 'lib/genealogy/alter_methods.rb', line 131

def self.(lineage)
  relationship = "#{lineage}_grandparents"
  define_method "remove_#{relationship}" do 
    parent = LINEAGE2PARENT[lineage]
    raise_if_gap_on(parent)
    send(parent).send("remove_parents")
  end
end

.generate_method_remove_parent(parent) ⇒ Object


33
34
35
36
37
38
39
40
41
42
# File 'lib/genealogy/alter_methods.rb', line 33

def self.generate_method_remove_parent(parent)
  define_method "remove_#{parent}" do
    if gclass.perform_validation_enabled
      self.send("#{parent}=",nil)
      save!
    else
      self.update_attribute(parent,nil)
    end
  end
end

Instance Method Details

#add_child(child, options = {}) ⇒ Object

see #add_children


271
272
273
# File 'lib/genealogy/alter_methods.rb', line 271

def add_child(child,options={})
  add_children(child,options)
end

#add_children(*children, options = {}) ⇒ Boolean

add children by assigning self as parent

Options Hash (options):

  • spouse (Object)

    if specified, children will have that spouse


250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/genealogy/alter_methods.rb', line 250

def add_children(*args)
  options = args.extract_options!
  raise_if_sex_undefined
  check_incompatible_relationship(:children, *args)
  transaction do
    args.inject(true) do |res,child|
      res &= case sex
      when gclass.sex_male_value
        child.add_mother(options[:spouse]) if options[:spouse]
        child.add_father(self)
      when gclass.sex_female_value
        child.add_father(options[:spouse]) if options[:spouse]
        child.add_mother(self)
      else 
        raise SexError, "Sex value not valid for #{self}"
      end
    end
  end
end

#add_father(parent) ⇒ Boolean

Add father

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


25
# File 'lib/genealogy/alter_methods.rb', line 25

generate_method_add_parent(:father)

#add_grandparents(pgf, pgm, mgf, mgm) ⇒ Boolean

add all grandparents calling #add_paternal_grandparents and #add_maternal_grandparents in a transaction


151
152
153
154
155
156
# File 'lib/genealogy/alter_methods.rb', line 151

def add_grandparents(pgf,pgm,mgf,mgm)
  transaction do
    add_paternal_grandparents(pgf,pgm)
    add_maternal_grandparents(mgf,mgm)
  end
end

#add_maternal_grandfather(grandparent) ⇒ Boolean

Add maternal grandfather

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


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

generate_method_add_grandparent(:maternal,:father)

#add_maternal_grandmother(grandparent) ⇒ Boolean

Add maternal grandmother

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


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

generate_method_add_grandparent(:maternal,:mother)

#add_maternal_grandparentsBoolean

Add maternal grandparents

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


124
# File 'lib/genealogy/alter_methods.rb', line 124

(:maternal)

#add_mother(parent) ⇒ Boolean

Add mother

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


26
# File 'lib/genealogy/alter_methods.rb', line 26

generate_method_add_parent(:mother)

#add_parents(father, mother) ⇒ Boolean

add both parents calling #add_father and #add_mother in a transaction


52
53
54
55
56
57
# File 'lib/genealogy/alter_methods.rb', line 52

def add_parents(father,mother)
  transaction do
    add_father(father)
    add_mother(mother)
  end
end

#add_paternal_grandfather(grandparent) ⇒ Boolean

Add paternal grandfather

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


85
# File 'lib/genealogy/alter_methods.rb', line 85

generate_method_add_grandparent(:paternal,:father)

#add_paternal_grandmother(grandparent) ⇒ Boolean

Add paternal grandmother

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


86
# File 'lib/genealogy/alter_methods.rb', line 86

generate_method_add_grandparent(:paternal,:mother)

#add_paternal_grandparentsBoolean

Add paternal grandparents

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


123
# File 'lib/genealogy/alter_methods.rb', line 123

(:paternal)

#add_sibling(sibling, options = {}) ⇒ Object

See Also:


202
203
204
# File 'lib/genealogy/alter_methods.rb', line 202

def add_sibling(sibling,options={})
  add_siblings(sibling,options)
end

#add_siblings(*siblings, options = {}) ⇒ Boolean

add siblings by assigning same parents to individuals passed as arguments

Options Hash (options):

  • half (Symbol)

    :father for paternal half siblings and :mother for maternal half siblings

  • spouse (Object)

    if specified, passed individual will be used as mother in case of half sibling


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/genealogy/alter_methods.rb', line 176

def add_siblings(*args)
  options = args.extract_options!
  check_incompatible_relationship(:sibling, *args)
  transaction do
    args.inject(true) do |res,sib|
      res &= case options[:half]
      when :father
        raise LineageGapException, "Can't add paternal halfsiblings without a father" unless father
        sib.add_mother(options[:spouse]) if options[:spouse]
        sib.add_father(father)
      when :mother
        raise LineageGapException, "Can't add maternal halfsiblings without a mother" unless mother
        sib.add_father(options[:spouse]) if options[:spouse]
        sib.add_mother(mother)
      when nil
        raise LineageGapException, "Can't add siblings without parents" unless father and mother
        sib.add_father(father)
        sib.add_mother(mother)
      else
        raise ArgumentError, "Admitted values for :half options are: :father, :mother or nil"
      end
    end
  end
end

#remove_child(child, options = {}) ⇒ Object

see #remove_children


312
313
314
# File 'lib/genealogy/alter_methods.rb', line 312

def remove_child(child,options={})
  remove_children(child,options)
end

#remove_children(*children, options = {}) ⇒ Boolean

remove children by nullifying the parent corresponding to self

Options Hash (options):

  • remove_other_parent (Boolean)

    if specified, passed individuals' mother will also be nullified


281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/genealogy/alter_methods.rb', line 281

def remove_children(*args)
  options = args.extract_options!

  raise_if_sex_undefined

  resulting_indivs = if args.blank?
    children(options)
  else
    args & children(options)
  end

  transaction do
    resulting_indivs.each do |child|
      if options[:remove_other_parent] == true
        child.remove_parents
      else
        case sex
        when gclass.sex_male_value
          child.remove_father
        when gclass.sex_female_value
          child.remove_mother
        else 
          raise SexError, "Sex value not valid for #{self}"
        end  
      end
    end
  end
  !resulting_indivs.empty? #returned value must be true if self has at least a siblings to affect
end

#remove_fatherBoolean

remove father. Foreign_key set to nil

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


43
# File 'lib/genealogy/alter_methods.rb', line 43

generate_method_remove_parent(:father)

#remove_grandparentsBoolean

remove all grandparents calling #remove_paternal_grandparents and #remove_maternal_grandparents in a transaction


162
163
164
165
166
167
# File 'lib/genealogy/alter_methods.rb', line 162

def remove_grandparents
  transaction do
    remove_paternal_grandparents
    remove_maternal_grandparents
  end
end

#remove_maternal_grandfatherBoolean

remove maternal grandfather

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


105
# File 'lib/genealogy/alter_methods.rb', line 105

generate_method_remove_grandparent(:maternal,:father)

#remove_maternal_grandmotherBoolean

remove maternal grandmother

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


106
# File 'lib/genealogy/alter_methods.rb', line 106

generate_method_remove_grandparent(:maternal,:mother)

#remove_maternal_grandparentsBoolean

remove maternal grandparents

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


140
# File 'lib/genealogy/alter_methods.rb', line 140

(:maternal)

#remove_motherBoolean

remove mother. Foreign_key set to nil

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


44
# File 'lib/genealogy/alter_methods.rb', line 44

generate_method_remove_parent(:mother)

#remove_parentsBoolean

remove both parents calling #remove_father and #remove_mother in a transaction


63
64
65
66
67
68
# File 'lib/genealogy/alter_methods.rb', line 63

def remove_parents
  transaction do
    remove_father
    remove_mother
  end
end

#remove_paternal_grandfatherBoolean

remove paternal grandfather

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


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

generate_method_remove_grandparent(:paternal,:father)

#remove_paternal_grandmotherBoolean

remove paternal grandmother

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


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

generate_method_remove_grandparent(:paternal,:mother)

#remove_paternal_grandparentsBoolean

remove paternal grandparents

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid


139
# File 'lib/genealogy/alter_methods.rb', line 139

(:paternal)

#remove_sibling(sib, options = {}) ⇒ Object

See Also:


240
241
242
# File 'lib/genealogy/alter_methods.rb', line 240

def remove_sibling(sib,options={})
  remove_siblings(sib,options)
end

#remove_siblings(*siblings, options = {}) ⇒ Boolean

remove siblings by nullifying parents of passed individuals

Options Hash (options):

  • half (Symbol)

    :father for paternal half siblings and :mother for maternal half siblings

  • remove_other_parent (Boolean)

    if specified, passed individuals' mother will also be nullified

Raises:

  • (ArgumentError)

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/genealogy/alter_methods.rb', line 214

def remove_siblings(*args)
  options = args.extract_options!
  raise ArgumentError.new("Unknown option value: half: #{options[:half]}.") if (options[:half] and ![:father,:mother].include?(options[:half]))
  resulting_indivs = if args.blank?
    siblings(options)
  else
    args & siblings(options)
  end
  transaction do
    resulting_indivs.each do |sib|
      case options[:half]
      when :father
        sib.remove_father
        sib.remove_mother if options[:remove_other_parent] == true
      when :mother
        sib.remove_father if options[:remove_other_parent] == true
        sib.remove_mother
      when nil
        sib.remove_parents
      end  
    end
  end
  !resulting_indivs.empty? #returned value must be true if self has at least a siblings to affect
end