motion-support 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) hide show
  1. data/.gitignore +3 -0
  2. data/Gemfile +1 -1
  3. data/README.md +231 -2
  4. data/Rakefile +3 -3
  5. data/app/app_delegate.rb +0 -2
  6. data/examples/Inflector/.gitignore +16 -0
  7. data/examples/Inflector/Gemfile +5 -0
  8. data/examples/Inflector/Rakefile +13 -0
  9. data/examples/Inflector/app/app_delegate.rb +8 -0
  10. data/examples/Inflector/app/inflections.rb +5 -0
  11. data/examples/Inflector/app/inflector_view_controller.rb +120 -0
  12. data/examples/Inflector/app/words_view_controller.rb +101 -0
  13. data/examples/Inflector/resources/Default-568h@2x.png +0 -0
  14. data/examples/Inflector/spec/main_spec.rb +9 -0
  15. data/lib/motion-support/core_ext/array.rb +13 -0
  16. data/lib/motion-support/core_ext/class.rb +8 -0
  17. data/lib/motion-support/core_ext/hash.rb +16 -0
  18. data/lib/motion-support/core_ext/integer.rb +9 -0
  19. data/lib/motion-support/core_ext/module.rb +14 -0
  20. data/lib/motion-support/core_ext/numeric.rb +8 -0
  21. data/lib/motion-support/core_ext/object.rb +14 -0
  22. data/lib/motion-support/core_ext/range.rb +8 -0
  23. data/lib/motion-support/core_ext/string.rb +14 -0
  24. data/lib/motion-support/core_ext/time.rb +19 -0
  25. data/lib/motion-support/core_ext.rb +3 -0
  26. data/lib/motion-support/inflector.rb +12 -156
  27. data/lib/motion-support.rb +5 -5
  28. data/motion/_stdlib/cgi.rb +22 -0
  29. data/motion/_stdlib/date.rb +77 -0
  30. data/motion/_stdlib/time.rb +19 -0
  31. data/motion/core_ext/array/access.rb +28 -0
  32. data/motion/core_ext/array/conversions.rb +86 -0
  33. data/motion/core_ext/array/extract_options.rb +11 -0
  34. data/motion/core_ext/array/grouping.rb +99 -0
  35. data/motion/core_ext/array/prepend_and_append.rb +7 -0
  36. data/motion/core_ext/array/wrap.rb +45 -0
  37. data/{lib/motion-support → motion/core_ext}/array.rb +0 -4
  38. data/motion/core_ext/class/attribute.rb +119 -0
  39. data/motion/core_ext/class/attribute_accessors.rb +168 -0
  40. data/motion/core_ext/date/acts_like.rb +8 -0
  41. data/motion/core_ext/date/calculations.rb +117 -0
  42. data/motion/core_ext/date/conversions.rb +56 -0
  43. data/motion/core_ext/date_and_time/calculations.rb +232 -0
  44. data/motion/core_ext/enumerable.rb +90 -0
  45. data/motion/core_ext/hash/deep_merge.rb +27 -0
  46. data/motion/core_ext/hash/except.rb +15 -0
  47. data/motion/core_ext/hash/indifferent_access.rb +19 -0
  48. data/motion/core_ext/hash/keys.rb +138 -0
  49. data/motion/core_ext/hash/reverse_merge.rb +22 -0
  50. data/motion/core_ext/hash/slice.rb +40 -0
  51. data/motion/core_ext/integer/inflections.rb +27 -0
  52. data/motion/core_ext/integer/multiple.rb +10 -0
  53. data/motion/core_ext/integer/time.rb +41 -0
  54. data/{lib/motion-support → motion/core_ext}/metaclass.rb +0 -0
  55. data/motion/core_ext/module/aliasing.rb +69 -0
  56. data/motion/core_ext/module/anonymous.rb +19 -0
  57. data/motion/core_ext/module/attr_internal.rb +38 -0
  58. data/motion/core_ext/module/attribute_accessors.rb +64 -0
  59. data/motion/core_ext/module/delegation.rb +175 -0
  60. data/motion/core_ext/module/introspection.rb +60 -0
  61. data/motion/core_ext/module/reachable.rb +5 -0
  62. data/motion/core_ext/module/remove_method.rb +12 -0
  63. data/motion/core_ext/ns_dictionary.rb +11 -0
  64. data/motion/core_ext/numeric/bytes.rb +44 -0
  65. data/motion/core_ext/numeric/conversions.rb +7 -0
  66. data/motion/core_ext/numeric/time.rb +75 -0
  67. data/motion/core_ext/object/acts_like.rb +10 -0
  68. data/motion/core_ext/object/blank.rb +105 -0
  69. data/motion/core_ext/object/deep_dup.rb +44 -0
  70. data/motion/core_ext/object/duplicable.rb +83 -0
  71. data/motion/core_ext/object/instance_variables.rb +28 -0
  72. data/motion/core_ext/object/to_param.rb +58 -0
  73. data/motion/core_ext/object/to_query.rb +26 -0
  74. data/motion/core_ext/object/try.rb +78 -0
  75. data/motion/core_ext/range/include_range.rb +23 -0
  76. data/motion/core_ext/range/overlaps.rb +8 -0
  77. data/motion/core_ext/regexp.rb +5 -0
  78. data/motion/core_ext/string/access.rb +104 -0
  79. data/motion/core_ext/string/behavior.rb +6 -0
  80. data/motion/core_ext/string/exclude.rb +11 -0
  81. data/motion/core_ext/string/filters.rb +55 -0
  82. data/motion/core_ext/string/indent.rb +43 -0
  83. data/motion/core_ext/string/inflections.rb +195 -0
  84. data/motion/core_ext/string/starts_ends_with.rb +4 -0
  85. data/motion/core_ext/string/strip.rb +24 -0
  86. data/motion/core_ext/time/acts_like.rb +8 -0
  87. data/motion/core_ext/time/calculations.rb +215 -0
  88. data/motion/core_ext/time/conversions.rb +52 -0
  89. data/motion/duration.rb +104 -0
  90. data/motion/hash_with_indifferent_access.rb +251 -0
  91. data/motion/inflections.rb +67 -0
  92. data/motion/inflector/inflections.rb +203 -0
  93. data/motion/inflector/methods.rb +321 -0
  94. data/{lib/motion-support → motion}/logger.rb +0 -0
  95. data/{lib/motion-support → motion}/version.rb +1 -1
  96. data/motion-support.gemspec +2 -2
  97. data/spec/motion-support/_helpers/constantize_test_cases.rb +75 -0
  98. data/spec/motion-support/_helpers/inflector_test_cases.rb +313 -0
  99. data/spec/motion-support/core_ext/array/access_spec.rb +29 -0
  100. data/spec/motion-support/core_ext/array/conversion_spec.rb +60 -0
  101. data/spec/motion-support/core_ext/array/extract_options_spec.rb +15 -0
  102. data/spec/motion-support/core_ext/array/grouping_spec.rb +85 -0
  103. data/spec/motion-support/core_ext/array/prepend_and_append_spec.rb +25 -0
  104. data/spec/motion-support/core_ext/array/wrap_spec.rb +19 -0
  105. data/spec/motion-support/{array_spec.rb → core_ext/array_spec.rb} +0 -5
  106. data/spec/motion-support/core_ext/class/attribute_accessor_spec.rb +127 -0
  107. data/spec/motion-support/core_ext/class/attribute_spec.rb +92 -0
  108. data/spec/motion-support/core_ext/date/acts_like_spec.rb +11 -0
  109. data/spec/motion-support/core_ext/date/calculation_spec.rb +186 -0
  110. data/spec/motion-support/core_ext/date/conversion_spec.rb +18 -0
  111. data/spec/motion-support/core_ext/date_and_time/calculation_spec.rb +336 -0
  112. data/spec/motion-support/core_ext/enumerable_spec.rb +130 -0
  113. data/spec/motion-support/core_ext/hash/deep_merge_spec.rb +32 -0
  114. data/spec/motion-support/core_ext/hash/except_spec.rb +43 -0
  115. data/spec/motion-support/core_ext/hash/key_spec.rb +230 -0
  116. data/spec/motion-support/core_ext/hash/reverse_merge_spec.rb +26 -0
  117. data/spec/motion-support/core_ext/hash/slice_spec.rb +61 -0
  118. data/spec/motion-support/core_ext/integer/inflection_spec.rb +23 -0
  119. data/spec/motion-support/core_ext/integer/multiple_spec.rb +19 -0
  120. data/spec/motion-support/{metaclass_spec.rb → core_ext/metaclass_spec.rb} +0 -0
  121. data/spec/motion-support/core_ext/module/aliasing_spec.rb +143 -0
  122. data/spec/motion-support/core_ext/module/anonymous_spec.rb +29 -0
  123. data/spec/motion-support/core_ext/module/attr_internal_spec.rb +104 -0
  124. data/spec/motion-support/core_ext/module/attribute_accessor_spec.rb +86 -0
  125. data/spec/motion-support/core_ext/module/delegation_spec.rb +136 -0
  126. data/spec/motion-support/core_ext/module/introspection_spec.rb +70 -0
  127. data/spec/motion-support/core_ext/module/reachable_spec.rb +61 -0
  128. data/spec/motion-support/core_ext/module/remove_method_spec.rb +25 -0
  129. data/spec/motion-support/core_ext/numeric/bytes_spec.rb +43 -0
  130. data/spec/motion-support/core_ext/object/acts_like_spec.rb +21 -0
  131. data/spec/motion-support/core_ext/object/blank_spec.rb +54 -0
  132. data/spec/motion-support/core_ext/object/deep_dup_spec.rb +54 -0
  133. data/spec/motion-support/core_ext/object/duplicable_spec.rb +31 -0
  134. data/spec/motion-support/core_ext/object/instance_variable_spec.rb +19 -0
  135. data/spec/motion-support/core_ext/object/to_param_spec.rb +75 -0
  136. data/spec/motion-support/core_ext/object/to_query_spec.rb +37 -0
  137. data/spec/motion-support/core_ext/object/try_spec.rb +92 -0
  138. data/spec/motion-support/core_ext/range/include_range_spec.rb +31 -0
  139. data/spec/motion-support/core_ext/range/overlap_spec.rb +43 -0
  140. data/spec/motion-support/core_ext/regexp_spec.rb +7 -0
  141. data/spec/motion-support/core_ext/string/access_spec.rb +53 -0
  142. data/spec/motion-support/core_ext/string/behavior_spec.rb +7 -0
  143. data/spec/motion-support/core_ext/string/exclude_spec.rb +8 -0
  144. data/spec/motion-support/core_ext/string/filter_spec.rb +48 -0
  145. data/spec/motion-support/core_ext/string/indent_spec.rb +56 -0
  146. data/spec/motion-support/core_ext/string/inflection_spec.rb +142 -0
  147. data/spec/motion-support/core_ext/string/starts_end_with_spec.rb +14 -0
  148. data/spec/motion-support/core_ext/string/strip_spec.rb +34 -0
  149. data/spec/motion-support/core_ext/string_spec.rb +88 -0
  150. data/spec/motion-support/core_ext/time/acts_like_spec.rb +11 -0
  151. data/spec/motion-support/core_ext/time/calculation_spec.rb +201 -0
  152. data/spec/motion-support/core_ext/time/conversion_spec.rb +54 -0
  153. data/spec/motion-support/duration_spec.rb +107 -0
  154. data/spec/motion-support/hash_with_indifferent_access_spec.rb +605 -0
  155. data/spec/motion-support/inflector_spec.rb +474 -35
  156. data/spec/motion-support/ns_dictionary_spec.rb +29 -0
  157. metadata +212 -35
  158. data/lib/motion-support/cattr_accessor.rb +0 -19
  159. data/lib/motion-support/class_inheritable_accessor.rb +0 -23
  160. data/lib/motion-support/class_inheritable_array.rb +0 -29
  161. data/lib/motion-support/hash.rb +0 -31
  162. data/lib/motion-support/nilclass.rb +0 -5
  163. data/lib/motion-support/object.rb +0 -17
  164. data/lib/motion-support/string.rb +0 -71
  165. data/spec/motion-support/cattr_accessor_spec.rb +0 -49
  166. data/spec/motion-support/class_inheritable_accessor_spec.rb +0 -49
  167. data/spec/motion-support/class_inheritable_array_spec.rb +0 -61
  168. data/spec/motion-support/hash_spec.rb +0 -31
  169. data/spec/motion-support/nilclass_spec.rb +0 -5
  170. data/spec/motion-support/object_spec.rb +0 -43
  171. data/spec/motion-support/string_spec.rb +0 -145
