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::OPPOSITELINEAGE, 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



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

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

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

add children by assigning self as parent

Parameters:

  • children (Object)

    list of children

  • options (Hash) (defaults to: {})

Options Hash (options):

  • spouse (Object)

    if specified, children will have that spouse

Returns:

  • (Boolean)


292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/genealogy/alter_methods.rb', line 292

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_before_type_cast
      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

Parameters:

  • parent (Object)

Returns:

  • (Boolean)

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

Parameters:

  • pgf (Object)

    paternal grandfather

  • pgm (Object)

    paternal grandmother

  • mgf (Object)

    maternal grandfather

  • mgm (Object)

    maternal grandmother

Returns:

  • (Boolean)

See Also:



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

Parameters:

  • gp (Object)

    grandparent

Returns:

  • (Boolean)

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

Parameters:

  • gp (Object)

    grandparent

Returns:

  • (Boolean)

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

Parameters:

  • gf (Object)

    grandfather

  • gm (Object)

    grandmother

Returns:

  • (Boolean)

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid



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

(:maternal)

#add_maternal_half_siblings(*args) ⇒ Object Also known as: add_maternal_half_sibling

See Also:



222
223
224
225
226
# File 'lib/genealogy/alter_methods.rb', line 222

def add_maternal_half_siblings(*args)
  options = args.extract_options!
  options[:half] = :mother
  add_siblings(*args,options)
end

#add_mother(parent) ⇒ Boolean

Add mother

Parameters:

  • parent (Object)

Returns:

  • (Boolean)

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

Parameters:

  • father (Object)
  • mother (Object)

Returns:

  • (Boolean)

See Also:



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

Parameters:

  • gp (Object)

    grandparent

Returns:

  • (Boolean)

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

Parameters:

  • gp (Object)

    grandparent

Returns:

  • (Boolean)

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

Parameters:

  • gf (Object)

    grandfather

  • gm (Object)

    grandmother

Returns:

  • (Boolean)

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid



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

(:paternal)

#add_paternal_half_siblings(*args) ⇒ Object Also known as: add_paternal_half_sibling

See Also:



215
216
217
218
219
# File 'lib/genealogy/alter_methods.rb', line 215

def add_paternal_half_siblings(*args)
  options = args.extract_options!
  options[:half] = :father
  add_siblings(*args,options)
end

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

See Also:



210
211
212
# File 'lib/genealogy/alter_methods.rb', line 210

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

Parameters:

  • siblings (Object)

    list of siblings

  • options (Hash) (defaults to: {})

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

Returns:

  • (Boolean)


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

def add_siblings(*args)
  options = args.extract_options!
  case options[:half]
  when :father
    check_incompatible_relationship(:paternal_half_sibling, *args)
  when :mother
    check_incompatible_relationship(:maternal_half_sibling, *args)
  when nil
    check_incompatible_relationship(:sibling, *args)
  end

  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



354
355
356
# File 'lib/genealogy/alter_methods.rb', line 354

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

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

remove children by nullifying the parent corresponding to self

Parameters:

  • children (Object)

    list of children

  • options (Hash) (defaults to: {})

Options Hash (options):

  • remove_other_parent (Boolean)

    if specified, passed individuals’ mother will also be nullified

Returns:

  • (Boolean)

    true if at least one child was affected, false otherwise



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/genealogy/alter_methods.rb', line 323

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_before_type_cast
        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

Returns:

  • (Boolean)

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

Returns:

  • (Boolean)

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

Returns:

  • (Boolean)

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

Returns:

  • (Boolean)

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid



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

(:maternal)

#remove_maternal_half_siblings(*args) ⇒ Object Also known as: remove_maternal_half_sibling

See Also:



276
277
278
279
280
# File 'lib/genealogy/alter_methods.rb', line 276

def remove_maternal_half_siblings(*args)
  options = args.extract_options!
  options[:half] = :mother
  remove_siblings(*args,options)
end

#remove_motherBoolean

remove mother. Foreign_key set to nil

Returns:

  • (Boolean)

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

Returns:

  • (Boolean)

See Also:



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

Returns:

  • (Boolean)

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

Returns:

  • (Boolean)

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

Returns:

  • (Boolean)

Raises:

  • (Exception)

    if perform validation is enabled and self is invalid



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

(:paternal)

#remove_paternal_half_siblings(*args) ⇒ Object Also known as: remove_paternal_half_sibling

See Also:



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

def remove_paternal_half_siblings(*args)
  options = args.extract_options!
  options[:half] = :father
  remove_siblings(*args,options)
end

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

See Also:



264
265
266
# File 'lib/genealogy/alter_methods.rb', line 264

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

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

remove siblings by nullifying parents of passed individuals

Parameters:

  • siblings (Object)

    list of siblings

  • options (Hash) (defaults to: {})

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

Returns:

  • (Boolean)

    true if at least one sibling was affected, false otherwise

Raises:

  • (ArgumentError)


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/genealogy/alter_methods.rb', line 238

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