Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #97 from jonhyman/feature/set_to_s_format

Allows customization of the #to_s date format used by Schedule#to_s.
  • Loading branch information...
commit 600b885c8e184432280b740592c46cb68e441e79 2 parents 3c059a8 + b2e7fe3
@seejohnrun authored
View
13 lib/ice_cube.rb
@@ -69,11 +69,18 @@ module Validations
ONE_DAY = ONE_HOUR * 24
ONE_WEEK = ONE_DAY * 7
- # Formatting
- TO_S_TIME_FORMAT = '%B %e, %Y'
-
def self.use_psych?
@use_psych ||= defined?(Psych) && defined?(Psych::VERSION)
end
+ # Defines the format used by IceCube when printing out Schedule#to_s.
+ # Defaults to '%B %e, %Y'
+ def self.to_s_time_format
+ @to_s_time_format ||= '%B %e, %Y'
+ end
+
+ # Sets the format used by IceCube when printing out Schedule#to_s.
+ def self.to_s_time_format=(format)
+ @to_s_time_format = format
+ end
end
View
6 lib/ice_cube/schedule.rb
@@ -239,11 +239,11 @@ def first(n = nil)
def to_s
pieces = []
ed = extimes; rd = rtimes - ed
- pieces.concat rd.sort.map { |t| t.strftime(TO_S_TIME_FORMAT) }
+ pieces.concat rd.sort.map { |t| t.strftime(IceCube.to_s_time_format) }
pieces.concat rrules.map { |t| t.to_s }
pieces.concat exrules.map { |t| "not #{t.to_s}" }
- pieces.concat ed.sort.map { |t| "not on #{t.strftime(TO_S_TIME_FORMAT)}" }
- pieces << "until #{end_time.strftime(TO_S_TIME_FORMAT)}" if end_time
+ pieces.concat ed.sort.map { |t| "not on #{t.strftime(IceCube.to_s_time_format)}" }
+ pieces << "until #{end_time.strftime(IceCube.to_s_time_format)}" if end_time
pieces.join(' / ')
end
View
2  lib/ice_cube/validations/until.rb
@@ -37,7 +37,7 @@ def build_hash(builder)
end
def build_s(builder)
- builder.piece(:until) << "until #{time.strftime(TO_S_TIME_FORMAT)}"
+ builder.piece(:until) << "until #{time.strftime(IceCube.to_s_time_format)}"
end
def validate(t, schedule)
View
12 spec/examples/to_s_spec.rb
@@ -185,4 +185,16 @@
schedule.to_s.should == 'Daily 2 times / until October 31, 2012'
end
+ it 'can have the date format overridden' do
+ old_format = IceCube.to_s_time_format
+ begin
+ IceCube.to_s_time_format = '%B %e, %Y at %I:%M%P'
+ schedule = IceCube::Schedule.new(Time.local(2012, 8, 31), :end_time => Time.local(2012, 10, 31, 17))
+ schedule.add_recurrence_rule IceCube::Rule.daily.count(2)
+ schedule.to_s.should == 'Daily 2 times / until October 31, 2012 at 05:00pm'
+ ensure
+ IceCube.to_s_time_format = old_format
+ end
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.