genealogy 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YWNhZWM3Y2ZkOTg2ZTYwNzNjOTAyODQ5ODk3NTA1ZTk3NWU4Y2JlMA==
4
+ N2JhNjJiMTU0MmE0MGMwMmI1YTI2NjAyZWRhOGZhODRjMDlkMzUyMw==
5
5
  data.tar.gz: !binary |-
6
- YTljMTlmNDMzN2ZlNWFhNThmN2MzMmRiZWRlZGNmMGNlNGNiZWQ5YQ==
6
+ ZTU5ZWM1OWI2NWE5MDc1MmNlOTM1MDhlMzg3YTFmN2YxZjJkNTIyZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NjRmZjYwMjI2MTAxMzY4ODc3YjY2ZWZiZjRlNWYxMTdiMDJhMGVlNzlkYjY1
10
- Y2I2YjE3NjU3OGEwMGI2YzU5ODBlZmYxNGI1Mjc4YjQxZjU5NjIzZjAwNjRl
11
- YmM0NjlhZmQ3NzM0Y2E3MTNjZmI0Mzk4MWNlNWMyMGRmYmNlMTc=
9
+ MGIxNGE2YzUxNTA1ZmU4NzE3NGIwMTMxNzBkYjcxMWFmOGZmZGMzMGViNzg3
10
+ MGI5NmYwZGE1OTNlY2JhNDU0YzM2ZGUzMjNhMmU3YWFkODZjMzcwZmQwNWMz
11
+ YTM4MmEwN2ZmZDJmNmQ5OGMxMGFlMmJhYmMzYTM5YjMwNzIxNWU=
12
12
  data.tar.gz: !binary |-
