Class: Gedcomx::Date

Inherits:
Object
  • Object
show all
Defined in:
lib/gedcomx/date.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = nil) ⇒ Date

Returns a new instance of Date.



47
48
49
50
51
# File 'lib/gedcomx/date.rb', line 47

def initialize(input = nil)
  @date = input || self.class.java_class.new
  @fields = []
  @fields = @date.fields.map { |field| Gedcomx::Field.new(field) } if @date.fields
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



4
5
6
# File 'lib/gedcomx/date.rb', line 4

def fields
  @fields
end

Class Method Details

.create(attributes = {}) ⇒ Object



10
11
12
13
14
15
# File 'lib/gedcomx/date.rb', line 10

def self.create(attributes = {})
  new_date = self.new
  new_date.original = attributes[:original] if attributes[:original]
  attributes[:fields].each { |field| new_date.add_field(field) } if attributes[:fields].is_a? Array
  new_date
end

.create_simple(attributes = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gedcomx/date.rb', line 17

def self.create_simple(attributes = {})
  day = attributes[:day]
  month = attributes[:month]
  year = attributes[:year]

  return nil unless ( day || month || year )
  new_fields = []
  full_string = ''

  if day
    full_string += day
    new_fields << Gedcomx::Field.create(values: [ { text: day, type: Gedcomx::TYPES[:day] } ])
  end

  if month
    full_string += ' ' unless full_string.empty?
    full_string += month
    new_fields << Gedcomx::Field.create(values: [ { text: month, type: Gedcomx::TYPES[:month] } ])
  end

  if year
    full_string += ' ' unless full_string.empty?
    full_string += year
    new_fields << Gedcomx::Field.create(values: [ { text: year, type: Gedcomx::TYPES[:year] } ])
  end

  new_fields << Gedcomx::Field.create(values: [ { text: full_string, type: Gedcomx::TYPES[:original] } ])
  self.create(fields: new_fields)
end

.java_classObject



6
7
8
# File 'lib/gedcomx/date.rb', line 6

def self.java_class
  Java::OrgGedcomxConclusion::Date
end

Instance Method Details

#add_field(field) ⇒ Object



68
69
70
71
72
# File 'lib/gedcomx/date.rb', line 68

def add_field(field)
  return false unless field.is_a? Gedcomx::Field
  @date.add_field field.to_java
  @fields << field
end

#dayObject



53
54
55
56
# File 'lib/gedcomx/date.rb', line 53

def day
  day_field = @fields.select { |field| field.type == Gedcomx::TYPES[:day] }.first
  day_field.values.first.text if day_field
end

#monthObject



58
59
60
61
# File 'lib/gedcomx/date.rb', line 58

def month
  month_field = @fields.select { |field| field.type == Gedcomx::TYPES[:month] }.first
  month_field.values.first.text if month_field
end

#originalObject



74
75
76
# File 'lib/gedcomx/date.rb', line 74

def original
  @date.original
end

#original=(input_string) ⇒ Object



78
79
80
81
# File 'lib/gedcomx/date.rb', line 78

def original=(input_string)
  return false unless input_string.is_a? String
  @date.original = input_string
end

#to_javaObject



83
84
85
# File 'lib/gedcomx/date.rb', line 83

def to_java
  @date
end

#yearObject



63
64
65
66
# File 'lib/gedcomx/date.rb', line 63

def year
  year_field = @fields.select { |field| field.type == Gedcomx::TYPES[:year] }.first
  year_field.values.first.text if year_field
end