class FmRest::StringDate
Gotchas:
1.
Date === <StringDate instance> # => false
The above can affect case conditions, as trying to match a StringDate
with:
case obj when Date ...
…will not work.
Instead one must specify the FmRest::StringDate
class:
case obj when Date, FmRest::StringDate ...
2.
StringDate#eql? only matches other strings, not dates.
This could affect hash indexing when a StringDate
is used as a key.
TODO: Verify the above
3.
StringDate#succ and StringDate#next return a String, despite Date#succ and Date#next also existing.
Workaround: Use StringDate#next_day or strdate + 1
4.
StringDate#to_s returns the original string, not the Date string representation.
Workaround: Use strdate.to_date.to_s
5.
StringDate#hash returns the hash for the string (important when using a StringDate
as a hash key)
6.
StringDate#as_json returns the string
Workaround: Use strdate.to_date.as_json
7.
Equality with Date is not reciprocal:
str_date == date #=> true date == str_date #=> false
NOTE: Potential workaround: Inherit StringDate
from Date instead of String
8.
Calling string transforming methods (e.g. .upcase) returns a StringDate
instead of a String.
NOTE: Potential workaround: Inherit StringDate
from Date instead of String
Constants
- DELEGATE_CLASS
Public Class Methods
# File lib/fmrest/string_date.rb, line 93 def initialize(str, date, **str_args) raise ArgumentError, "str must be of class String" unless str.is_a?(String) raise ArgumentError, "date must be of class #{self.class::DELEGATE_CLASS.name}" unless date.is_a?(self.class::DELEGATE_CLASS) super(str, **str_args) @delegate = date freeze end
# File lib/fmrest/string_date.rb, line 82 def strptime(str, date_format, *_) begin date = self::DELEGATE_CLASS.strptime(str, date_format) rescue ArgumentError raise InvalidDate end new(str, date) end
Public Instance Methods
# File lib/fmrest/string_date.rb, line 135 def +(val) return @delegate + val if val.kind_of?(Numeric) super end
# File lib/fmrest/string_date.rb, line 140 def <<(val) return @delegate << val if val.kind_of?(Numeric) super end
# File lib/fmrest/string_date.rb, line 130 def <=>(oth) return @delegate <=> oth if oth.is_a?(::Date) || oth.is_a?(Numeric) super end
# File lib/fmrest/string_date.rb, line 145 def ==(oth) return @delegate == oth if oth.kind_of?(::Date) || oth.kind_of?(Numeric) super end
# File lib/fmrest/string_date.rb, line 156 def between?(a, b) return @delegate.between?(a, b) if [a, b].any? {|o| o.is_a?(::Date) || o.is_a?(Numeric) } super end
ActiveSupport method
# File lib/fmrest/string_date.rb, line 122 def in_time_zone(*_) @delegate.in_time_zone(*_) end
# File lib/fmrest/string_date.rb, line 126 def inspect "#<#{self.class.name} #{@delegate.inspect} - #{super}>" end
# File lib/fmrest/string_date.rb, line 104 def is_a?(klass) klass == ::Date || super end
# File lib/fmrest/string_date.rb, line 109 def to_date @delegate end
# File lib/fmrest/string_date.rb, line 113 def to_datetime @delegate.to_datetime end
# File lib/fmrest/string_date.rb, line 117 def to_time @delegate.to_time end
# File lib/fmrest/string_date.rb, line 151 def upto(oth, &blk) return @delegate.upto(oth, &blk) if oth.kind_of?(::Date) || oth.kind_of?(Numeric) super end
Private Instance Methods
# File lib/fmrest/string_date.rb, line 167 def method_missing(method, *args, &block) @delegate.send(method, *args, &block) end
# File lib/fmrest/string_date.rb, line 163 def respond_to_missing?(name, include_private = false) @delegate.respond_to?(name, include_private) end