|
1 |
| -require 'partial-date/version' |
2 |
| -require 'partial-date/error' |
3 |
| - |
4 |
| -# Public: A module for handling partial date storage. Partial dates are stored |
5 |
| -# as an 8 digit integer with optional month and day values. |
6 |
| -# |
7 |
| -# Examples |
8 |
| -# |
9 |
| -# p_date = 20100501 |
10 |
| -# # => 20100501 |
11 |
| -# |
12 |
| -# p_year = 2010 |
13 |
| -# puts p_date |
14 |
| -# # => 20100000 |
| 1 | +require File.expand_path('../partial-date/version', __FILE__) |
| 2 | +require File.expand_path('../partial-date/error', __FILE__) |
| 3 | +require 'date' |
| 4 | +# require 'partial-date/version' |
| 5 | +# require 'partial-date/error' |
| 6 | + |
15 | 7 | module PartialDate
|
16 | 8 |
|
17 |
| - # Public: Get the date in partial_date format. |
| 9 | + # Public: A class for handling partial date storage. Partial dates are stored |
| 10 | + # as an 8 digit integer with optional month and day values. |
18 | 11 | #
|
19 | 12 | # Examples
|
20 |
| - # myobject.p_year = "2010" |
21 |
| - # myobject.p_date |
22 |
| - # # => 20100000 |
23 |
| - # |
24 |
| - # Returns an integer representation of a partial date. |
25 |
| - def p_date |
26 |
| - #puts "Partial date is now read as #{@p_date}" |
27 |
| - @p_date ||= 0 |
28 |
| - end |
29 |
| - |
30 |
| - # Public: Sets the year portion of a partial date. |
| 13 | + # |
| 14 | + # date = PartialDate::Date.new |
| 15 | + # date.year = 2012 |
| 16 | + # date.month = 12 |
| 17 | + # date.day = 1 |
31 | 18 | #
|
32 |
| - # value - The string or integer value for a year. |
| 19 | + # date = PartialDate::Date.new {|d| d.year = 2012; d.month = 12; d.day = 1} |
33 | 20 | #
|
34 |
| - # Examples |
35 |
| - # myobject.p_year = "2000" |
36 |
| - # myobject.p_year = 2000 |
37 |
| - # myobject.p_date |
38 |
| - # # => 20000000 |
| 21 | + # date = PartialDate::Date.new {|d| d.year = 2012 } |
39 | 22 | #
|
40 |
| - # Returns nothing |
41 |
| - def p_year=(value) |
42 |
| - |
43 |
| - if value.nil? |
44 |
| - raise PartialDateError, "Year cannot be nil" |
| 23 | + class Date |
| 24 | + |
| 25 | + # Public: Create a new partial date class from a block of integers |
| 26 | + # or strings. |
| 27 | + # |
| 28 | + # Examples |
| 29 | + # |
| 30 | + # # From integers |
| 31 | + # date = PartialDate::Date.new {|d| d.year = 2012; d.month = 12; d.day = 1} |
| 32 | + # date.value |
| 33 | + # # => 20121201 |
| 34 | + # |
| 35 | + # # From strings |
| 36 | + # date = PartialDate::Date.new {|d| d.year = "2012"; d.month = "12"; d.day = "1"} |
| 37 | + # date.value |
| 38 | + # # => 20121201 |
| 39 | + # |
| 40 | + # Returns a date object. |
| 41 | + def initialize |
| 42 | + yield self if block_given? |
45 | 43 | end
|
46 | 44 |
|
47 |
| - if value.is_a?(String) |
48 |
| - if value =~ /\A\d{4}\z/ |
49 |
| - value = value.to_i |
50 |
| - else |
51 |
| - raise PartialDateError, "Year must be a valid four digit string or integer between 1 and 9999" |
52 |
| - end |
| 45 | + |
| 46 | + # Public: Get the date value in partial_date format. |
| 47 | + # |
| 48 | + # Examples |
| 49 | + # date.year = "2012" |
| 50 | + # date.value |
| 51 | + # # => 20120000 |
| 52 | + # |
| 53 | + # Returns an integer representation of a partial date. |
| 54 | + def value |
| 55 | + @value ||= 0 |
53 | 56 | end
|
54 | 57 |
|
55 |
| - if value.is_a?(Integer) && (value <= 9999 && value > 0) |
56 |
| - @p_date = (self.p_date - self.p_year) * 10000 + (value * 10000) |
57 |
| - else |
58 |
| - raise PartialDateError, "Year must be an integer between 1 and 9999" |
| 58 | + |
| 59 | + |
| 60 | + # Public: Loads an 8 digit date value into a date object. |
| 61 | + # |
| 62 | + # value - an 8 digit value in partial date format. |
| 63 | + # |
| 64 | + # Examples |
| 65 | + # |
| 66 | + # date.load = 201212201 |
| 67 | + # date.value |
| 68 | + # # => 20120000 |
| 69 | + # date.year |
| 70 | + # # => 2012 |
| 71 | + # date.month |
| 72 | + # # => 12 |
| 73 | + # date.day |
| 74 | + # # => 01 |
| 75 | + # |
| 76 | + # Returns nothing |
| 77 | + def load(value) |
| 78 | + if value.is_a?(Integer) && (value >= 10000 && value <= 99991231) |
| 79 | + @value = value |
| 80 | + else |
| 81 | + raise PartialDateError, "Date value must be an integer betwen 10000 and 99991231" |
| 82 | + end |
59 | 83 | end
|
60 |
| - end |
61 | 84 |
|
62 |
| - # Public: Get the year from a partial date. |
63 |
| - def p_year |
64 |
| - self.p_date > 9999 ? (self.p_date / 10000).abs : 0 |
65 |
| - end |
66 | 85 |
|
67 |
| - # Public: Set the month of a partial date. |
68 |
| - def p_month=(value) |
69 |
| - value = 0 if value.nil? |
| 86 | + # Public: Sets the year portion of a partial date. |
| 87 | + # |
| 88 | + # value - The string or integer value for a year. |
| 89 | + # |
| 90 | + # Examples |
| 91 | + # date.year = "2000" |
| 92 | + # date.value |
| 93 | + # # => 20000000 |
| 94 | + # |
| 95 | + # Returns nothing |
| 96 | + def year=(value) |
| 97 | + |
| 98 | + if value.nil? |
| 99 | + raise PartialDateError, "Year cannot be nil" |
| 100 | + end |
| 101 | + |
| 102 | + if value.is_a?(String) |
| 103 | + if value =~ /\A\d{4}\z/ |
| 104 | + value = value.to_i |
| 105 | + else |
| 106 | + raise PartialDateError, "Year must be a valid four digit string or integer between 1 and 9999" |
| 107 | + end |
| 108 | + end |
70 | 109 |
|
71 |
| - if value.is_a?(String) |
72 |
| - if value =~ /\A\d{1,2}\z/ |
73 |
| - value = value.to_i |
| 110 | + if value.is_a?(Integer) && (value <= 9999 && value > 0) |
| 111 | + @value = (self.value - self.year) * 10000 + (value * 10000) |
74 | 112 | else
|
75 |
| - raise PartialDateError, "Month must be a valid one or two digit string or integer between 1 and 12" |
| 113 | + raise PartialDateError, "Year must be an integer between 1 and 9999" |
76 | 114 | end
|
77 | 115 | end
|
78 | 116 |
|
79 |
| - if value.is_a?(Integer) && (value <= 12 && value >= 0) |
80 |
| - @p_date = self.p_date - (self.p_month * 100) + (value * 100) |
81 |
| - else |
82 |
| - raise PartialDateError, "Month must be integer between 1 and 12" |
| 117 | + # Public: Get the year from a partial date. |
| 118 | + def year |
| 119 | + self.value > 9999 ? (self.value / 10000).abs : 0 |
83 | 120 | end
|
84 |
| - end |
85 | 121 |
|
86 |
| - # Public: Get the month from a partial date. |
87 |
| - def p_month |
88 |
| - self.p_date > 99 ? ((self.p_date - (self.p_date / 10000).abs * 10000) / 100).abs : 0 |
89 |
| - end |
| 122 | + # Public: Set the month of a partial date. |
| 123 | + def month=(value) |
| 124 | + |
| 125 | + raise PartialDateError, "A year must be set before a month" if year == 0 |
90 | 126 |
|
| 127 | + value = 0 if value.nil? |
91 | 128 |
|
92 |
| - # Public: Set the day portion of a partial date. Day is optional so zero, |
93 |
| - # nil and empty strings are allowed. |
94 |
| - def p_day=(value) |
95 |
| - is_valid = true |
96 |
| - value = 0 if value.nil? |
| 129 | + if value.is_a?(String) |
| 130 | + if value =~ /\A\d{1,2}\z/ |
| 131 | + value = value.to_i |
| 132 | + else |
| 133 | + raise PartialDateError, "Month must be a valid one or two digit string or integer between 1 and 12" |
| 134 | + end |
| 135 | + end |
97 | 136 |
|
98 |
| - if value.is_a?(String) |
99 |
| - if value =~ /\A\d{1,2}\z/ || value.blank? |
100 |
| - value = value.to_i |
| 137 | + if value.is_a?(Integer) && (value <= 12 && value >= 0) |
| 138 | + @value = self.value - (self.month * 100) + (value * 100) |
101 | 139 | else
|
102 |
| - is_valid = false |
103 |
| - @day_error = "must be a one or two digit number" |
| 140 | + raise PartialDateError, "Month must be integer between 1 and 12" |
104 | 141 | end
|
105 | 142 | end
|
106 | 143 |
|
107 |
| - unless is_valid && value.is_a?(Integer) && value < 32 |
108 |
| - is_valid = false |
109 |
| - @day_error = "must be a valid day" |
| 144 | + # Public: Get the month from a partial date. |
| 145 | + def month |
| 146 | + self.value > 99 ? ((self.value - (self.value / 10000).abs * 10000) / 100).abs : 0 |
110 | 147 | end
|
111 | 148 |
|
112 |
| - if is_valid |
113 |
| - @p_date = (self.p_date - self.p_day + value) |
| 149 | + |
| 150 | + # Public: Set the day portion of a partial date. Day is optional so zero, |
| 151 | + # nil and empty strings are allowed. |
| 152 | + def day=(value) |
| 153 | + |
| 154 | + raise PartialDateError, "A year and month must be set before a day" if year == 0 && month == 0 |
| 155 | + |
| 156 | + value = 0 if value.nil? |
| 157 | + |
| 158 | + if value.is_a?(String) |
| 159 | + if value =~ /\A\d{1,2}\z/ |
| 160 | + value = value.to_i |
| 161 | + else |
| 162 | + raise PartialDateError, "Day must be a valid one or two digit string or integer between 1 and 31" |
| 163 | + end |
| 164 | + end |
| 165 | + |
| 166 | + if value.is_a?(Integer) && (value >= 0 && value <= 31) |
| 167 | + begin |
| 168 | + date = ::Date.civil(self.year, self.month, value) if value > 0 |
| 169 | + @value = (self.value - self.day + value) |
| 170 | + rescue |
| 171 | + raise PartialDateError, "Day must be a valid day for the given month" |
| 172 | + end |
| 173 | + else |
| 174 | + raise PartialDateError, "Day must be an integer between 1 and 31" |
| 175 | + end |
114 | 176 | end
|
115 |
| - end |
116 | 177 |
|
117 |
| - # Public: Get the day from a partial date. |
118 |
| - def p_day |
119 |
| - self.p_date > 0 ? self.p_date - (self.p_date / 100).abs * 100 : 0 |
| 178 | + # Public: Get the day from a partial date. |
| 179 | + def day |
| 180 | + self.value > 0 ? self.value - (self.value / 100).abs * 100 : 0 |
| 181 | + end |
120 | 182 | end
|
121 | 183 | end
|
0 commit comments