Module: Genealogy::AlterMethods

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

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_method_add_grandparent(lineage, grandparent) ⇒ Object



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

def self.generate_method_add_grandparent(lineage,grandparent)
  relationship = "#{lineage}_grand#{grandparent}"
  define_method "add_#{relationship}" do |gp|
    parent = gclass::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



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

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

.generate_method_add_parent(parent) ⇒ Object



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

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



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

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

.generate_method_remove_grandparents_by_lineage(lineage) ⇒ Object



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

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

.generate_method_remove_parent(parent) ⇒ Object



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

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



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

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)


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

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



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

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:



149
150
151
152
153
154
# File 'lib/genealogy/alter_methods.rb', line 149

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



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

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



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

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



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

(:maternal)

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

See Also:



220
221
222
223
224
# File 'lib/genealogy/alter_methods.rb', line 220

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



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

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:



50
51
52
53
54
55
# File 'lib/genealogy/alter_methods.rb', line 50

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



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

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



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

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



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

(:paternal)

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

See Also:



213
214
215
216
217
# File 'lib/genealogy/alter_methods.rb', line 213

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:



208
209
210
# File 'lib/genealogy/alter_methods.rb', line 208

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)


174
175
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
# File 'lib/genealogy/alter_methods.rb', line 174

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



352
353
354
# File 'lib/genealogy/alter_methods.rb', line 352

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



321
322
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
# File 'lib/genealogy/alter_methods.rb', line 321

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



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

generate_method_remove_parent(:father)

#remove_grandparentsBoolean

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



160
161
162
163
164
165
# File 'lib/genealogy/alter_methods.rb', line 160

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



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

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



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

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



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

(:maternal)

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

See Also:



274
275
276
277
278
# File 'lib/genealogy/alter_methods.rb', line 274

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



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

generate_method_remove_parent(:mother)

#remove_parentsBoolean

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

Returns:

  • (Boolean)

See Also:



61
62
63
64
65
66
# File 'lib/genealogy/alter_methods.rb', line 61

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



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

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



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

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



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

(:paternal)

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

See Also:



267
268
269
270
271
# File 'lib/genealogy/alter_methods.rb', line 267

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:



262
263
264
# File 'lib/genealogy/alter_methods.rb', line 262

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)


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

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