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
- .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
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.generate_method_add_grandparents_by_lineage(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.generate_method_remove_grandparents_by_lineage(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,={}) add_children(child,) end |
#add_children(*children, options = {}) ⇒ Boolean
add children by assigning self as parent
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) = 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
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
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
85 |
# File 'lib/genealogy/alter_methods.rb', line 85 generate_method_add_grandparent(:maternal,:father) |
#add_maternal_grandmother(grandparent) ⇒ Boolean
Add maternal grandmother
86 |
# File 'lib/genealogy/alter_methods.rb', line 86 generate_method_add_grandparent(:maternal,:mother) |
#add_maternal_grandparents ⇒ Boolean
Add maternal grandparents
122 |
# File 'lib/genealogy/alter_methods.rb', line 122 generate_method_add_grandparents_by_lineage(:maternal) |
#add_maternal_half_siblings(*args) ⇒ Object Also known as: add_maternal_half_sibling
220 221 222 223 224 |
# File 'lib/genealogy/alter_methods.rb', line 220 def add_maternal_half_siblings(*args) = args. [:half] = :mother add_siblings(*args,) end |
#add_mother(parent) ⇒ Boolean
Add mother
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
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
83 |
# File 'lib/genealogy/alter_methods.rb', line 83 generate_method_add_grandparent(:paternal,:father) |
#add_paternal_grandmother(grandparent) ⇒ Boolean
Add paternal grandmother
84 |
# File 'lib/genealogy/alter_methods.rb', line 84 generate_method_add_grandparent(:paternal,:mother) |
#add_paternal_grandparents ⇒ Boolean
Add paternal grandparents
121 |
# File 'lib/genealogy/alter_methods.rb', line 121 generate_method_add_grandparents_by_lineage(:paternal) |
#add_paternal_half_siblings(*args) ⇒ Object Also known as: add_paternal_half_sibling
213 214 215 216 217 |
# File 'lib/genealogy/alter_methods.rb', line 213 def add_paternal_half_siblings(*args) = args. [:half] = :father add_siblings(*args,) end |
#add_sibling(sibling, options = {}) ⇒ Object
208 209 210 |
# File 'lib/genealogy/alter_methods.rb', line 208 def add_sibling(sibling,={}) add_siblings(sibling,) end |
#add_siblings(*siblings, options = {}) ⇒ Boolean
add siblings by assigning same parents to individuals passed as arguments
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) = 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
352 353 354 |
# File 'lib/genealogy/alter_methods.rb', line 352 def remove_child(child,={}) remove_children(child,) end |
#remove_children(*children, options = {}) ⇒ Boolean
remove children by nullifying the parent corresponding to self
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) = 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
41 |
# File 'lib/genealogy/alter_methods.rb', line 41 generate_method_remove_parent(:father) |
#remove_grandparents ⇒ Boolean
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_grandfather ⇒ Boolean
remove maternal grandfather
103 |
# File 'lib/genealogy/alter_methods.rb', line 103 generate_method_remove_grandparent(:maternal,:father) |
#remove_maternal_grandmother ⇒ Boolean
remove maternal grandmother
104 |
# File 'lib/genealogy/alter_methods.rb', line 104 generate_method_remove_grandparent(:maternal,:mother) |
#remove_maternal_grandparents ⇒ Boolean
remove maternal grandparents
138 |
# File 'lib/genealogy/alter_methods.rb', line 138 generate_method_remove_grandparents_by_lineage(:maternal) |
#remove_maternal_half_siblings(*args) ⇒ Object Also known as: remove_maternal_half_sibling
274 275 276 277 278 |
# File 'lib/genealogy/alter_methods.rb', line 274 def remove_maternal_half_siblings(*args) = args. [:half] = :mother remove_siblings(*args,) end |
#remove_mother ⇒ Boolean
remove mother. Foreign_key set to nil
42 |
# File 'lib/genealogy/alter_methods.rb', line 42 generate_method_remove_parent(:mother) |
#remove_parents ⇒ Boolean
remove both parents calling #remove_father and #remove_mother in a transaction
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_grandfather ⇒ Boolean
remove paternal grandfather
101 |
# File 'lib/genealogy/alter_methods.rb', line 101 generate_method_remove_grandparent(:paternal,:father) |
#remove_paternal_grandmother ⇒ Boolean
remove paternal grandmother
102 |
# File 'lib/genealogy/alter_methods.rb', line 102 generate_method_remove_grandparent(:paternal,:mother) |
#remove_paternal_grandparents ⇒ Boolean
remove paternal grandparents
137 |
# File 'lib/genealogy/alter_methods.rb', line 137 generate_method_remove_grandparents_by_lineage(:paternal) |
#remove_paternal_half_siblings(*args) ⇒ Object Also known as: remove_paternal_half_sibling
267 268 269 270 271 |
# File 'lib/genealogy/alter_methods.rb', line 267 def remove_paternal_half_siblings(*args) = args. [:half] = :father remove_siblings(*args,) end |
#remove_sibling(sib, options = {}) ⇒ Object
262 263 264 |
# File 'lib/genealogy/alter_methods.rb', line 262 def remove_sibling(sib,={}) remove_siblings(sib,) end |
#remove_siblings(*siblings, options = {}) ⇒ Boolean
remove siblings by nullifying parents of passed individuals
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) = 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 |