13
- NGEyMzdmM2U0ODIzNzJlMzYyM2I1NjY4YjdhZGIxMjM4MjNhZjU4YjEwZTM3
14
- YzRmZjM1MTFkZWI2YWQyZjM5NDBlZDdhNGI5MzU4ZWFkZDI2ZTU4MzJkNDEw
15
- YzZlMzUwNTU5OGU2YzlmMjgyMGNlNTI1OWZhY2M1MjhlNTAyYjk=
13
+ NDljNjIxODUzMDBmZjgxNzk5Y2RlMTk0ZWI3NTMxYTM3MDcxNjMwMjQxY2Qx
14
+ M2IxZDNiNzg5MDJlMDFhZWEzYjY2ZWY2MDc3ZmJmZWM4MjZjODU3YTkxYTJm
15
+ ZGU5ZDNmM2MwOWExY2E5YzgxZWYwMTlhM2M4OTllYjlmMTdkNGU=
data/lib/genealogy.rb CHANGED
@@ -7,4 +7,3 @@ require File.join(File.expand_path(File.dirname(__FILE__)), 'genealogy/genealogy
7
7
 
8
8
  ActiveRecord::Base.send :extend, Genealogy
9
9
 
10
-
@@ -20,7 +20,7 @@ module Genealogy
20
20
  self.update_attribute(parent,relative)
21
21
  end
22
22
  end
23
-
23
+
24
24
  # remove method
25
25
  define_method "remove_#{parent}" do
26
26
  if perform_validation
@@ -127,7 +127,7 @@ module Genealogy
127
127
 
128
128
  def remove_siblings(*args)
129
129
  options = args.extract_options!
130
-
130
+
131
131
  raise WrongOptionException.new("Unknown option value: :half => #{options[:half]}.") if (options[:half] and ![:father,:mother].include?(options[:half]))
132
132
 
133
133
  resulting_indivs = if args.blank?
@@ -162,7 +162,7 @@ module Genealogy
162
162
  end
163
163
 
164
164
  [:father, :mother].each do |parent|
165
-
165
+
166
166
  # add paternal/maternal half_siblings
167
167
  define_method "add_#{Genealogy::PARENT2LINEAGE[parent]}_half_siblings" do | *args |
168
168
  options = args.extract_options!
@@ -170,7 +170,7 @@ module Genealogy
170
170
  args << options
171
171
  send("add_siblings",*args)
172
172
  end
173
-
173
+
174
174
  # add paternal/maternal half_sibling
175
175
  define_method "add_#{Genealogy::PARENT2LINEAGE[parent]}_half_sibling" do | sib,options={} |
176
176
  options[:half] = parent
@@ -192,11 +192,10 @@ module Genealogy
192
192
  end
193
193
  end
194
194
 
195
-
196
195
  # offspring
197
196
  def add_offspring(*args)
198
197
  options = args.extract_options!
199
-
198
+
200
199
  raise_if_sex_undefined
201
200
 
202
201
  transaction do
@@ -221,7 +220,7 @@ module Genealogy
221
220
 
222
221
  def remove_offspring(*args)
223
222
  options = args.extract_options!
224
-
223
+
225
224
  raise_if_sex_undefined
226
225
 
227
226
  resulting_indivs = if args.blank?
@@ -16,37 +16,38 @@ module Genealogy
16
16
 
17
17
  end
18
18
 
19
-
20
19
  def has_parents options = {}
21
20
 
22
- admitted_keys = [:sex_column, :sex_values, :father_column, :mother_column, :current_spouse_column, :current_spouse, :perform_validation]
21
+ admitted_keys = [:sex_column, :sex_values, :father_column, :mother_column, :current_spouse_column, :current_spouse, :birth_date_column, :death_date_column, :perform_validation]
23
22
  check_options(options, admitted_keys) do |key, value|
24
23
  if key == :sex_values
25
24
  raise WrongOptionException, ":sex_values option must be an array of length 2: first for male sex symbol an last for female" unless value.is_a?(Array) and value.size == 2
26
25
  end
27
26
  end
28
-
27
+
29
28
  class_attribute :genealogy_enabled, :current_spouse_enabled, :genealogy_class, :perform_validation
30
29
  self.genealogy_enabled = true
31
30
  self.current_spouse_enabled = options[:current_spouse].try(:==,true) || false # default false
32
31
  self.genealogy_class = self # keep track of the original extend class to prevent wrong scopes in query method in case of STI
33
32
  self.perform_validation = options[:perform_validation].try(:==,false) ? false : true # default true
34
-
33
+
35
34
  tracked_relatives = [:father, :mother]
36
35
  tracked_relatives << :current_spouse if current_spouse_enabled
37
36
 
38
37
  ## sex
39
38
  # class attributes
40
- class_attribute :sex_column, :sex_values, :sex_male_value, :sex_female_value
39
+ class_attribute :sex_column, :sex_values, :sex_male_value, :sex_female_value, :birth_date_column, :death_date_column
41
40
  self.sex_column = options[:sex_column] || 'sex'
42
41
  self.sex_values = options[:sex_values] || ['M','F']
43
42
  self.sex_male_value = self.sex_values.first
44
43
  self.sex_female_value = self.sex_values.last
44
+ self.birth_date_column = options[:birth_date_column] || 'birth_date'
45
+ self.death_date_column = options[:death_date_column] || 'death_date'
45
46
  # instance attribute
46
47
  alias_attribute :sex, sex_column if self.sex_column != 'sex'
47
48
  # validation
48
49
  validates_presence_of sex_column
49
- validates_format_of sex_column, :with => /[#{sex_values.join}]/
50
+ validates_format_of sex_column, :with => /[#{sex_values.join}]/
50
51
 
51
52
  tracked_relatives.each do |key|
52
53
  # class attribute where is stored the correspondig foreign_key column name
@@ -67,7 +68,7 @@ module Genealogy
67
68
  include Genealogy::SpouseMethods if current_spouse_enabled
68
69
 
69
70
  end
70
-
71
+
71
72
  module MotherAssociationExtension
72
73
  def with(father_id)
73
74
  where(father_id: father_id)
@@ -28,7 +28,7 @@ module Genealogy
28
28
  end
29
29
 
30
30
  # eligible
31
- define_method "eligible_#{Genealogy::PARENT2LINEAGE[parent]}_grand#{grandparent}s" do
31
+ define_method "eligible_#{Genealogy::PARENT2LINEAGE[parent]}_grand#{grandparent}s" do
32
32
  if send(parent) and send("#{Genealogy::PARENT2LINEAGE[parent]}_grand#{grandparent}").nil?
33
33
  send(parent).send("eligible_#{grandparent}s") - [self]
34
34
  else
@@ -63,7 +63,7 @@ module Genealogy
63
63
  # offspring
64
64
  def offspring(options = {})
65
65
  if spouse = options[:spouse]
66
- raise WrongSexException, "Problems while looking for #{self}'s offspring made with spouse #{spouse} who should not be a #{spouse.sex}." if spouse.sex == sex
66
+ raise WrongSexException, "Problems while looking for #{self}'s offspring made with spouse #{spouse} who should not be a #{spouse.sex}." if spouse.sex == sex
67
67
  end
68
68
  result = case sex
69
69
  when sex_male_value
@@ -129,7 +129,7 @@ module Genealogy
129
129
 
130
130
  def half_siblings
131
131
  siblings(:half => :only)
132
- # todo: inprove with option :father and :mother
132
+ # todo: inprove with option :father and :mother
133
133
  end
134
134
 
135
135
  def paternal_half_siblings
@@ -242,7 +242,7 @@ module Genealogy
242
242
  nieces_and_nephews(options.merge({sex: :female}), sibling_options)
243
243
  end
244
244
 
245
- def family(options = {})
245
+ def family(options = {})
246
246
  res = [self] | siblings | parents | offspring
247
247
  res |= [current_spouse] if self.class.current_spouse_enabled
248
248
  res |= case options[:half]
@@ -258,13 +258,13 @@ module Genealogy
258
258
  raise WrongOptionValueException, "Admitted values for :half options are: :father, :mother, :include, nil"
259
259
  end
260
260
  res = offspring.inject(res){|memo,child| memo |= child.parents} #add spouses
261
-
261
+
262
262
  res += [grandparents + grandchildren + uncles_and_aunts + nieces_and_nephews].flatten if options[:extended]
263
263
 
264
264
  res.compact
265
265
  end
266
266
 
267
- def family_hash(options = {})
267
+ def family_hash(options = {})
268
268
  roles = [:father, :mother, :children, :siblings]
269
269
  roles += [:current_spouse] if self.class.current_spouse_enabled
270
270
  roles += case options[:half]
@@ -285,11 +285,11 @@ module Genealogy
285
285
  h
286
286
  end
287
287
 
288
- def extended_family(options = {})
288
+ def extended_family(options = {})
289
289
  family(options.merge(:extended => true))
290
290
  end
291
291
 
292
- def extended_family_hash(options = {})
292
+ def extended_family_hash(options = {})
293
293
  family_hash(options.merge(:extended => true))
294
294
  end
295
295
 
@@ -299,7 +299,7 @@ module Genealogy
299
299
  'male'
300
300
  when sex_female_value
301
301
  'female'
302
- else
302
+ else
303
303
  raise WrongSexException, "Sex value not valid for #{self}"
304
304
  end
305
305
  end
@@ -311,7 +311,36 @@ module Genealogy
311
311
 
312
312
  def is_male?
313
313
  return male? if respond_to?(:male?)
314
- sex == sex_male_value
314
+ sex == sex_male_value
315
+ end
316
+
317
+ def birth
318
+ self.send("#{genealogy_class.birth_date_column}")
319
+ end
320
+
321
+ def death
322
+ self.send("#{genealogy_class.death_date_column}")
323
+ end
324
+
325
+ def age(options={})
326
+ birth_date = birth
327
+ death_date = death
328
+ return if birth_date.nil?
329
+
330
+ current = options[:end_date] ? DateTime.parse(options[:end_date]) : death_date || Time.zone.now
331
+ years = current.year - birth_date.year
332
+
333
+ if options[:measurement] == :years || !options[:measurement]
334
+ return options[:string] ? "#{years} years" : years
335
+ end
336
+
337
+ months = current.month - birth_date.month
338
+ months += 12 if months < 0
339
+
340
+ if options[:measurement] == :months
341
+ return options[:string] ? "#{years} years and #{months} months" : (years * 12) + months
342
+ end
343
+ return years
315
344
  end
316
345
 
317
346
  module ClassMethods
@@ -1,3 +1,3 @@
1
1
  module Genealogy
2
- VERSION = "1.4.0"
2
+ VERSION = "1.5.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genealogy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - masciugo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-25 00:00:00.000000000 Z
11
+ date: 2014-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -144,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
144
  version: '0'
145
145
  requirements: []
146
146
  rubyforge_project: ! '[none]'
147
- rubygems_version: 2.3.0
147
+ rubygems_version: 2.1.10
148
148
  signing_key:
149
149
  specification_version: 4
150
150
  summary: Organise ActiveRecord models into a genealogical tree structure