sugarcube 0.15.3 → 0.15.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,13 +12,12 @@ class NSDate
12
12
  def string_with_style(style)
13
13
  date_formatter = NSDateFormatter.new
14
14
  if style.is_a? Symbol
15
- style = style.nsdatesyle
15
+ style = style.nsdatestyle
16
16
  end
17
17
  date_formatter.setDateStyle(style)
18
18
  date_formatter.stringFromDate(self)
19
19
  end
20
20
 
21
-
22
21
  def string_with_format(format)
23
22
  format_template = NSDateFormatter.dateFormatFromTemplate(format, options:0,
24
23
  locale:NSLocale.currentLocale)
@@ -27,6 +26,38 @@ class NSDate
27
26
  date_formatter.stringFromDate(self)
28
27
  end
29
28
 
29
+ def upto(last_date, delta={days: 1}, &block)
30
+ return if last_date < self
31
+
32
+ date = self
33
+ while date <= last_date
34
+ if block.arity == 0
35
+ block.call
36
+ else
37
+ block.call(date)
38
+ end
39
+ new_date = date.delta(delta)
40
+ break if new_date <= date
41
+ date = new_date
42
+ end
43
+ end
44
+
45
+ def downto(last_date, delta={days: -1}, &block)
46
+ return if last_date > self
47
+
48
+ date = self
49
+ while date >= last_date
50
+ if block.arity == 0
51
+ block.call
52
+ else
53
+ block.call(date)
54
+ end
55
+ new_date = date.delta(delta)
56
+ break if new_date >= date
57
+ date = new_date
58
+ end
59
+ end
60
+
30
61
  def timezone
31
62
  return _calendar_components(NSTimeZoneCalendarUnit).timeZone
32
63
  end
@@ -1,3 +1,3 @@
1
1
  module SugarCube
2
- Version = '0.15.3'
2
+ Version = '0.15.5'
3
3
  end
