Class: Circa::Date
Overview
Manage partial dates.
Constant Summary collapse
- REGEX =
Match partial dates in format %Y-%m-%d
/^(\d{4})(?:-(0[0-9]|1[0-2])(?:-([0-2][0-9]|3[0-1])))$/
Instance Attribute Summary collapse
-
#valid_parts ⇒ Hash
readonly
A hash of valid parts in the date, with keys [:year, :month, :day] where applicable, and according values.
Instance Method Summary collapse
-
#initialize(date_string) ⇒ Date
constructor
Create a new Date.
-
#to_date ⇒ ::Date
Get the date as a Date.
-
#to_s ⇒ String
Get the date as a string.
Constructor Details
#initialize(date_string) ⇒ Date
Create a new Circa::Date
26 27 28 29 30 31 32 33 34 |
# File 'lib/circa/date.rb', line 26 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 ⇒ Hash (readonly)
Returns A hash of valid parts in the date, with keys [:year, :month, :day] where applicable, and according values.
13 14 15 16 17 18 19 20 21 22 23 24 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 |
# File 'lib/circa/date.rb', line 13 class Date include Util # Match partial dates in format %Y-%m-%d REGEX = /^(\d{4})(?:-(0[0-9]|1[0-2])(?:-([0-2][0-9]|3[0-1])))$/ attr_reader :valid_parts # Create a new {Circa::Date} # @param [String] date_string # A string in format %Y-%m-%d # @raise [ArgumentError] # If an invalid string is given def initialize(date_string) @year = '0000' @month = '00' @day = '00' @valid_parts = {} unless validate(date_string) raise ArgumentError, "Invalid date: #{date_string}" end end # Get the date as a string # @return [String] # A string in format %Y-%m-%d def to_s "#{@year}-#{@month}-#{@day}" end # Get the date as a {::Date} # @return [::Date] # A {::Date} def to_date return nil if valid_parts.empty? parts = [:year, :month, :day] args = valid_parts_as_args(parts) ::Date.send(:new, *args) end private def validate(date_string) matches = REGEX.match(date_string) return false if matches.nil? set_year(matches) && set_month(matches) && set_day(matches) end def set_year(matches) @year = matches[1] set_dependent(@year, matches[2], :year) end def set_month(matches) @month = matches[2] set_dependent(@month, matches[3], :month) end def set_dependent(a, b, name) a.to_i > 0 ? @valid_parts[name] = a : b.to_i == 0 end def set_day(matches) @day = matches[3] if @day.to_i > 0 return false unless ::Date.strptime(matches[0]).to_s == matches[0] @valid_parts[:day] = @day end true end end |
Instance Method Details
#to_date ⇒ ::Date
Get the date as a Date
46 47 48 49 50 51 |
# File 'lib/circa/date.rb', line 46 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 ⇒ String
Get the date as a string
39 40 41 |
# File 'lib/circa/date.rb', line 39 def to_s "#{@year}-#{@month}-#{@day}" end |