Module: Genealogy::AlterMethods
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
- .generate_method_add_grandparent(lineage, grandparent) ⇒ Object
- .generate_method_add_grandparents_by_lineage(lineage) ⇒ Object
- .generate_method_add_parent(parent) ⇒ Object
- .generate_method_remove_grandparent(lineage, grandparent) ⇒ Object
- .generate_method_remove_grandparents_by_lineage(lineage) ⇒ Object
- .generate_method_remove_parent(parent) ⇒ Object
Instance Method Summary collapse
-
#add_child(child, options = {}) ⇒ Object
see #add_children.
-
#add_children(*children, options = {}) ⇒ Boolean
add children by assigning self as parent.
-
#add_father(parent) ⇒ Boolean
Add father.
-
#add_grandparents(pgf, pgm, mgf, mgm) ⇒ Boolean
add all grandparents calling #add_paternal_grandparents and #add_maternal_grandparents in a transaction.
-
#add_maternal_grandfather(grandparent) ⇒ Boolean
Add maternal grandfather.
-
#add_maternal_grandmother(grandparent) ⇒ Boolean
Add maternal grandmother.
-
#add_maternal_grandparents ⇒ Boolean
Add maternal grandparents.
- #add_maternal_half_siblings(*args) ⇒ Object (also: #add_maternal_half_sibling)
-
#add_mother(parent) ⇒ Boolean
Add mother.
-
#add_parents(father, mother) ⇒ Boolean
add both parents calling #add_father and #add_mother in a transaction.
-
#add_paternal_grandfather(grandparent) ⇒ Boolean
Add paternal grandfather.
-
#add_paternal_grandmother(grandparent) ⇒ Boolean
Add paternal grandmother.
-
#add_paternal_grandparents ⇒ Boolean
Add paternal grandparents.
- #add_paternal_half_siblings(*args) ⇒ Object (also: #add_paternal_half_sibling)
- #add_sibling(sibling, options = {}) ⇒ Object
-
#add_siblings(*siblings, options = {}) ⇒ Boolean
add siblings by assigning same parents to individuals passed as arguments.
-
#remove_child(child, options = {}) ⇒ Object
see #remove_children.
-
#remove_children(*children, options = {}) ⇒ Boolean
remove children by nullifying the parent corresponding to self.
-
#remove_father ⇒ Boolean
remove father.
-
#remove_grandparents ⇒ Boolean
remove all grandparents calling #remove_paternal_grandparents and #remove_maternal_grandparents in a transaction.
-
#remove_maternal_grandfather ⇒ Boolean
remove maternal grandfather.
-
#remove_maternal_grandmother ⇒ Boolean
remove maternal grandmother.
-
#remove_maternal_grandparents ⇒ Boolean
remove maternal grandparents.
- #remove_maternal_half_siblings(*args) ⇒ Object (also: #remove_maternal_half_sibling)
-
#remove_mother ⇒ Boolean
remove mother.
-
#remove_parents ⇒ Boolean
remove both parents calling #remove_father and #remove_mother in a transaction.
-
#remove_paternal_grandfather ⇒ Boolean
remove paternal grandfather.
-
#remove_paternal_grandmother ⇒ Boolean
remove paternal grandmother.
-
#remove_paternal_grandparents ⇒ Boolean
remove paternal grandparents.
- #remove_paternal_half_siblings(*args) ⇒ Object (also: #remove_paternal_half_sibling)
- #remove_sibling(sib, options = {}) ⇒ Object
-
#remove_siblings(*siblings, options = {}) ⇒ Boolean
remove siblings by nullifying parents of passed individuals.
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.generate_method_add_grandparents_by_lineage(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.generate_method_remove_grandparents_by_lineage(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,={}) add_children(child,) end |
#add_children(*children, options = {}) ⇒ Boolean
add children by assigning self as parent
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) = args. 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([:spouse]) if [:spouse] child.add_father(self) when gclass.sex_female_value child.add_father([:spouse]) if [:spouse] child.add_mother(self) else raise SexError, "Sex value not valid for #{self}" end end end end |
#add_father(parent) ⇒ Boolean
Add father
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
87 |
# File 'lib/genealogy/alter_methods.rb', line 87 generate_method_add_grandparent(:maternal,:father) |
#add_maternal_grandmother(grandparent) ⇒ Boolean
Add maternal grandmother
88 |
# File 'lib/genealogy/alter_methods.rb', line 88 generate_method_add_grandparent(:maternal,:mother) |
#add_maternal_grandparents ⇒ Boolean
Add maternal grandparents
124 |
# File 'lib/genealogy/alter_methods.rb', line 124 generate_method_add_grandparents_by_lineage(:maternal) |
#add_maternal_half_siblings(*args) ⇒ Object Also known as: add_maternal_half_sibling
222 223 224 225 226 |
# File 'lib/genealogy/alter_methods.rb', line 222 def add_maternal_half_siblings(*args) = args. [:half] = :mother add_siblings(*args,) end |
#add_mother(parent) ⇒ Boolean
Add mother
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
85 |
# File 'lib/genealogy/alter_methods.rb', line 85 generate_method_add_grandparent(:paternal,:father) |
#add_paternal_grandmother(grandparent) ⇒ Boolean
Add paternal grandmother
86 |
# File 'lib/genealogy/alter_methods.rb', line 86 generate_method_add_grandparent(:paternal,:mother) |
#add_paternal_grandparents ⇒ Boolean
Add paternal grandparents
123 |
# File 'lib/genealogy/alter_methods.rb', line 123 generate_method_add_grandparents_by_lineage(:paternal) |
#add_paternal_half_siblings(*args) ⇒ Object Also known as: add_paternal_half_sibling
215 216 217 218 219 |
# File 'lib/genealogy/alter_methods.rb', line 215 def add_paternal_half_siblings(*args) = args. [:half] = :father add_siblings(*args,) end |
#add_sibling(sibling, options = {}) ⇒ Object
210 211 212 |
# File 'lib/genealogy/alter_methods.rb', line 210 def add_sibling(sibling,={}) add_siblings(sibling,) end |
#add_siblings(*siblings, options = {}) ⇒ Boolean
add siblings by assigning same parents to individuals passed as arguments
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) = args. case [: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 [:half] when :father raise LineageGapException, "Can't add paternal halfsiblings without a father" unless father sib.add_mother([:spouse]) if [:spouse] sib.add_father(father) when :mother raise LineageGapException, "Can't add maternal halfsiblings without a mother" unless mother sib.add_father([:spouse]) if [: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,={}) remove_children(child,) end |
#remove_children(*children, options = {}) ⇒ Boolean
remove children by nullifying the parent corresponding to self
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) = args. raise_if_sex_undefined resulting_indivs = if args.blank? children() else args & children() end transaction do resulting_indivs.each do |child| if [: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_father ⇒ Boolean
remove father. Foreign_key set to nil
43 |
# File 'lib/genealogy/alter_methods.rb', line 43 generate_method_remove_parent(:father) |
#remove_grandparents ⇒ Boolean
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_grandfather ⇒ Boolean
remove maternal grandfather
105 |
# File 'lib/genealogy/alter_methods.rb', line 105 generate_method_remove_grandparent(:maternal,:father) |
#remove_maternal_grandmother ⇒ Boolean
remove maternal grandmother
106 |
# File 'lib/genealogy/alter_methods.rb', line 106 generate_method_remove_grandparent(:maternal,:mother) |
#remove_maternal_grandparents ⇒ Boolean
remove maternal grandparents
140 |
# File 'lib/genealogy/alter_methods.rb', line 140 generate_method_remove_grandparents_by_lineage(:maternal) |
#remove_maternal_half_siblings(*args) ⇒ Object Also known as: remove_maternal_half_sibling
276 277 278 279 280 |
# File 'lib/genealogy/alter_methods.rb', line 276 def remove_maternal_half_siblings(*args) = args. [:half] = :mother remove_siblings(*args,) end |
#remove_mother ⇒ Boolean
remove mother. Foreign_key set to nil
44 |
# File 'lib/genealogy/alter_methods.rb', line 44 generate_method_remove_parent(:mother) |
#remove_parents ⇒ Boolean
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_grandfather ⇒ Boolean
remove paternal grandfather
103 |
# File 'lib/genealogy/alter_methods.rb', line 103 generate_method_remove_grandparent(:paternal,:father) |
#remove_paternal_grandmother ⇒ Boolean
remove paternal grandmother
104 |
# File 'lib/genealogy/alter_methods.rb', line 104 generate_method_remove_grandparent(:paternal,:mother) |
#remove_paternal_grandparents ⇒ Boolean
remove paternal grandparents
139 |
# File 'lib/genealogy/alter_methods.rb', line 139 generate_method_remove_grandparents_by_lineage(:paternal) |
#remove_paternal_half_siblings(*args) ⇒ Object Also known as: remove_paternal_half_sibling
269 270 271 272 273 |
# File 'lib/genealogy/alter_methods.rb', line 269 def remove_paternal_half_siblings(*args) = args. [:half] = :father remove_siblings(*args,) end |
#remove_sibling(sib, options = {}) ⇒ Object
264 265 266 |
# File 'lib/genealogy/alter_methods.rb', line 264 def remove_sibling(sib,={}) remove_siblings(sib,) end |
#remove_siblings(*siblings, options = {}) ⇒ Boolean
remove siblings by nullifying parents of passed individuals
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) = args. raise ArgumentError.new("Unknown option value: half: #{[:half]}.") if ([:half] and ![:father,:mother].include?([:half])) resulting_indivs = if args.blank? siblings() else args & siblings() end transaction do resulting_indivs.each do |sib| case [:half] when :father sib.remove_father sib.remove_mother if [:remove_other_parent] == true when :mother sib.remove_father if [: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 |