lib/periodical/period.rb in periodical-0.2.0 vs lib/periodical/period.rb in periodical-1.0.0
- old
+ new
@@ -17,85 +17,72 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
module Periodical
- class Period
- VALID_UNITS = [:days, :weeks, :months, :years]
+ Period = Struct.new(:count, :unit) do
+ VALID_UNITS = [:days, :weeks, :months, :years].freeze
- # Accepts strings in the format of "2 weeks" or "weeks"
- def self.parse(value)
- if Array === value
- parts = value
+ # Plural is preferred, as in "1 or more days".
+ def initialize(count = 1, unit = :days)
+ super(count, unit)
+ end
+
+ def to_s
+ if self.count != 1
+ "#{self.count} #{self.unit}"
else
- parts = value.to_s.split(/\s+/, 2)
+ self.unit.to_s
end
-
- if parts.size == 1
- if parts[0].to_i == 0
- count = 1
- unit = parts[0].to_sym
- else
- count = parts[0].to_i
- unit = :days
-
- if count == 7
- count = 1
- unit = :weeks
- end
-
- if count == 365
- count = 1
- unit = :years
- end
- end
- else
- count = parts[0].to_i
- unit = parts[1].gsub(/\s+/, '_').downcase.to_sym
- end
-
- unless VALID_UNITS.include? unit
- raise ArgumentError.new("Invalid unit specified #{unit}!")
- end
-
- if count == 0
- raise ArgumentError.new("Count must be non-zero!")
- end
-
- return self.new(count, unit)
end
- def initialize(count = 1, unit = :days)
- @count = count
- @unit = unit
+ def advance(date, multiple = 1)
+ self.send("advance_#{unit}", date, multiple * self.count)
end
- def to_s
- "#{@count} #{@unit}"
+ private
+
+ def advance_days(date, count)
+ date + count
end
- def to_formatted_s
- if @count == 1
- @unit.to_s.gsub(/s$/, '')
- else
- to_s
- end
+ def advance_weeks(date, count)
+ date + (7 * count)
end
- def advance(date, multiple = 1)
- total = multiple * @count
+ def advance_months(date, count)
+ date >> count
+ end
+
+ def advance_years(date, count)
+ date >> (12 * count)
+ end
+
+ class << self
+ # Accepts strings in the format of "2 weeks" or "weeks"
+ def parse(string)
+ parts = string.split(/\s+/, 2)
+
+ if parts.size == 1
+ count = 1
+ unit = parts[0]
+ else
+ count, unit = parts
+ end
+
+ self.new(count.to_i, unit.to_sym)
+ end
- if unit == :days
- date + total
- elsif unit == :weeks
- date + (total * 7)
- elsif unit == :months
- date >> total
- elsif unit == :years
- date >> (total * 12)
+ def load(string)
+ if string
+ string = string.strip
+
+ parse(string) unless string.empty?
+ end
end
+
+ def dump(period)
+ period.to_s if period
+ end
end
-
- attr :unit
- attr :count
end
end