@@ -0,0 +1,92 @@
1
+ describe "class" do
2
+ describe "class_attribute" do
3
+ before do
4
+ @klass = Class.new
5
+ @klass.class_eval { class_attribute :setting }
6
+ @sub = Class.new(@klass)
7
+ end
8
+
9
+ it "should default to nil" do
10
+ @klass.setting.should.be.nil
11
+ @sub.setting.should.be.nil
12
+ end
13
+
14
+ it "should be inheritable" do
15
+ @klass.setting = 1
16
+ @sub.setting.should == 1
17
+ end
18
+
19
+ it "should be overridable" do
20
+ @sub.setting = 1
21
+ @klass.setting.should.be.nil
22
+
23
+ @klass.setting = 2
24
+ @sub.setting.should == 1
25
+
26
+ Class.new(@sub).setting.should == 1
27
+ end
28
+
29
+ it "should define a query method" do
30
+ @klass.setting?.should.be.false
31
+ @klass.setting = 1
32
+ @klass.setting?.should.be.true
33
+ end
34
+
35
+ it "should define an instance reader that delegates to class" do
36
+ @klass.new.setting.should.be.nil
37
+
38
+ @klass.setting = 1
39
+ @klass.new.setting.should == 1
40
+ end
41
+
42
+ it "should allow to override per instance" do
43
+ object = @klass.new
44
+ object.setting = 1
45
+ @klass.setting.should == nil
46
+ @klass.setting = 2
47
+ object.setting.should == 1
48
+ end
49
+
50
+ it "should define query method on instance" do
51
+ object = @klass.new
52
+ object.setting?.should.be.false
53
+ object.setting = 1
54
+ object.setting?.should.be.true
55
+ end
56
+
57
+ describe "instance_writer => false" do
58
+ it "should not create instance writer" do
59
+ object = Class.new { class_attribute :setting, :instance_writer => false }.new
60
+ lambda { object.setting = 'boom' }.should.raise NoMethodError
61
+ end
62
+ end
63
+
64
+ describe "instance_reader => false" do
65
+ it "should not create instance reader" do
66
+ object = Class.new { class_attribute :setting, :instance_reader => false }.new
67
+ lambda { object.setting }.should.raise NoMethodError
68
+ lambda { object.setting? }.should.raise NoMethodError
69
+ end
70
+ end
71
+
72
+ describe "instance_accessor => false" do
73
+ it "should not create reader or writer" do
74
+ object = Class.new { class_attribute :setting, :instance_accessor => false }.new
75
+ lambda { object.setting }.should.raise NoMethodError
76
+ lambda { object.setting? }.should.raise NoMethodError
77
+ lambda { object.setting = 'boom' }.should.raise NoMethodError
78
+ end
79
+ end
80
+
81
+ it "should work well with singleton classes" do
82
+ object = @klass.new
83
+ object.singleton_class.setting = 'foo'
84
+ object.setting.should == "foo"
85
+ end
86
+
87
+ it "should return set value through setter" do
88
+ val = @klass.send(:setting=, 1)
89
+ val.should == 1
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,11 @@
1
+ describe "Date" do
2
+ describe "acts_like" do
3
+ it "should act like date" do
4
+ Date.new.should.acts_like(:date)
5
+ end
6
+
7
+ it "should not act like time" do
8
+ Date.new.should.not.acts_like(:time)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,186 @@
1
+ describe "Date" do
2
+ describe "calculations" do
3
+ describe "yesterday" do
4
+ it "should calculate yesterday's date" do
5
+ Date.new(1982,10,15).yesterday.should == Date.new(1982,10,14)
6
+ end
7
+
8
+ it "should construct yesterday's date" do
9
+ Date.yesterday.should == Date.current - 1
10
+ end
11
+ end
12
+
13
+ describe "tomorrow" do
14
+ it "should calculate tomorrow's date" do
15
+ Date.new(1982,10,4).tomorrow.should == Date.new(1982,10,5)
16
+ end
17
+
18
+ it "should construct tomorrow's date" do
19
+ Date.tomorrow.should == Date.current + 1
20
+ end
21
+ end
22
+
23
+ describe "change" do
24
+ it "should change correctly" do
25
+ Date.new(2005,2,11).change(:day => 21).should == Date.new(2005,2,21)
26
+ Date.new(2005,2,11).change(:year => 2007, :month => 5).should == Date.new(2007,5,11)
27
+ Date.new(2005,2,22).change(:year => 2006).should == Date.new(2006,2,22)
28
+ Date.new(2005,2,22).change(:month => 6).should == Date.new(2005,6,22)
29
+ end
30
+ end
31
+
32
+ describe "sunday" do
33
+ it "should calculate correctly" do
34
+ Date.new(2008,3,02).sunday.should == Date.new(2008,3,2)
35
+ Date.new(2008,2,29).sunday.should == Date.new(2008,3,2)
36
+ end
37
+ end
38
+
39
+ describe "beginning_of_week" do
40
+ it "should calculate correctly" do
41
+ Date.new(1982,10,15).beginning_of_week.should == Date.new(1982,10,11)
42
+ end
43
+ end
44
+
45
+ describe "end_of_week" do
46
+ it "should calculate correctly" do
47
+ Date.new(1982,10,4).end_of_week.should == Date.new(1982,10,10)
48
+ end
49
+ end
50
+
51
+ describe "end_of_year" do
52
+ it "should calculate correctly" do
53
+ Date.new(2008,2,22).end_of_year.should == Date.new(2008,12,31)
54
+ end
55
+ end
56
+
57
+ describe "end_of_month" do
58
+ it "should calculate correctly" do
59
+ Date.new(2005,3,20).end_of_month.should == Date.new(2005,3,31)
60
+ Date.new(2005,2,20).end_of_month.should == Date.new(2005,2,28)
61
+ Date.new(2005,4,20).end_of_month.should == Date.new(2005,4,30)
62
+ end
63
+ end
64
+
65
+ describe "prev_year" do
66
+ it "should calculate correctly" do
67
+ Date.new(1983,10,14).prev_year.should == Date.new(1982,10,14)
68
+ end
69
+
70
+ it "should work with leap years" do
71
+ Date.new(2000,2,29).prev_year.should == Date.new(1999,2,28)
72
+ end
73
+ end
74
+
75
+ describe "last_year" do
76
+ it "should calculate correctly" do
77
+ Date.new(2005,6,5).last_year.should == Date.new(2004,6,5)
78
+ Date.new(1983,10,14).last_year.should == Date.new(1982,10,14)
79
+ end
80
+
81
+ it "should work with leap years" do
82
+ Date.new(2000,2,29).last_year.should == Date.new(1999,2,28)
83
+ end
84
+ end
85
+
86
+ describe "next_year" do
87
+ it "should calculate correctly" do
88
+ Date.new(1981,10,10).next_year.should == Date.new(1982,10,10)
89
+ end
90
+
91
+ it "should work with leap years" do
92
+ Date.new(2000,2,29).next_year.should == Date.new(2001,2,28)
93
+ end
94
+ end
95
+
96
+ describe "advance" do
97
+ it "should calculate correctly" do
98
+ Date.new(2005,2,28).advance(:years => 1).should == Date.new(2006,2,28)
99
+ Date.new(2005,2,28).advance(:months => 4).should == Date.new(2005,6,28)
100
+ Date.new(2005,2,28).advance(:weeks => 3).should == Date.new(2005,3,21)
101
+ Date.new(2005,2,28).advance(:days => 5).should == Date.new(2005,3,5)
102
+ Date.new(2005,2,28).advance(:years => 7, :months => 7).should == Date.new(2012,9,28)
103
+ Date.new(2005,2,28).advance(:years => 7, :months => 19, :days => 5).should == Date.new(2013,10,3)
104
+ Date.new(2005,2,28).advance(:years => 7, :months => 19, :weeks => 2, :days => 5).should == Date.new(2013,10,17)
105
+ Date.new(2004,2,29).advance(:years => 1).should == Date.new(2005,2,28)
106
+ end
107
+
108
+ it "should advance years before days" do
109
+ Date.new(2011, 2, 28).advance(:years => 1, :days => 1).should == Date.new(2012, 2, 29)
110
+ end
111
+
112
+ it "should advance months before days" do
113
+ Date.new(2010, 2, 28).advance(:months => 1, :days => 1).should == Date.new(2010, 3, 29)
114
+ end
115
+
116
+ it "should not change passed option hash" do
117
+ options = { :years => 3, :months => 11, :days => 2 }
118
+ Date.new(2005,2,28).advance(options)
119
+ options.should == { :years => 3, :months => 11, :days => 2 }
120
+ end
121
+ end
122
+
123
+ describe "last_week" do
124
+ it "should calculate correctly" do
125
+ Date.new(2005,5,17).last_week.should == Date.new(2005,5,9)
126
+ Date.new(2007,1,7).last_week.should == Date.new(2006,12,25)
127
+ Date.new(2010,2,19).last_week(:friday).should == Date.new(2010,2,12)
128
+ Date.new(2010,2,19).last_week(:saturday).should == Date.new(2010,2,13)
129
+ Date.new(2010,3,4).last_week(:saturday).should == Date.new(2010,2,27)
130
+ end
131
+ end
132
+
133
+ describe "last_month" do
134
+ it "should calculate correctly on the 31st" do
135
+ Date.new(2004, 3, 31).last_month.should == Date.new(2004, 2, 29)
136
+
137
+ end
138
+ end
139
+
140
+ describe "last_quarter" do
141
+ it "should calculate correctly on the 31st" do
142
+ Date.new(2004, 5, 31).last_quarter.should == Date.new(2004, 2, 29)
143
+ end
144
+ end
145
+
146
+ describe "since" do
147
+ it "should calculate correctly" do
148
+ Date.new(2005,2,21).since(45).should == Time.local(2005,2,21,0,0,45)
149
+ end
150
+ end
151
+
152
+ describe "ago" do
153
+ it "should calculate correctly" do
154
+ Date.new(2005,2,21).ago(45).should == Time.local(2005,2,20,23,59,15)
155
+ end
156
+ end
157
+
158
+ describe "beginning_of_day" do
159
+ it "should calculate correctly" do
160
+ Date.new(2005,2,21).beginning_of_day.should == Time.local(2005,2,21,0,0,0)
161
+ end
162
+ end
163
+
164
+ describe "end_of_day" do
165
+ it "should calculate correctly" do
166
+ Date.new(2005,2,21).end_of_day.should == Time.local(2005,2,21,23,59,59,Rational(999999999, 1000))
167
+ end
168
+ end
169
+
170
+ describe "past?" do
171
+ it "should calculate correctly" do
172
+ Date.yesterday.should.be.past
173
+ Date.today.last_week.should.be.past
174
+ Date.tomorrow.should.not.be.past
175
+ end
176
+ end
177
+
178
+ describe "future?" do
179
+ it "should calculate correctly" do
180
+ Date.tomorrow.should.be.future
181
+ Date.today.next_week.should.be.future
182
+ Date.yesterday.should.not.be.future
183
+ end
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,18 @@
1
+ describe "date" do
2
+ describe "conversions" do
3
+ it "should convert date to string" do
4
+ date = Date.new(2005, 2, 21)
5
+ date.to_s.should == "2005-2-21"
6
+ date.to_s(:short).should == "21 Feb"
7
+ date.to_s(:long).should == "February 21, 2005"
8
+ date.to_s(:long_ordinal).should == "February 21st, 2005"
9
+ date.to_s(:db).should == "2005-02-21"
10
+ date.to_s(:rfc822).should == "21 Feb 2005"
11
+ end
12
+
13
+ it "should make inspect readable" do
14
+ Date.new(2005, 2, 21).readable_inspect.should == "Mon, 21 Feb 2005"
15
+ Date.new(2005, 2, 21).readable_inspect.should == Date.new(2005, 2, 21).inspect
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,336 @@
1
+ class Date
2
+ def self.for_spec(year, month, day, hour, minute, second, usec)
3
+ new(year, month, day)
4
+ end
5
+ end
6
+
7
+ class Time
8
+ def self.for_spec(year, month, day, hour, minute, second, usec)
9
+ local(year, month, day, hour, minute, second, usec)
10
+ end
11
+ end
12
+
13
+ [Date, Time].each do |klass|
14
+ describe klass do
15
+ @klass = klass
16
+ def date_or_time(year, month, day, hour, minute, second, usec = nil)
17
+ @klass.for_spec(year, month, day, hour, minute, second, usec)
18
+ end
19
+
20
+ def with_bw_default(bw = :monday)
21
+ old_bw = Date.beginning_of_week
22
+ Date.beginning_of_week = bw
23
+ yield
24
+ ensure
25
+ Date.beginning_of_week = old_bw
26
+ end
27
+
28
+ describe "yesterday" do
29
+ it "should be calculated correctly" do
30
+ date_or_time(2005,2,22,10,10,10).yesterday.should == date_or_time(2005,2,21,10,10,10)
31
+ date_or_time(2005,3,2,10,10,10).yesterday.yesterday.should == date_or_time(2005,2,28,10,10,10)
32
+ end
33
+ end
34
+
35
+ describe "tomorrow" do
36
+ it "should be calculated correctly" do
37
+ date_or_time(2005,2,22,10,10,10).tomorrow.should == date_or_time(2005,2,23,10,10,10)
38
+ date_or_time(2005,2,28,10,10,10).tomorrow.tomorrow.should == date_or_time(2005,3,2,10,10,10)
39
+ end
40
+ end
41
+
42
+ describe "days_ago" do
43
+ it "should be calculated correctly" do
44
+ date_or_time(2005,6,5,10,10,10).days_ago(1).should == date_or_time(2005,6,4,10,10,10)
45
+ date_or_time(2005,6,5,10,10,10).days_ago(5).should == date_or_time(2005,5,31,10,10,10)
46
+ end
47
+ end
48
+
49
+ describe "days_since" do
50
+ it "should be calculated correctly" do
51
+ date_or_time(2005,6,5,10,10,10).days_since(1).should == date_or_time(2005,6,6,10,10,10)
52
+ date_or_time(2004,12,31,10,10,10).days_since(1).should == date_or_time(2005,1,1,10,10,10)
53
+ end
54
+ end
55
+
56
+ describe "weeks_ago" do
57
+ it "should be calculated correctly" do
58
+ date_or_time(2005,6,5,10,10,10).weeks_ago(1).should == date_or_time(2005,5,29,10,10,10)
59
+ date_or_time(2005,6,5,10,10,10).weeks_ago(5).should == date_or_time(2005,5,1,10,10,10)
60
+ date_or_time(2005,6,5,10,10,10).weeks_ago(6).should == date_or_time(2005,4,24,10,10,10)
61
+ date_or_time(2005,6,5,10,10,10).weeks_ago(14).should == date_or_time(2005,2,27,10,10,10)
62
+ date_or_time(2005,1,1,10,10,10).weeks_ago(1).should == date_or_time(2004,12,25,10,10,10)
63
+ end
64
+ end
65
+
66
+ describe "weeks_since" do
67
+ it "should be calculated correctly" do
68
+ date_or_time(2005,7,7,10,10,10).weeks_since(1).should == date_or_time(2005,7,14,10,10,10)
69
+ date_or_time(2005,7,7,10,10,10).weeks_since(1).should == date_or_time(2005,7,14,10,10,10)
70
+ date_or_time(2005,6,27,10,10,10).weeks_since(1).should == date_or_time(2005,7,4,10,10,10)
71
+ date_or_time(2004,12,28,10,10,10).weeks_since(1).should == date_or_time(2005,1,4,10,10,10)
72
+ end
73
+ end
74
+
75
+ describe "months_ago" do
76
+ it "should be calculated correctly" do
77
+ date_or_time(2005,6,5,10,10,10).months_ago(1).should == date_or_time(2005,5,5,10,10,10)
78
+ date_or_time(2005,6,5,10,10,10).months_ago(7).should == date_or_time(2004,11,5,10,10,10)
79
+ date_or_time(2005,6,5,10,10,10).months_ago(6).should == date_or_time(2004,12,5,10,10,10)
80
+ date_or_time(2005,6,5,10,10,10).months_ago(12).should == date_or_time(2004,6,5,10,10,10)
81
+ date_or_time(2005,6,5,10,10,10).months_ago(24).should == date_or_time(2003,6,5,10,10,10)
82
+ end
83
+ end
84
+
85
+ describe "months_since" do
86
+ it "should be calculated correctly" do
87
+ date_or_time(2005,6,5,10,10,10).months_since(1).should == date_or_time(2005,7,5,10,10,10)
88
+ date_or_time(2005,12,5,10,10,10).months_since(1).should == date_or_time(2006,1,5,10,10,10)
89
+ date_or_time(2005,6,5,10,10,10).months_since(6).should == date_or_time(2005,12,5,10,10,10)
90
+ date_or_time(2005,12,5,10,10,10).months_since(6).should == date_or_time(2006,6,5,10,10,10)
91
+ date_or_time(2005,6,5,10,10,10).months_since(7).should == date_or_time(2006,1,5,10,10,10)
92
+ date_or_time(2005,6,5,10,10,10).months_since(12).should == date_or_time(2006,6,5,10,10,10)
93
+ date_or_time(2005,6,5,10,10,10).months_since(24).should == date_or_time(2007,6,5,10,10,10)
94
+ date_or_time(2005,3,31,10,10,10).months_since(1).should == date_or_time(2005,4,30,10,10,10)
95
+ date_or_time(2005,1,29,10,10,10).months_since(1).should == date_or_time(2005,2,28,10,10,10)
96
+ date_or_time(2005,1,30,10,10,10).months_since(1).should == date_or_time(2005,2,28,10,10,10)
97
+ date_or_time(2005,1,31,10,10,10).months_since(1).should == date_or_time(2005,2,28,10,10,10)
98
+ end
99
+ end
100
+
101
+ describe "years_ago" do
102
+ it "should be calculated correctly" do
103
+ date_or_time(2005,6,5,10,10,10).years_ago(1).should == date_or_time(2004,6,5,10,10,10)
104
+ date_or_time(2005,6,5,10,10,10).years_ago(7).should == date_or_time(1998,6,5,10,10,10)
105
+ # 1 year ago from leap day
106
+ date_or_time(2004,2,29,10,10,10).years_ago(1).should == date_or_time(2003,2,28,10,10,10)
107
+ end
108
+ end
109
+
110
+ describe "years_since" do
111
+ it "should be calculated correctly" do
112
+ date_or_time(2005,6,5,10,10,10).years_since(1).should == date_or_time(2006,6,5,10,10,10)
113
+ date_or_time(2005,6,5,10,10,10).years_since(7).should == date_or_time(2012,6,5,10,10,10)
114
+ # 1 year since leap day
115
+ date_or_time(2004,2,29,10,10,10).years_since(1).should == date_or_time(2005,2,28,10,10,10)
116
+ end
117
+ end
118
+
119
+ describe "beginning_of_month" do
120
+ it "should be calculated correctly" do
121
+ date_or_time(2005,2,22,10,10,10).beginning_of_month.should == date_or_time(2005,2,1,0,0,0)
122
+ end
123
+ end
124
+
125
+ describe "beginning_of_quarter" do
126
+ it "should be calculated correctly" do
127
+ date_or_time(2005,2,15,10,10,10).beginning_of_quarter.should == date_or_time(2005,1,1,0,0,0)
128
+ date_or_time(2005,1,1,0,0,0).beginning_of_quarter.should == date_or_time(2005,1,1,0,0,0)
129
+ date_or_time(2005,12,31,10,10,10).beginning_of_quarter.should == date_or_time(2005,10,1,0,0,0)
130
+ date_or_time(2005,6,30,23,59,59).beginning_of_quarter.should == date_or_time(2005,4,1,0,0,0)
131
+ end
132
+ end
133
+
134
+ describe "end_of_quarter" do
135
+ it "should be calculated correctly" do
136
+ date_or_time(2007,2,15,10,10,10).end_of_quarter.should == date_or_time(2007,3,31,23,59,59,Rational(999999999, 1000))
137
+ date_or_time(2007,3,31,0,0,0).end_of_quarter.should == date_or_time(2007,3,31,23,59,59,Rational(999999999, 1000))
138
+ date_or_time(2007,12,21,10,10,10).end_of_quarter.should == date_or_time(2007,12,31,23,59,59,Rational(999999999, 1000))
139
+ date_or_time(2007,4,1,0,0,0).end_of_quarter.should == date_or_time(2007,6,30,23,59,59,Rational(999999999, 1000))
140
+ date_or_time(2008,5,31,0,0,0).end_of_quarter.should == date_or_time(2008,6,30,23,59,59,Rational(999999999, 1000))
141
+ end
142
+ end
143
+
144
+ describe "beginning_of_year" do
145
+ it "should be calculated correctly" do
146
+ date_or_time(2005,2,22,10,10,10).beginning_of_year.should == date_or_time(2005,1,1,0,0,0)
147
+ end
148
+ end
149
+
150
+ describe "next_week" do
151
+ it "should be calculated correctly" do
152
+ date_or_time(2005,2,22,15,15,10).next_week.should == date_or_time(2005,2,28,0,0,0)
153
+ date_or_time(2005,2,22,15,15,10).next_week(:friday).should == date_or_time(2005,3,4,0,0,0)
154
+ date_or_time(2006,10,23,0,0,0).next_week.should == date_or_time(2006,10,30,0,0,0)
155
+ date_or_time(2006,10,23,0,0,0).next_week(:wednesday).should == date_or_time(2006,11,1,0,0,0)
156
+ end
157
+
158
+ it "should be calculated correctly with default beginning of week" do
159
+ # calling with_bw_default produces LocalJumpError, even though a block is given
160
+ begin
161
+ old_bw = Date.beginning_of_week
162
+ Date.beginning_of_week = :tuesday
163
+
164
+ date_or_time(2012,3,21,0,0,0).next_week(:wednesday).should == date_or_time(2012,3,28,0,0,0)
165
+ date_or_time(2012,3,21,0,0,0).next_week(:saturday).should == date_or_time(2012,3,31,0,0,0)
166
+ date_or_time(2012,3,21,0,0,0).next_week(:tuesday).should == date_or_time(2012,3,27,0,0,0)
167
+ date_or_time(2012,3,21,0,0,0).next_week(:monday).should == date_or_time(2012,4,02,0,0,0)
168
+ ensure
169
+ Date.beginning_of_week = old_bw
170
+ end
171
+ end
172
+ end
173
+
174
+ describe "next_month" do
175
+ it "should be calculated correctly" do
176
+ date_or_time(2005,8,31,15,15,10).next_month.should == date_or_time(2005,9,30,15,15,10)
177
+ end
178
+ end
179
+
180
+ describe "next_quarter" do
181
+ it "should be calculated correctly" do
182
+ date_or_time(2005,8,31,15,15,10).next_quarter.should == date_or_time(2005,11,30,15,15,10)
183
+ end
184
+ end
185
+
186
+ describe "next_year" do
187
+ it "should be calculated correctly" do
188
+ date_or_time(2005,6,5,10,10,10).next_year.should == date_or_time(2006,6,5,10,10,10)
189
+ end
190
+ end
191
+
192
+ describe "prev_week" do
193
+ it "should be calculated correctly" do
194
+ date_or_time(2005,3,1,15,15,10).prev_week.should == date_or_time(2005,2,21,0,0,0)
195
+ date_or_time(2005,3,1,15,15,10).prev_week(:tuesday).should == date_or_time(2005,2,22,0,0,0)
196
+ date_or_time(2005,3,1,15,15,10).prev_week(:friday).should == date_or_time(2005,2,25,0,0,0)
197
+ date_or_time(2006,11,6,0,0,0).prev_week.should == date_or_time(2006,10,30,0,0,0)
198
+ date_or_time(2006,11,23,0,0,0).prev_week(:wednesday).should == date_or_time(2006,11,15,0,0,0)
199
+ end
200
+
201
+ it "should be calculated correctly with default beginning of week" do
202
+ begin
203
+ old_bw = Date.beginning_of_week
204
+ Date.beginning_of_week = :tuesday
205
+
206
+ date_or_time(2012,3,21,0,0,0).prev_week(:wednesday).should == date_or_time(2012,3,14,0,0,0)
207
+ date_or_time(2012,3,21,0,0,0).prev_week(:saturday).should == date_or_time(2012,3,17,0,0,0)
208
+ date_or_time(2012,3,21,0,0,0).prev_week(:tuesday).should == date_or_time(2012,3,13,0,0,0)
209
+ date_or_time(2012,3,21,0,0,0).prev_week(:monday).should == date_or_time(2012,3,19,0,0,0)
210
+ ensure
211
+ Date.beginning_of_week = old_bw
212
+ end
213
+ end
214
+ end
215
+
216
+ describe "prev_month" do
217
+ it "should be calculated correctly" do
218
+ date_or_time(2004,3,31,10,10,10).prev_month.should == date_or_time(2004,2,29,10,10,10)
219
+ end
220
+ end
221
+
222
+ describe "prev_quarter" do
223
+ it "should be calculated correctly" do
224
+ date_or_time(2004,5,31,10,10,10).prev_quarter.should == date_or_time(2004,2,29,10,10,10)
225
+ end
226
+ end
227
+
228
+ describe "prev_year" do
229
+ it "should be calculated correctly" do
230
+ date_or_time(2005,6,5,10,10,10).prev_year.should == date_or_time(2004,6,5,10,10,10)
231
+ end
232
+ end
233
+
234
+ describe "days_to_week_start" do
235
+ it "should be calculated correctly" do
236
+ date_or_time(2011,11,01,0,0,0).days_to_week_start(:tuesday).should == 0
237
+ date_or_time(2011,11,02,0,0,0).days_to_week_start(:tuesday).should == 1
238
+ date_or_time(2011,11,03,0,0,0).days_to_week_start(:tuesday).should == 2
239
+ date_or_time(2011,11,04,0,0,0).days_to_week_start(:tuesday).should == 3
240
+ date_or_time(2011,11,05,0,0,0).days_to_week_start(:tuesday).should == 4
241
+ date_or_time(2011,11,06,0,0,0).days_to_week_start(:tuesday).should == 5
242
+ date_or_time(2011,11,07,0,0,0).days_to_week_start(:tuesday).should == 6
243
+
244
+ date_or_time(2011,11,03,0,0,0).days_to_week_start(:monday).should == 3
245
+ date_or_time(2011,11,04,0,0,0).days_to_week_start(:tuesday).should == 3
246
+ date_or_time(2011,11,05,0,0,0).days_to_week_start(:wednesday).should == 3
247
+ date_or_time(2011,11,06,0,0,0).days_to_week_start(:thursday).should == 3
248
+ date_or_time(2011,11,07,0,0,0).days_to_week_start(:friday).should == 3
249
+ date_or_time(2011,11,8,0,0,0).days_to_week_start(:saturday).should == 3
250
+ date_or_time(2011,11,9,0,0,0).days_to_week_start(:sunday).should == 3
251
+ end
252
+
253
+ it "should be calculated correctly with default beginning of week" do
254
+ begin
255
+ old_bw = Date.beginning_of_week
256
+ Date.beginning_of_week = :friday
257
+
258
+ date_or_time(2012,03,8,0,0,0).days_to_week_start.should == 6
259
+ date_or_time(2012,03,7,0,0,0).days_to_week_start.should == 5
260
+ date_or_time(2012,03,6,0,0,0).days_to_week_start.should == 4
261
+ date_or_time(2012,03,5,0,0,0).days_to_week_start.should == 3
262
+ date_or_time(2012,03,4,0,0,0).days_to_week_start.should == 2
263
+ date_or_time(2012,03,3,0,0,0).days_to_week_start.should == 1
264
+ date_or_time(2012,03,2,0,0,0).days_to_week_start.should == 0
265
+ ensure
266
+ Date.beginning_of_week = old_bw
267
+ end
268
+ end
269
+ end
270
+
271
+ describe "beginning_of_week" do
272
+ it "should be calculated correctly" do
273
+ date_or_time(2005,2,4,10,10,10).beginning_of_week.should == date_or_time(2005,1,31,0,0,0)
274
+ date_or_time(2005,11,28,0,0,0).beginning_of_week.should == date_or_time(2005,11,28,0,0,0)
275
+ date_or_time(2005,11,29,0,0,0).beginning_of_week.should == date_or_time(2005,11,28,0,0,0)
276
+ date_or_time(2005,11,30,0,0,0).beginning_of_week.should == date_or_time(2005,11,28,0,0,0)
277
+ date_or_time(2005,12,01,0,0,0).beginning_of_week.should == date_or_time(2005,11,28,0,0,0)
278
+ date_or_time(2005,12,02,0,0,0).beginning_of_week.should == date_or_time(2005,11,28,0,0,0)
279
+ date_or_time(2005,12,03,0,0,0).beginning_of_week.should == date_or_time(2005,11,28,0,0,0)
280
+ date_or_time(2005,12,04,0,0,0).beginning_of_week.should == date_or_time(2005,11,28,0,0,0)
281
+ end
282
+ end
283
+
284
+ describe "end_of_week" do
285
+ it "should be calculated correctly" do
286
+ date_or_time(2007,12,31,10,10,10).end_of_week.should == date_or_time(2008,1,6,23,59,59,Rational(999999999, 1000))
287
+ date_or_time(2007,8,27,0,0,0).end_of_week.should == date_or_time(2007,9,2,23,59,59,Rational(999999999, 1000))
288
+ date_or_time(2007,8,28,0,0,0).end_of_week.should == date_or_time(2007,9,2,23,59,59,Rational(999999999, 1000))
289
+ date_or_time(2007,8,29,0,0,0).end_of_week.should == date_or_time(2007,9,2,23,59,59,Rational(999999999, 1000))
290
+ date_or_time(2007,8,30,0,0,0).end_of_week.should == date_or_time(2007,9,2,23,59,59,Rational(999999999, 1000))
291
+ date_or_time(2007,8,31,0,0,0).end_of_week.should == date_or_time(2007,9,2,23,59,59,Rational(999999999, 1000))
292
+ date_or_time(2007,9,01,0,0,0).end_of_week.should == date_or_time(2007,9,2,23,59,59,Rational(999999999, 1000))
293
+ date_or_time(2007,9,02,0,0,0).end_of_week.should == date_or_time(2007,9,2,23,59,59,Rational(999999999, 1000))
294
+ end
295
+ end
296
+
297
+ describe "end_of_month" do
298
+ it "should be calculated correctly" do
299
+ date_or_time(2005,3,20,10,10,10).end_of_month.should == date_or_time(2005,3,31,23,59,59,Rational(999999999, 1000))
300
+ date_or_time(2005,2,20,10,10,10).end_of_month.should == date_or_time(2005,2,28,23,59,59,Rational(999999999, 1000))
301
+ date_or_time(2005,4,20,10,10,10).end_of_month.should == date_or_time(2005,4,30,23,59,59,Rational(999999999, 1000))
302
+ end
303
+ end
304
+
305
+ describe "end_of_year" do
306
+ it "should be calculated correctly" do
307
+ date_or_time(2007,2,22,10,10,10).end_of_year.should == date_or_time(2007,12,31,23,59,59,Rational(999999999, 1000))
308
+ date_or_time(2007,12,31,10,10,10).end_of_year.should == date_or_time(2007,12,31,23,59,59,Rational(999999999, 1000))
309
+ end
310
+ end
311
+
312
+ describe "weekdays" do
313
+ it "should return monday with default beginning of week" do
314
+ begin
315
+ old_bw = Date.beginning_of_week
316
+ Date.beginning_of_week = :saturday
317
+
318
+ date_or_time(2012,9,18,0,0,0).monday.should == date_or_time(2012,9,17,0,0,0)
319
+ ensure
320
+ Date.beginning_of_week = old_bw
321
+ end
322
+ end
323
+
324
+ it "should return sunday with default beginning of week" do
325
+ begin
326
+ old_bw = Date.beginning_of_week
327
+ Date.beginning_of_week = :wednesday
328
+
329
+ date_or_time(2012,9,19,0,0,0).sunday.should == date_or_time(2012,9,23,23,59,59, Rational(999999999, 1000))
330
+ ensure
331
+ Date.beginning_of_week = old_bw
332
+ end
333
+ end
334
+ end
335
+ end
336
+ end