Class: Datacite::Mapping::Date
- Inherits:
-
Object
- Object
- Datacite::Mapping::Date
- Includes:
- Comparable, 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
-
#date_information ⇒ String
Information to clarify a date.
-
#date_value ⇒ DateValue?
readonly
The single date/time represented by this
<date/>
field, if it does not represent a ragne. -
#range_end ⇒ DateValue?
readonly
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. -
#range_start ⇒ DateValue?
readonly
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. -
#type ⇒ DateType
The type of date.
Instance Method Summary collapse
- #<=>(other)
- #hash
-
#initialize(type:, value:) ⇒ Date
constructor
Initializes a new
Date
. - #to_s
-
#value ⇒ String
The value as a string.
-
#value=(val)
rubocop:disable Metrics/AbcSize.
Constructor Details
#initialize(type:, value:) ⇒ Date
Initializes a new Date
72 73 74 75 |
# File 'lib/datacite/mapping/date.rb', line 72 def initialize(type:, value:) self.type = type self.value = value end |
Instance Attribute Details
#date_information ⇒ String
Returns information to clarify a date.
121 |
# File 'lib/datacite/mapping/date.rb', line 121 text_node :date_information, '@dateInformation', default_value: nil |
#date_value ⇒ DateValue? (readonly)
Returns the single date/time represented by this <date/>
field,
if it does not represent a ragne.
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 |
# File 'lib/datacite/mapping/date.rb', line 59 class Date include Comparable 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) raise 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 raise ArgumentError, "Unable to parse date value #{val}" end @value = date_value ? date_value.to_s : "#{range_start}/#{range_end}" end def <=>(other) return nil unless other.class == self.class i[date_value range_start range_end type].each do |v| order = send(v) <=> other.send(v) return order if order.nonzero? end 0 end def hash [date_value, range_start, range_end, type].hash end def to_s @value end # @!attribute [rw] type # @return [DateType] the type of date. Cannot be nil. typesafe_enum_node :type, '@dateType', class: DateType # @!attribute [rw] date_information # @return [String] information to clarify a date text_node :date_information, '@dateInformation', default_value: nil # @!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_end ⇒ DateValue? (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.
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 |
# File 'lib/datacite/mapping/date.rb', line 59 class Date include Comparable 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) raise 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 raise ArgumentError, "Unable to parse date value #{val}" end @value = date_value ? date_value.to_s : "#{range_start}/#{range_end}" end def <=>(other) return nil unless other.class == self.class i[date_value range_start range_end type].each do |v| order = send(v) <=> other.send(v) return order if order.nonzero? end 0 end def hash [date_value, range_start, range_end, type].hash end def to_s @value end # @!attribute [rw] type # @return [DateType] the type of date. Cannot be nil. typesafe_enum_node :type, '@dateType', class: DateType # @!attribute [rw] date_information # @return [String] information to clarify a date text_node :date_information, '@dateInformation', default_value: nil # @!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_start ⇒ DateValue? (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.
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 |
# File 'lib/datacite/mapping/date.rb', line 59 class Date include Comparable 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) raise 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 raise ArgumentError, "Unable to parse date value #{val}" end @value = date_value ? date_value.to_s : "#{range_start}/#{range_end}" end def <=>(other) return nil unless other.class == self.class i[date_value range_start range_end type].each do |v| order = send(v) <=> other.send(v) return order if order.nonzero? end 0 end def hash [date_value, range_start, range_end, type].hash end def to_s @value end # @!attribute [rw] type # @return [DateType] the type of date. Cannot be nil. typesafe_enum_node :type, '@dateType', class: DateType # @!attribute [rw] date_information # @return [String] information to clarify a date text_node :date_information, '@dateInformation', default_value: nil # @!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 |
#type ⇒ DateType
Returns the type of date. Cannot be nil.
117 |
# File 'lib/datacite/mapping/date.rb', line 117 typesafe_enum_node :type, '@dateType', class: DateType |
Instance Method Details
#<=>(other)
97 98 99 100 101 102 103 104 105 |
# File 'lib/datacite/mapping/date.rb', line 97 def <=>(other) return nil unless other.class == self.class i[date_value range_start range_end type].each do |v| order = send(v) <=> other.send(v) return order if order.nonzero? end 0 end |
#hash
107 108 109 |
# File 'lib/datacite/mapping/date.rb', line 107 def hash [date_value, range_start, range_end, type].hash end |
#to_s
111 112 113 |
# File 'lib/datacite/mapping/date.rb', line 111 def to_s @value end |
#value ⇒ String
Returns The value as a string. May be any W3C DateTime format.
125 |
# File 'lib/datacite/mapping/date.rb', line 125 text_node :value, 'text()' |
#value=(val)
rubocop:disable Metrics/AbcSize
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/datacite/mapping/date.rb', line 83 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 raise ArgumentError, "Unable to parse date value #{val}" end @value = date_value ? date_value.to_s : "#{range_start}/#{range_end}" end |