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), ISO8601 datetime, or RKMS-ISO8601 date range.

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



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

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

Instance Attribute Details

#date_valueDateValue? (readonly)

Returns the single date/time represented by this <date/> field, if it does not represent a ragne.

Returns:

  • (DateValue, nil)

    the single date/time represented by this <date/> field, if it does not represent a ragne



51
52
53
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
# File 'lib/datacite/mapping/date.rb', line 51

class Date
  include XML::Mapping

  attr_reader :date_value
  attr_reader :range_start
  attr_reader :range_end

  # 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

  def value=(val) # rubocop:disable Metrics/AbcSize
    parts = val.to_s.split('/', -1) # negative limit so we don't drop trailing empty string
    @date_value, @range_start, @range_end = nil
    if parts.size == 1
      @date_value = DateValue.new(val)
    elsif parts.size == 2
      @range_start, @range_end = parts.map(&:strip).map { |part| DateValue.new(part) unless part == '' }
      # puts "#{val} -> [#{range_start}, #{range_end}]"
    else
      fail ArgumentError, "Unable to parse date value #{val}"
    end
    @value = val
  end

  private

  # @!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()'

  fallback_mapping :datacite_3, :_default
end

#range_endDateValue? (readonly)

Returns the end of the date range represented by this <date/> field, if it represents a range, and the range is not open on the upper end.

Returns:

  • (DateValue, nil)

    the end of the date range represented by this <date/> field, if it represents a range, and the range is not open on the upper end



51
52
53
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
# File 'lib/datacite/mapping/date.rb', line 51

class Date
  include XML::Mapping

  attr_reader :date_value
  attr_reader :range_start
  attr_reader :range_end

  # 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

  def value=(val) # rubocop:disable Metrics/AbcSize
    parts = val.to_s.split('/', -1) # negative limit so we don't drop trailing empty string
    @date_value, @range_start, @range_end = nil
    if parts.size == 1
      @date_value = DateValue.new(val)
    elsif parts.size == 2
      @range_start, @range_end = parts.map(&:strip).map { |part| DateValue.new(part) unless part == '' }
      # puts "#{val} -> [#{range_start}, #{range_end}]"
    else
      fail ArgumentError, "Unable to parse date value #{val}"
    end
    @value = val
  end

  private

  # @!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()'

  fallback_mapping :datacite_3, :_default
end

#range_startDateValue? (readonly)

Returns the start of the date range represented by this <date/> field, if it represents a range, and the range is not open on the lower end.

Returns:

  • (DateValue, nil)

    the start of the date range represented by this <date/> field, if it represents a range, and the range is not open on the lower end



51
52
53
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
# File 'lib/datacite/mapping/date.rb', line 51

class Date
  include XML::Mapping

  attr_reader :date_value
  attr_reader :range_start
  attr_reader :range_end

  # 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

  def value=(val) # rubocop:disable Metrics/AbcSize
    parts = val.to_s.split('/', -1) # negative limit so we don't drop trailing empty string
    @date_value, @range_start, @range_end = nil
    if parts.size == 1
      @date_value = DateValue.new(val)
    elsif parts.size == 2
      @range_start, @range_end = parts.map(&:strip).map { |part| DateValue.new(part) unless part == '' }
      # puts "#{val} -> [#{range_start}, #{range_end}]"
    else
      fail ArgumentError, "Unable to parse date value #{val}"
    end
    @value = val
  end

  private

  # @!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()'

  fallback_mapping :datacite_3, :_default
end

Instance Method Details

#value=(val)

rubocop:disable Metrics/AbcSize



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/datacite/mapping/date.rb', line 73

def value=(val) # rubocop:disable Metrics/AbcSize
  parts = val.to_s.split('/', -1) # negative limit so we don't drop trailing empty string
  @date_value, @range_start, @range_end = nil
  if parts.size == 1
    @date_value = DateValue.new(val)
  elsif parts.size == 2
    @range_start, @range_end = parts.map(&:strip).map { |part| DateValue.new(part) unless part == '' }
    # puts "#{val} -> [#{range_start}, #{range_end}]"
  else
    fail ArgumentError, "Unable to parse date value #{val}"
  end
  @value = val
end