Sha256: a879ffa4ce35b9aaddec4c55f9c00b4241ea991311bc7445c14fa6bbac18ecb0
Contents?: true
Size: 1.91 KB
Versions: 1
Compression:
Stored size: 1.91 KB
Contents
module OData4 module Properties # Defines the DateTime OData4 type. class DateTime < OData4::Property # Returns the property value, properly typecast # @return [DateTime, nil] def value if (@value.nil? || @value.empty?) && allows_nil? nil else begin date_class.strptime(@value, strptime_format) rescue ArgumentError date_class.parse(@value) end end end # Sets the property value # @params new_value [DateTime] def value=(new_value) validate(new_value) @value = parse_value(new_value) end # The OData4 type name def type 'Edm.DateTime' end # Value to be used in JSON. # @return [String] def xml_value @value.andand.sub(/[\+\-]00:00$/, 'Z') end # Value to be used in JSON. # @return [String] def json_value xml_value end # Value to be used in URLs. # @return [String] def url_value xml_value end protected # Specifies date/time implementation to use def date_class ::DateTime end # Specifies the date/time format string used for `strptime` def strptime_format #'%Y-%m-%dT%H:%M:%S.%L' "%Y-%m-%dT%H:%M:%SZ" end def validate(value) begin return if value.nil? && allows_nil? return if value.is_a?(date_class) date_class.parse(value) rescue raise ArgumentError, "Value '#{value}' is not a date time format that can be parsed" end end def parse_value(value) return value if value.nil? && allows_nil? if value.is_a?(date_class) parsed_value = value else parsed_value = date_class.parse(value) end parsed_value.strftime(strptime_format) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
odata4-0.7.0 | lib/odata4/properties/date_time.rb |