Class: Datacite::Mapping::Date

Inherits:
Object
  • Object
show all
Includes:
XML::Mapping
Defined in:
lib/datacite/mapping/date.rb

Overview

Represents a DataCite <date/> field, which can be a year, date (year-month-day or just year-month), or ISO8601 datetime.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, value:) ⇒ Date

Initializes a new Date

Parameters:

  • type (DateType)

    the type of date. Cannot be nil.

  • value (DateTime, Date, Integer, String)

    The value, as a DateTime, Date, or Integer, or as a String in any W3C DateTime format



70
71
72
73
# File 'lib/datacite/mapping/date.rb', line 70

def initialize(type:, value:)
  self.type = type
  self.value = value
end

Instance Attribute Details

#dayInteger? (readonly)

Returns The day. Can be nil if no day was specified.

Returns:

  • (Integer, nil)

    The day. Can be nil if no day was specified.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/datacite/mapping/date.rb', line 54

class Date
  include XML::Mapping

  attr_reader :year
  attr_reader :month
  attr_reader :day
  attr_reader :hour
  attr_reader :minute
  attr_reader :sec
  attr_reader :nsec

  # Initializes a new `Date`
  #
  # @param type [DateType] the type of date. Cannot be nil.
  # @param value [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def initialize(type:, value:)
    self.type = type
    self.value = value
  end

  def type=(val)
    fail ArgumentError, 'Date type cannot be nil' unless val
    @type = val
  end

  # Sets the value.
  #
  # @param val [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def value=(val) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
    @date_time = to_datetime(val)
    @date = @date_time ? @date_time.to_date : to_date(val)
    @year = to_year(val)
    @month = to_month(val)
    @day = to_day(val)
    new_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
    if new_value.include?('T')
      @hour = @date_time.hour if @date_time
      @minute = @date_time.minute if @date_time
      @sec = @date_time.sec if @date_time
      @nsec = @date_time.to_time.nsec if @date_time
    end
    fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
    @value = new_value
  end

  private

  def to_year(val)
    return val if val.is_a?(Integer)
    return val.year if val.respond_to?(:year)
    matchdata = val.to_s.match(/^[0-9]+/)
    matchdata[0].to_i if matchdata
  end

  def to_month(val)
    return val.month if val.respond_to?(:month)
    matchdata = val.to_s.match(/^[0-9]+-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_day(val)
    return val.day if val.respond_to?(:day)
    matchdata = val.to_s.match(/^[0-9]+-[0-9]{2}-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_datetime(val)
    return val if val.is_a?(DateTime)
    DateTime.parse(val.to_s)
  rescue ArgumentError => e
    Mapping.log.debug("Can't extract DateTime from date value '#{val}': #{e}")
    nil
  end

  def to_date(val)
    return val if val.is_a?(::Date)
    return ::Date.parse(val.iso8601) if val.respond_to?(:iso8601)
    ::Date.parse(val.to_s)
  rescue ArgumentError => e
    Mapping.log.debug("Can't extract Date from date value '#{val}': #{e}")
    nil
  end

  # @!attribute [rw] type
  #  @return [DateType] the type of date. Cannot be nil.
  typesafe_enum_node :type, '@dateType', class: DateType

  # @!method value
  #   @return [String] The value as a string. May be any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime).
  text_node :value, 'text()'
end

#hourInteger? (readonly)

Returns The hour. Can be nil if no hour was specified.

Returns:

  • (Integer, nil)

    The hour. Can be nil if no hour was specified.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/datacite/mapping/date.rb', line 54

class Date
  include XML::Mapping

  attr_reader :year
  attr_reader :month
  attr_reader :day
  attr_reader :hour
  attr_reader :minute
  attr_reader :sec
  attr_reader :nsec

  # Initializes a new `Date`
  #
  # @param type [DateType] the type of date. Cannot be nil.
  # @param value [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def initialize(type:, value:)
    self.type = type
    self.value = value
  end

  def type=(val)
    fail ArgumentError, 'Date type cannot be nil' unless val
    @type = val
  end

  # Sets the value.
  #
  # @param val [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def value=(val) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
    @date_time = to_datetime(val)
    @date = @date_time ? @date_time.to_date : to_date(val)
    @year = to_year(val)
    @month = to_month(val)
    @day = to_day(val)
    new_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
    if new_value.include?('T')
      @hour = @date_time.hour if @date_time
      @minute = @date_time.minute if @date_time
      @sec = @date_time.sec if @date_time
      @nsec = @date_time.to_time.nsec if @date_time
    end
    fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
    @value = new_value
  end

  private

  def to_year(val)
    return val if val.is_a?(Integer)
    return val.year if val.respond_to?(:year)
    matchdata = val.to_s.match(/^[0-9]+/)
    matchdata[0].to_i if matchdata
  end

  def to_month(val)
    return val.month if val.respond_to?(:month)
    matchdata = val.to_s.match(/^[0-9]+-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_day(val)
    return val.day if val.respond_to?(:day)
    matchdata = val.to_s.match(/^[0-9]+-[0-9]{2}-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_datetime(val)
    return val if val.is_a?(DateTime)
    DateTime.parse(val.to_s)
  rescue ArgumentError => e
    Mapping.log.debug("Can't extract DateTime from date value '#{val}': #{e}")
    nil
  end

  def to_date(val)
    return val if val.is_a?(::Date)
    return ::Date.parse(val.iso8601) if val.respond_to?(:iso8601)
    ::Date.parse(val.to_s)
  rescue ArgumentError => e
    Mapping.log.debug("Can't extract Date from date value '#{val}': #{e}")
    nil
  end

  # @!attribute [rw] type
  #  @return [DateType] the type of date. Cannot be nil.
  typesafe_enum_node :type, '@dateType', class: DateType

  # @!method value
  #   @return [String] The value as a string. May be any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime).
  text_node :value, 'text()'
end

#minuteInteger? (readonly)

Returns The minutes. Can be nil if no minutes were specified.

Returns:

  • (Integer, nil)

    The minutes. Can be nil if no minutes were specified.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/datacite/mapping/date.rb', line 54

class Date
  include XML::Mapping

  attr_reader :year
  attr_reader :month
  attr_reader :day
  attr_reader :hour
  attr_reader :minute
  attr_reader :sec
  attr_reader :nsec

  # Initializes a new `Date`
  #
  # @param type [DateType] the type of date. Cannot be nil.
  # @param value [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def initialize(type:, value:)
    self.type = type
    self.value = value
  end

  def type=(val)
    fail ArgumentError, 'Date type cannot be nil' unless val
    @type = val
  end

  # Sets the value.
  #
  # @param val [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def value=(val) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
    @date_time = to_datetime(val)
    @date = @date_time ? @date_time.to_date : to_date(val)
    @year = to_year(val)
    @month = to_month(val)
    @day = to_day(val)
    new_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
    if new_value.include?('T')
      @hour = @date_time.hour if @date_time
      @minute = @date_time.minute if @date_time
      @sec = @date_time.sec if @date_time
      @nsec = @date_time.to_time.nsec if @date_time
    end
    fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
    @value = new_value
  end

  private

  def to_year(val)
    return val if val.is_a?(Integer)
    return val.year if val.respond_to?(:year)
    matchdata = val.to_s.match(/^[0-9]+/)
    matchdata[0].to_i if matchdata
  end

  def to_month(val)
    return val.month if val.respond_to?(:month)
    matchdata = val.to_s.match(/^[0-9]+-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_day(val)
    return val.day if val.respond_to?(:day)
    matchdata = val.to_s.match(/^[0-9]+-[0-9]{2}-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_datetime(val)
    return val if val.is_a?(DateTime)
    DateTime.parse(val.to_s)
  rescue ArgumentError => e
    Mapping.log.debug("Can't extract DateTime from date value '#{val}': #{e}")
    nil
  end

  def to_date(val)
    return val if val.is_a?(::Date)
    return ::Date.parse(val.iso8601) if val.respond_to?(:iso8601)
    ::Date.parse(val.to_s)
  rescue ArgumentError => e
    Mapping.log.debug("Can't extract Date from date value '#{val}': #{e}")
    nil
  end

  # @!attribute [rw] type
  #  @return [DateType] the type of date. Cannot be nil.
  typesafe_enum_node :type, '@dateType', class: DateType

  # @!method value
  #   @return [String] The value as a string. May be any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime).
  text_node :value, 'text()'
end

#monthInteger? (readonly)

Returns The month. Can be nil if no month was specified.

Returns:

  • (Integer, nil)

    The month. Can be nil if no month was specified.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/datacite/mapping/date.rb', line 54

class Date
  include XML::Mapping

  attr_reader :year
  attr_reader :month
  attr_reader :day
  attr_reader :hour
  attr_reader :minute
  attr_reader :sec
  attr_reader :nsec

  # Initializes a new `Date`
  #
  # @param type [DateType] the type of date. Cannot be nil.
  # @param value [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def initialize(type:, value:)
    self.type = type
    self.value = value
  end

  def type=(val)
    fail ArgumentError, 'Date type cannot be nil' unless val
    @type = val
  end

  # Sets the value.
  #
  # @param val [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def value=(val) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
    @date_time = to_datetime(val)
    @date = @date_time ? @date_time.to_date : to_date(val)
    @year = to_year(val)
    @month = to_month(val)
    @day = to_day(val)
    new_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
    if new_value.include?('T')
      @hour = @date_time.hour if @date_time
      @minute = @date_time.minute if @date_time
      @sec = @date_time.sec if @date_time
      @nsec = @date_time.to_time.nsec if @date_time
    end
    fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
    @value = new_value
  end

  private

  def to_year(val)
    return val if val.is_a?(Integer)
    return val.year if val.respond_to?(:year)
    matchdata = val.to_s.match(/^[0-9]+/)
    matchdata[0].to_i if matchdata
  end

  def to_month(val)
    return val.month if val.respond_to?(:month)
    matchdata = val.to_s.match(/^[0-9]+-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_day(val)
    return val.day if val.respond_to?(:day)
    matchdata = val.to_s.match(/^[0-9]+-[0-9]{2}-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_datetime(val)
    return val if val.is_a?(DateTime)
    DateTime.parse(val.to_s)
  rescue ArgumentError => e
    Mapping.log.debug("Can't extract DateTime from date value '#{val}': #{e}")
    nil
  end

  def to_date(val)
    return val if val.is_a?(::Date)
    return ::Date.parse(val.iso8601) if val.respond_to?(:iso8601)
    ::Date.parse(val.to_s)
  rescue ArgumentError => e
    Mapping.log.debug("Can't extract Date from date value '#{val}': #{e}")
    nil
  end

  # @!attribute [rw] type
  #  @return [DateType] the type of date. Cannot be nil.
  typesafe_enum_node :type, '@dateType', class: DateType

  # @!method value
  #   @return [String] The value as a string. May be any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime).
  text_node :value, 'text()'
end

#nsecInteger? (readonly)

Returns The nanoseconds. Can be nil if no nanoseconds were specified.

Returns:

  • (Integer, nil)

    The nanoseconds. Can be nil if no nanoseconds were specified.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/datacite/mapping/date.rb', line 54

class Date
  include XML::Mapping

  attr_reader :year
  attr_reader :month
  attr_reader :day
  attr_reader :hour
  attr_reader :minute
  attr_reader :sec
  attr_reader :nsec

  # Initializes a new `Date`
  #
  # @param type [DateType] the type of date. Cannot be nil.
  # @param value [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def initialize(type:, value:)
    self.type = type
    self.value = value
  end

  def type=(val)
    fail ArgumentError, 'Date type cannot be nil' unless val
    @type = val
  end

  # Sets the value.
  #
  # @param val [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def value=(val) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
    @date_time = to_datetime(val)
    @date = @date_time ? @date_time.to_date : to_date(val)
    @year = to_year(val)
    @month = to_month(val)
    @day = to_day(val)
    new_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
    if new_value.include?('T')
      @hour = @date_time.hour if @date_time
      @minute = @date_time.minute if @date_time
      @sec = @date_time.sec if @date_time
      @nsec = @date_time.to_time.nsec if @date_time
    end
    fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
    @value = new_value
  end

  private

  def to_year(val)
    return val if val.is_a?(Integer)
    return val.year if val.respond_to?(:year)
    matchdata = val.to_s.match(/^[0-9]+/)
    matchdata[0].to_i if matchdata
  end

  def to_month(val)
    return val.month if val.respond_to?(:month)
    matchdata = val.to_s.match(/^[0-9]+-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_day(val)
    return val.day if val.respond_to?(:day)
    matchdata = val.to_s.match(/^[0-9]+-[0-9]{2}-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_datetime(val)
    return val if val.is_a?(DateTime)
    DateTime.parse(val.to_s)
  rescue ArgumentError => e
    Mapping.log.debug("Can't extract DateTime from date value '#{val}': #{e}")
    nil
  end

  def to_date(val)
    return val if val.is_a?(::Date)
    return ::Date.parse(val.iso8601) if val.respond_to?(:iso8601)
    ::Date.parse(val.to_s)
  rescue ArgumentError => e
    Mapping.log.debug("Can't extract Date from date value '#{val}': #{e}")
    nil
  end

  # @!attribute [rw] type
  #  @return [DateType] the type of date. Cannot be nil.
  typesafe_enum_node :type, '@dateType', class: DateType

  # @!method value
  #   @return [String] The value as a string. May be any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime).
  text_node :value, 'text()'
end

#secInteger? (readonly)

Returns The seconds. Can be nil if no seconds were specified.

Returns:

  • (Integer, nil)

    The seconds. Can be nil if no seconds were specified.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/datacite/mapping/date.rb', line 54

class Date
  include XML::Mapping

  attr_reader :year
  attr_reader :month
  attr_reader :day
  attr_reader :hour
  attr_reader :minute
  attr_reader :sec
  attr_reader :nsec

  # Initializes a new `Date`
  #
  # @param type [DateType] the type of date. Cannot be nil.
  # @param value [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def initialize(type:, value:)
    self.type = type
    self.value = value
  end

  def type=(val)
    fail ArgumentError, 'Date type cannot be nil' unless val
    @type = val
  end

  # Sets the value.
  #
  # @param val [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def value=(val) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
    @date_time = to_datetime(val)
    @date = @date_time ? @date_time.to_date : to_date(val)
    @year = to_year(val)
    @month = to_month(val)
    @day = to_day(val)
    new_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
    if new_value.include?('T')
      @hour = @date_time.hour if @date_time
      @minute = @date_time.minute if @date_time
      @sec = @date_time.sec if @date_time
      @nsec = @date_time.to_time.nsec if @date_time
    end
    fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
    @value = new_value
  end

  private

  def to_year(val)
    return val if val.is_a?(Integer)
    return val.year if val.respond_to?(:year)
    matchdata = val.to_s.match(/^[0-9]+/)
    matchdata[0].to_i if matchdata
  end

  def to_month(val)
    return val.month if val.respond_to?(:month)
    matchdata = val.to_s.match(/^[0-9]+-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_day(val)
    return val.day if val.respond_to?(:day)
    matchdata = val.to_s.match(/^[0-9]+-[0-9]{2}-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_datetime(val)
    return val if val.is_a?(DateTime)
    DateTime.parse(val.to_s)
  rescue ArgumentError => e
    Mapping.log.debug("Can't extract DateTime from date value '#{val}': #{e}")
    nil
  end

  def to_date(val)
    return val if val.is_a?(::Date)
    return ::Date.parse(val.iso8601) if val.respond_to?(:iso8601)
    ::Date.parse(val.to_s)
  rescue ArgumentError => e
    Mapping.log.debug("Can't extract Date from date value '#{val}': #{e}")
    nil
  end

  # @!attribute [rw] type
  #  @return [DateType] the type of date. Cannot be nil.
  typesafe_enum_node :type, '@dateType', class: DateType

  # @!method value
  #   @return [String] The value as a string. May be any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime).
  text_node :value, 'text()'
end

#yearInteger (readonly)

Returns The year.

Returns:

  • (Integer)

    The year.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/datacite/mapping/date.rb', line 54

class Date
  include XML::Mapping

  attr_reader :year
  attr_reader :month
  attr_reader :day
  attr_reader :hour
  attr_reader :minute
  attr_reader :sec
  attr_reader :nsec

  # Initializes a new `Date`
  #
  # @param type [DateType] the type of date. Cannot be nil.
  # @param value [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def initialize(type:, value:)
    self.type = type
    self.value = value
  end

  def type=(val)
    fail ArgumentError, 'Date type cannot be nil' unless val
    @type = val
  end

  # Sets the value.
  #
  # @param val [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def value=(val) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
    @date_time = to_datetime(val)
    @date = @date_time ? @date_time.to_date : to_date(val)
    @year = to_year(val)
    @month = to_month(val)
    @day = to_day(val)
    new_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
    if new_value.include?('T')
      @hour = @date_time.hour if @date_time
      @minute = @date_time.minute if @date_time
      @sec = @date_time.sec if @date_time
      @nsec = @date_time.to_time.nsec if @date_time
    end
    fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
    @value = new_value
  end

  private

  def to_year(val)
    return val if val.is_a?(Integer)
    return val.year if val.respond_to?(:year)
    matchdata = val.to_s.match(/^[0-9]+/)
    matchdata[0].to_i if matchdata
  end

  def to_month(val)
    return val.month if val.respond_to?(:month)
    matchdata = val.to_s.match(/^[0-9]+-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_day(val)
    return val.day if val.respond_to?(:day)
    matchdata = val.to_s.match(/^[0-9]+-[0-9]{2}-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_datetime(val)
    return val if val.is_a?(DateTime)
    DateTime.parse(val.to_s)
  rescue ArgumentError => e
    Mapping.log.debug("Can't extract DateTime from date value '#{val}': #{e}")
    nil
  end

  def to_date(val)
    return val if val.is_a?(::Date)
    return ::Date.parse(val.iso8601) if val.respond_to?(:iso8601)
    ::Date.parse(val.to_s)
  rescue ArgumentError => e
    Mapping.log.debug("Can't extract Date from date value '#{val}': #{e}")
    nil
  end

  # @!attribute [rw] type
  #  @return [DateType] the type of date. Cannot be nil.
  typesafe_enum_node :type, '@dateType', class: DateType

  # @!method value
  #   @return [String] The value as a string. May be any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime).
  text_node :value, 'text()'
end

Instance Method Details

#value=(val)

Sets the value.

Parameters:

  • val (DateTime, Date, Integer, String)

    The value, as a DateTime, Date, or Integer, or as a String in any W3C DateTime format



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/datacite/mapping/date.rb', line 84

def value=(val) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  @date_time = to_datetime(val)
  @date = @date_time ? @date_time.to_date : to_date(val)
  @year = to_year(val)
  @month = to_month(val)
  @day = to_day(val)
  new_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
  if new_value.include?('T')
    @hour = @date_time.hour if @date_time
    @minute = @date_time.minute if @date_time
    @sec = @date_time.sec if @date_time
    @nsec = @date_time.to_time.nsec if @date_time
  end
  fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
  @value = new_value
end