Class: FmRest::StringDate
- Inherits:
-
String
- Object
- String
- FmRest::StringDate
- Defined in:
- lib/fmrest/string_date.rb
Overview
Gotchas:
1.
Date ===
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
Direct Known Subclasses
Defined Under Namespace
Classes: InvalidDate
Constant Summary collapse
- DELEGATE_CLASS =
::Date
Class Method Summary collapse
Instance Method Summary collapse
- #+(val) ⇒ Object
- #<<(val) ⇒ Object
- #<=>(oth) ⇒ Object
- #==(oth) ⇒ Object (also: #===)
- #between?(a, b) ⇒ Boolean
-
#in_time_zone(*_) ⇒ Object
ActiveSupport method.
-
#initialize(str, date, **str_args) ⇒ StringDate
constructor
A new instance of StringDate.
- #inspect ⇒ Object
- #is_a?(klass) ⇒ Boolean (also: #kind_of?)
- #to_date ⇒ Object
- #to_datetime ⇒ Object
- #to_time ⇒ Object
- #upto(oth, &blk) ⇒ Object
Constructor Details
#initialize(str, date, **str_args) ⇒ StringDate
Returns a new instance of StringDate.
93 94 95 96 97 98 99 100 101 102 |
# 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
167 168 169 |
# File 'lib/fmrest/string_date.rb', line 167 def method_missing(method, *args, &block) @delegate.send(method, *args, &block) end |
Class Method Details
.strptime(str, date_format, *_) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# 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 |
Instance Method Details
#+(val) ⇒ Object
135 136 137 138 |
# File 'lib/fmrest/string_date.rb', line 135 def +(val) return @delegate + val if val.kind_of?(Numeric) super end |
#<<(val) ⇒ Object
140 141 142 143 |
# File 'lib/fmrest/string_date.rb', line 140 def <<(val) return @delegate << val if val.kind_of?(Numeric) super end |
#<=>(oth) ⇒ Object
130 131 132 133 |
# File 'lib/fmrest/string_date.rb', line 130 def <=>(oth) return @delegate <=> oth if oth.is_a?(::Date) || oth.is_a?(Numeric) super end |
#==(oth) ⇒ Object Also known as: ===
145 146 147 148 |
# File 'lib/fmrest/string_date.rb', line 145 def ==(oth) return @delegate == oth if oth.kind_of?(::Date) || oth.kind_of?(Numeric) super end |
#between?(a, b) ⇒ Boolean
156 157 158 159 |
# 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 |
#in_time_zone(*_) ⇒ Object
ActiveSupport method
122 123 124 |
# File 'lib/fmrest/string_date.rb', line 122 def in_time_zone(*_) @delegate.in_time_zone(*_) end |
#inspect ⇒ Object
126 127 128 |
# File 'lib/fmrest/string_date.rb', line 126 def inspect "#<#{self.class.name} #{@delegate.inspect} - #{super}>" end |
#is_a?(klass) ⇒ Boolean Also known as: kind_of?
104 105 106 |
# File 'lib/fmrest/string_date.rb', line 104 def is_a?(klass) klass == ::Date || super end |
#to_date ⇒ Object
109 110 111 |
# File 'lib/fmrest/string_date.rb', line 109 def to_date @delegate end |
#to_datetime ⇒ Object
113 114 115 |
# File 'lib/fmrest/string_date.rb', line 113 def to_datetime @delegate.to_datetime end |
#to_time ⇒ Object
117 118 119 |
# File 'lib/fmrest/string_date.rb', line 117 def to_time @delegate.to_time end |
#upto(oth, &blk) ⇒ Object
151 152 153 154 |
# 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 |