Class: Circa::Date
Constant Summary collapse
- REGEX =
/^(\d{4})(?:-(0[0-9]|1[0-2])(?:-([0-2][0-9]|3[0-1])))$/
Instance Attribute Summary collapse
-
#valid_parts ⇒ Object
readonly
Returns the value of attribute valid_parts.
Instance Method Summary collapse
-
#initialize(date_string) ⇒ Date
constructor
A new instance of Date.
- #to_date ⇒ Object
- #to_s ⇒ Object
Methods included from Util
Constructor Details
#initialize(date_string) ⇒ Date
Returns a new instance of Date.
11 12 13 14 15 16 17 18 19 |
# File 'lib/circa/date.rb', line 11 def initialize(date_string) @year = '0000' @month = '00' @day = '00' @valid_parts = {} unless validate(date_string) raise ArgumentError, "Invalid date: #{date_string}" end end |
Instance Attribute Details
#valid_parts ⇒ Object (readonly)
Returns the value of attribute valid_parts.
9 10 11 |
# File 'lib/circa/date.rb', line 9 def valid_parts @valid_parts end |
Instance Method Details
#to_date ⇒ Object
25 26 27 28 29 30 |
# File 'lib/circa/date.rb', line 25 def to_date return nil if valid_parts.empty? parts = [:year, :month, :day] args = valid_parts_as_args(parts) ::Date.send(:new, *args) end |
#to_s ⇒ Object
21 22 23 |
# File 'lib/circa/date.rb', line 21 def to_s "#{@year}-#{@month}-#{@day}" end |