Class: Datacite::Mapping::DateValue
- Inherits:
-
Object
- Object
- Datacite::Mapping::DateValue
- Includes:
- Comparable
- Defined in:
- lib/datacite/mapping/date_value.rb
Overview
Represents a DataCite "date" value, which can be a year, date (year-month-day or just year-month), or ISO8601 datetime.
Instance Attribute Summary collapse
-
#date
readonly
Returns the value of attribute date.
-
#day ⇒ Integer?
readonly
The day.
-
#hour ⇒ Integer?
readonly
The hour.
-
#iso_value
readonly
Returns the value of attribute iso_value.
-
#minute ⇒ Integer?
readonly
The minutes.
-
#month ⇒ Integer?
readonly
The month.
-
#nsec ⇒ Integer?
readonly
The nanoseconds.
-
#sec ⇒ Integer?
readonly
The seconds.
-
#year ⇒ Integer
readonly
The year.
-
#zone
readonly
Returns the value of attribute zone.
Instance Method Summary collapse
- #<=>(other)
- #hash
-
#initialize(val) ⇒ DateValue
constructor
Creates a new DateValue.
- #to_s
Constructor Details
#initialize(val) ⇒ DateValue
Creates a new Datacite::Mapping::DateValue.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/datacite/mapping/date_value.rb', line 34 def initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength datetime = to_datetime(val) @date = datetime ? datetime.to_date : to_date(val) @year = to_year(val) @month = to_month(val) @day = to_day(val) @iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s if datetime && iso_value.include?('T') @hour = datetime.hour @minute = datetime.minute @sec = datetime.sec @nsec = datetime.to_time.nsec @zone = datetime.zone end raise ArgumentError, "Unable to parse date value '#{val}'" unless @year end |
Instance Attribute Details
#date (readonly)
Returns the value of attribute date.
28 29 30 |
# File 'lib/datacite/mapping/date_value.rb', line 28 def date @date end |
#day ⇒ Integer? (readonly)
Returns The day. Can be nil
if no day was specified.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/datacite/mapping/date_value.rb', line 25 class DateValue include Comparable attr_reader :year, :month, :day, :hour, :minute, :sec, :nsec, :date, :zone, :iso_value # Creates a new {DateValue}. # # @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 initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength datetime = to_datetime(val) @date = datetime ? datetime.to_date : to_date(val) @year = to_year(val) @month = to_month(val) @day = to_day(val) @iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s if datetime && iso_value.include?('T') @hour = datetime.hour @minute = datetime.minute @sec = datetime.sec @nsec = datetime.to_time.nsec @zone = datetime.zone end raise ArgumentError, "Unable to parse date value '#{val}'" unless @year end def <=>(other) return nil unless other.instance_of?(self.class) %i[year month day hour minute sec nsec].each do |v| order = send(v) <=> other.send(v) return order if order.nonzero? end 0 end def hash [year, month, day, hour, minute, sec, nsec, zone].hash end def to_s @str_value = begin str_value = iso_value if nsec && nsec != 0 frac_str = (nsec / 1e9).to_s.sub('0', '') str_value.sub!(/(T[0-9]+:[0-9]+:[0-9]+)/, "\\1#{frac_str}") unless str_value.include?(frac_str) end str_value end 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 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 nil end end |
#hour ⇒ Integer? (readonly)
Returns The hour. Can be nil
if no hour was specified.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/datacite/mapping/date_value.rb', line 25 class DateValue include Comparable attr_reader :year, :month, :day, :hour, :minute, :sec, :nsec, :date, :zone, :iso_value # Creates a new {DateValue}. # # @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 initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength datetime = to_datetime(val) @date = datetime ? datetime.to_date : to_date(val) @year = to_year(val) @month = to_month(val) @day = to_day(val) @iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s if datetime && iso_value.include?('T') @hour = datetime.hour @minute = datetime.minute @sec = datetime.sec @nsec = datetime.to_time.nsec @zone = datetime.zone end raise ArgumentError, "Unable to parse date value '#{val}'" unless @year end def <=>(other) return nil unless other.instance_of?(self.class) %i[year month day hour minute sec nsec].each do |v| order = send(v) <=> other.send(v) return order if order.nonzero? end 0 end def hash [year, month, day, hour, minute, sec, nsec, zone].hash end def to_s @str_value = begin str_value = iso_value if nsec && nsec != 0 frac_str = (nsec / 1e9).to_s.sub('0', '') str_value.sub!(/(T[0-9]+:[0-9]+:[0-9]+)/, "\\1#{frac_str}") unless str_value.include?(frac_str) end str_value end 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 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 nil end end |
#iso_value (readonly)
Returns the value of attribute iso_value.
28 29 30 |
# File 'lib/datacite/mapping/date_value.rb', line 28 def iso_value @iso_value end |
#minute ⇒ Integer? (readonly)
Returns The minutes. Can be nil
if no minutes were specified.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/datacite/mapping/date_value.rb', line 25 class DateValue include Comparable attr_reader :year, :month, :day, :hour, :minute, :sec, :nsec, :date, :zone, :iso_value # Creates a new {DateValue}. # # @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 initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength datetime = to_datetime(val) @date = datetime ? datetime.to_date : to_date(val) @year = to_year(val) @month = to_month(val) @day = to_day(val) @iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s if datetime && iso_value.include?('T') @hour = datetime.hour @minute = datetime.minute @sec = datetime.sec @nsec = datetime.to_time.nsec @zone = datetime.zone end raise ArgumentError, "Unable to parse date value '#{val}'" unless @year end def <=>(other) return nil unless other.instance_of?(self.class) %i[year month day hour minute sec nsec].each do |v| order = send(v) <=> other.send(v) return order if order.nonzero? end 0 end def hash [year, month, day, hour, minute, sec, nsec, zone].hash end def to_s @str_value = begin str_value = iso_value if nsec && nsec != 0 frac_str = (nsec / 1e9).to_s.sub('0', '') str_value.sub!(/(T[0-9]+:[0-9]+:[0-9]+)/, "\\1#{frac_str}") unless str_value.include?(frac_str) end str_value end 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 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 nil end end |
#month ⇒ Integer? (readonly)
Returns The month. Can be nil
if no month was specified.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/datacite/mapping/date_value.rb', line 25 class DateValue include Comparable attr_reader :year, :month, :day, :hour, :minute, :sec, :nsec, :date, :zone, :iso_value # Creates a new {DateValue}. # # @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 initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength datetime = to_datetime(val) @date = datetime ? datetime.to_date : to_date(val) @year = to_year(val) @month = to_month(val) @day = to_day(val) @iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s if datetime && iso_value.include?('T') @hour = datetime.hour @minute = datetime.minute @sec = datetime.sec @nsec = datetime.to_time.nsec @zone = datetime.zone end raise ArgumentError, "Unable to parse date value '#{val}'" unless @year end def <=>(other) return nil unless other.instance_of?(self.class) %i[year month day hour minute sec nsec].each do |v| order = send(v) <=> other.send(v) return order if order.nonzero? end 0 end def hash [year, month, day, hour, minute, sec, nsec, zone].hash end def to_s @str_value = begin str_value = iso_value if nsec && nsec != 0 frac_str = (nsec / 1e9).to_s.sub('0', '') str_value.sub!(/(T[0-9]+:[0-9]+:[0-9]+)/, "\\1#{frac_str}") unless str_value.include?(frac_str) end str_value end 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 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 nil end end |
#nsec ⇒ Integer? (readonly)
Returns The nanoseconds. Can be nil
if no nanoseconds were specified.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/datacite/mapping/date_value.rb', line 25 class DateValue include Comparable attr_reader :year, :month, :day, :hour, :minute, :sec, :nsec, :date, :zone, :iso_value # Creates a new {DateValue}. # # @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 initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength datetime = to_datetime(val) @date = datetime ? datetime.to_date : to_date(val) @year = to_year(val) @month = to_month(val) @day = to_day(val) @iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s if datetime && iso_value.include?('T') @hour = datetime.hour @minute = datetime.minute @sec = datetime.sec @nsec = datetime.to_time.nsec @zone = datetime.zone end raise ArgumentError, "Unable to parse date value '#{val}'" unless @year end def <=>(other) return nil unless other.instance_of?(self.class) %i[year month day hour minute sec nsec].each do |v| order = send(v) <=> other.send(v) return order if order.nonzero? end 0 end def hash [year, month, day, hour, minute, sec, nsec, zone].hash end def to_s @str_value = begin str_value = iso_value if nsec && nsec != 0 frac_str = (nsec / 1e9).to_s.sub('0', '') str_value.sub!(/(T[0-9]+:[0-9]+:[0-9]+)/, "\\1#{frac_str}") unless str_value.include?(frac_str) end str_value end 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 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 nil end end |
#sec ⇒ Integer? (readonly)
Returns The seconds. Can be nil
if no seconds were specified.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/datacite/mapping/date_value.rb', line 25 class DateValue include Comparable attr_reader :year, :month, :day, :hour, :minute, :sec, :nsec, :date, :zone, :iso_value # Creates a new {DateValue}. # # @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 initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength datetime = to_datetime(val) @date = datetime ? datetime.to_date : to_date(val) @year = to_year(val) @month = to_month(val) @day = to_day(val) @iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s if datetime && iso_value.include?('T') @hour = datetime.hour @minute = datetime.minute @sec = datetime.sec @nsec = datetime.to_time.nsec @zone = datetime.zone end raise ArgumentError, "Unable to parse date value '#{val}'" unless @year end def <=>(other) return nil unless other.instance_of?(self.class) %i[year month day hour minute sec nsec].each do |v| order = send(v) <=> other.send(v) return order if order.nonzero? end 0 end def hash [year, month, day, hour, minute, sec, nsec, zone].hash end def to_s @str_value = begin str_value = iso_value if nsec && nsec != 0 frac_str = (nsec / 1e9).to_s.sub('0', '') str_value.sub!(/(T[0-9]+:[0-9]+:[0-9]+)/, "\\1#{frac_str}") unless str_value.include?(frac_str) end str_value end 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 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 nil end end |
#year ⇒ Integer (readonly)
Returns The year.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/datacite/mapping/date_value.rb', line 25 class DateValue include Comparable attr_reader :year, :month, :day, :hour, :minute, :sec, :nsec, :date, :zone, :iso_value # Creates a new {DateValue}. # # @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 initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength datetime = to_datetime(val) @date = datetime ? datetime.to_date : to_date(val) @year = to_year(val) @month = to_month(val) @day = to_day(val) @iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s if datetime && iso_value.include?('T') @hour = datetime.hour @minute = datetime.minute @sec = datetime.sec @nsec = datetime.to_time.nsec @zone = datetime.zone end raise ArgumentError, "Unable to parse date value '#{val}'" unless @year end def <=>(other) return nil unless other.instance_of?(self.class) %i[year month day hour minute sec nsec].each do |v| order = send(v) <=> other.send(v) return order if order.nonzero? end 0 end def hash [year, month, day, hour, minute, sec, nsec, zone].hash end def to_s @str_value = begin str_value = iso_value if nsec && nsec != 0 frac_str = (nsec / 1e9).to_s.sub('0', '') str_value.sub!(/(T[0-9]+:[0-9]+:[0-9]+)/, "\\1#{frac_str}") unless str_value.include?(frac_str) end str_value end 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 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 nil end end |
#zone (readonly)
Returns the value of attribute zone.
28 29 30 |
# File 'lib/datacite/mapping/date_value.rb', line 28 def zone @zone end |
Instance Method Details
#<=>(other)
51 52 53 54 55 56 57 58 59 |
# File 'lib/datacite/mapping/date_value.rb', line 51 def <=>(other) return nil unless other.instance_of?(self.class) %i[year month day hour minute sec nsec].each do |v| order = send(v) <=> other.send(v) return order if order.nonzero? end 0 end |
#hash
61 62 63 |
# File 'lib/datacite/mapping/date_value.rb', line 61 def hash [year, month, day, hour, minute, sec, nsec, zone].hash end |
#to_s
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/datacite/mapping/date_value.rb', line 65 def to_s @str_value = begin str_value = iso_value if nsec && nsec != 0 frac_str = (nsec / 1e9).to_s.sub('0', '') str_value.sub!(/(T[0-9]+:[0-9]+:[0-9]+)/, "\\1#{frac_str}") unless str_value.include?(frac_str) end str_value end end |