@@ -0,0 +1,88 @@
1
+ describe "NSDate" do
2
+
3
+ it "Should have an NSDate##from_components method" do
4
+ date = NSDate.from_components(year: 2012, month: 1, day: 2)
5
+ date.year.should == 2012
6
+ date.month.should == 1
7
+ date.day.should == 2
8
+ end
9
+
10
+ before do
11
+ @date = NSDate.from_components(year: 2013, month: 1, day: 2, hour:12, minute: 15, second: 30)
12
+ end
13
+
14
+ it "should have an NSDate#string_with_style(style) method that accepts symbols" do
15
+ @date.string_with_style(:medium).should == 'Jan 2, 2013'
16
+ end
17
+
18
+ it "should have an NSDate#string_with_style(style) method that accepts NSDateStyle constants" do
19
+ @date.string_with_style(NSDateFormatterShortStyle).should == '1/2/13'
20
+ end
21
+
22
+ it "should have an NSDate#string_with_format method (1)" do
23
+ @date.string_with_format("yyyyMMdd HH:mm:ss").should == '01/02/2013, 12:15:30'
24
+ end
25
+
26
+ it "should have an NSDate#string_with_format method (2)" do
27
+ @date.string_with_format("yyyyMMMMd HH:mm:ss").should == 'January 2, 2013, 12:15:30'
28
+ end
29
+
30
+ it "should have an NSDate#timezone method" do
31
+ String.should === @date.timezone.name
32
+ end
33
+
34
+ it "should have an NSDate#era method" do
35
+ @date.era.should == 1
36
+ end
37
+
38
+ it "should have an NSDate#today method" do
39
+ @date.today?.should == false
40
+ NSDate.new.today?.should === true
41
+ end
42
+
43
+ it "should have an NSDate#same_day method" do
44
+ @date.same_day?(@date.start_of_day).should === true
45
+ end
46
+
47
+ it "should have an NSDate#utc_offset method" do
48
+ Fixnum.should === @date.utc_offset
49
+ end
50
+
51
+ it "should have an NSDate#leap_year method" do
52
+ @date.leap_year?.should == false
53
+ NSDate.from_components(year:2012, month:1, day:1).leap_year?.should == true
54
+ end
55
+
56
+ it "should have an NSDate#date_array method" do
57
+ @date.date_array.should == [2013, 1, 2]
58
+ end
59
+
60
+ it "should have an NSDate#time_array method" do
61
+ @date.time_array.should == [12, 15, 30]
62
+ end
63
+
64
+ it "should have an NSDate#datetime_array method" do
65
+ @date.datetime_array.should == [2013, 1, 2, 12, 15, 30]
66
+ end
67
+
68
+ it "should have an NSDate#start_of_day method" do
69
+ @date.start_of_day.datetime_array.should == [2013, 1, 2, 0, 0, 0]
70
+ end
71
+
72
+ it "should have an NSDate#end_of_day method" do
73
+ @date.end_of_day.datetime_array.should == [2013, 1, 3, 0, 0, 0]
74
+ end
75
+
76
+ it "should have an NSDate#days_in_month method" do
77
+ @date.days_in_month.should == 31
78
+ NSDate.from_components(year:2013, month:2, day:1).days_in_month.should == 28
79
+ NSDate.from_components(year:2012, month:2, day:1).days_in_month.should == 29
80
+ end
81
+
82
+ it "should have an NSDate#days_in_year method" do
83
+ @date.days_in_year.should == 365
84
+ NSDate.from_components(year:2013, month:2, day:1).days_in_year.should == 365
85
+ NSDate.from_components(year:2012, month:2, day:1).days_in_year.should == 366
86
+ end
87
+
88
+ end
@@ -0,0 +1,107 @@
1
+ describe "NSDate upto/downto" do
2
+
3
+ before do
4
+ @start = NSDate.from_components(year:2013, month: 1, day: 1)
5
+ end
6
+
7
+ it "should have an #upto method that doesn't do anything if receiver < arg" do
8
+ bam = 0
9
+ @start.upto(@start.delta(days:-1)) do
10
+ bam += 1
11
+ end
12
+ bam.should == 0
13
+ end
14
+
15
+ it "should have a #upto method that should allow the same date" do
16
+ bam = 0
17
+ @start.upto(@start) do
18
+ bam += 1
19
+ end
20
+ bam.should == 1
21
+ end
22
+
23
+ it "should have an #upto method that should count up by days" do
24
+ bam = 0
25
+ @start.upto(@start.delta(days:10)) do
26
+ bam += 1
27
+ end
28
+ bam.should == 11
29
+ end
30
+ it "should have a #downto method that should count down by days" do
31
+ bam = 0
32
+ @start.downto(@start.delta(days:-10)) do
33
+ bam += 1
34
+ end
35
+ bam.should == 11
36
+ end
37
+
38
+
39
+ it "should have an #upto method that should pass the date to the block" do
40
+ @start.upto(@start.delta(days:1)) do |date|
41
+ NSDate.should === date
42
+ end
43
+ end
44
+
45
+ it "should have an #upto method that should count up by any delta" do
46
+ bam = 0
47
+ @start.upto(@start.delta(days:1), hours:1) do
48
+ bam += 1
49
+ end
50
+ bam.should == 25
51
+ end
52
+
53
+ it "should have an #upto method that should abort if delta is negative" do
54
+ bam = 0
55
+ @start.upto(@start.delta(days:1), hours:-1) do
56
+ bam += 1
57
+ end
58
+ bam.should == 1
59
+ end
60
+
61
+ it "should have a #downto method that doesn't do anything if receiver > arg" do
62
+ bam = 0
63
+ @start.downto(@start.delta(days:1)) do
64
+ bam += 1
65
+ end
66
+ bam.should == 0
67
+ end
68
+
69
+ it "should have a #downto method that should allow the same date" do
70
+ bam = 0
71
+ @start.downto(@start) do
72
+ bam += 1
73
+ end
74
+ bam.should == 1
75
+ end
76
+
77
+ it "should have a #downto method that should count down by days" do
78
+ bam = 0
79
+ @start.downto(@start.delta(days:-10)) do
80
+ bam += 1
81
+ end
82
+ bam.should == 11
83
+ end
84
+
85
+ it "should have a #downto method that should pass the date to the block" do
86
+ @start.downto(@start.delta(days:-1)) do |date|
87
+ NSDate.should === date
88
+ end
89
+ end
90
+
91
+ it "should have a #downto method that should count down by any delta" do
92
+ bam = 0
93
+ @start.downto(@start.delta(days:-1), hours:-1) do
94
+ bam += 1
95
+ end
96
+ bam.should == 25
97
+ end
98
+
99
+ it "should have a #downto method that should abort if delta is positive" do
100
+ bam = 0
101
+ @start.downto(@start.delta(days:-1), hours:1) do
102
+ bam += 1
103
+ end
104
+ bam.should == 1
105
+ end
106
+
107
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sugarcube
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.3
4
+ version: 0.15.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2013-02-02 00:00:00.000000000 Z
16
+ date: 2013-02-04 00:00:00.000000000 Z
17
17
  dependencies: []
18
18
  description: ! '== Description
19
19
 
@@ -122,6 +122,8 @@ files:
122
122
  - spec/notification_spec.rb
123
123
  - spec/nsarray_spec.rb
124
124
  - spec/nscoder_spec.rb
125
+ - spec/nsdate_spec.rb
126
+ - spec/nsdate_upto_spec.rb
125
127
  - spec/nsstring_files_spec.rb
126
128
  - spec/numeric_spec.rb
127
129
  - spec/symbol_uicolor_spec.rb
@@ -166,6 +168,8 @@ test_files:
166
168
  - spec/notification_spec.rb
167
169
  - spec/nsarray_spec.rb
168
170
  - spec/nscoder_spec.rb
171
+ - spec/nsdate_spec.rb
172
+ - spec/nsdate_upto_spec.rb
169
173
  - spec/nsstring_files_spec.rb
170
174
  - spec/numeric_spec.rb
171
175
  - spec/symbol_uicolor_spec.rb