motion_blender-support 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (182) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.yardopts +1 -0
  4. data/Gemfile +4 -0
  5. data/HACKS.md +7 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +359 -0
  8. data/Rakefile +14 -0
  9. data/app/app_delegate.rb +5 -0
  10. data/examples/Inflector/.gitignore +16 -0
  11. data/examples/Inflector/Gemfile +5 -0
  12. data/examples/Inflector/Rakefile +13 -0
  13. data/examples/Inflector/app/app_delegate.rb +8 -0
  14. data/examples/Inflector/app/inflections.rb +5 -0
  15. data/examples/Inflector/app/inflector_view_controller.rb +109 -0
  16. data/examples/Inflector/app/words_view_controller.rb +101 -0
  17. data/examples/Inflector/resources/Default-568h@2x.png +0 -0
  18. data/examples/Inflector/spec/main_spec.rb +9 -0
  19. data/lib/motion-support/callbacks.rb +8 -0
  20. data/lib/motion-support/concern.rb +4 -0
  21. data/lib/motion-support/core_ext/array.rb +10 -0
  22. data/lib/motion-support/core_ext/class.rb +5 -0
  23. data/lib/motion-support/core_ext/hash.rb +13 -0
  24. data/lib/motion-support/core_ext/integer.rb +6 -0
  25. data/lib/motion-support/core_ext/module.rb +11 -0
  26. data/lib/motion-support/core_ext/numeric.rb +6 -0
  27. data/lib/motion-support/core_ext/object.rb +12 -0
  28. data/lib/motion-support/core_ext/range.rb +5 -0
  29. data/lib/motion-support/core_ext/string.rb +13 -0
  30. data/lib/motion-support/core_ext/time.rb +16 -0
  31. data/lib/motion-support/core_ext.rb +13 -0
  32. data/lib/motion-support/inflector.rb +8 -0
  33. data/lib/motion-support.rb +81 -0
  34. data/motion/_stdlib/array.rb +13 -0
  35. data/motion/_stdlib/cgi.rb +22 -0
  36. data/motion/_stdlib/date.rb +81 -0
  37. data/motion/_stdlib/enumerable.rb +9 -0
  38. data/motion/_stdlib/time.rb +19 -0
  39. data/motion/callbacks.rb +511 -0
  40. data/motion/concern.rb +122 -0
  41. data/motion/core_ext/array/access.rb +28 -0
  42. data/motion/core_ext/array/conversions.rb +86 -0
  43. data/motion/core_ext/array/extract_options.rb +11 -0
  44. data/motion/core_ext/array/grouping.rb +99 -0
  45. data/motion/core_ext/array/prepend_and_append.rb +7 -0
  46. data/motion/core_ext/array/wrap.rb +45 -0
  47. data/motion/core_ext/array.rb +19 -0
  48. data/motion/core_ext/class/attribute.rb +119 -0
  49. data/motion/core_ext/class/attribute_accessors.rb +168 -0
  50. data/motion/core_ext/date/acts_like.rb +8 -0
  51. data/motion/core_ext/date/calculations.rb +117 -0
  52. data/motion/core_ext/date/conversions.rb +56 -0
  53. data/motion/core_ext/date_and_time/calculations.rb +232 -0
  54. data/motion/core_ext/enumerable.rb +90 -0
  55. data/motion/core_ext/hash/deep_delete_if.rb +23 -0
  56. data/motion/core_ext/hash/deep_merge.rb +27 -0
  57. data/motion/core_ext/hash/except.rb +15 -0
  58. data/motion/core_ext/hash/indifferent_access.rb +19 -0
  59. data/motion/core_ext/hash/keys.rb +150 -0
  60. data/motion/core_ext/hash/reverse_merge.rb +22 -0
  61. data/motion/core_ext/hash/slice.rb +40 -0
  62. data/motion/core_ext/integer/inflections.rb +27 -0
  63. data/motion/core_ext/integer/multiple.rb +10 -0
  64. data/motion/core_ext/integer/time.rb +41 -0
  65. data/motion/core_ext/kernel/singleton_class.rb +6 -0
  66. data/motion/core_ext/metaclass.rb +8 -0
  67. data/motion/core_ext/module/aliasing.rb +69 -0
  68. data/motion/core_ext/module/anonymous.rb +19 -0
  69. data/motion/core_ext/module/attr_internal.rb +38 -0
  70. data/motion/core_ext/module/attribute_accessors.rb +64 -0
  71. data/motion/core_ext/module/delegation.rb +175 -0
  72. data/motion/core_ext/module/introspection.rb +60 -0
  73. data/motion/core_ext/module/reachable.rb +5 -0
  74. data/motion/core_ext/module/remove_method.rb +12 -0
  75. data/motion/core_ext/ns_dictionary.rb +14 -0
  76. data/motion/core_ext/ns_string.rb +14 -0
  77. data/motion/core_ext/numeric/bytes.rb +44 -0
  78. data/motion/core_ext/numeric/conversions.rb +49 -0
  79. data/motion/core_ext/numeric/time.rb +75 -0
  80. data/motion/core_ext/object/acts_like.rb +10 -0
  81. data/motion/core_ext/object/blank.rb +105 -0
  82. data/motion/core_ext/object/deep_dup.rb +44 -0
  83. data/motion/core_ext/object/duplicable.rb +87 -0
  84. data/motion/core_ext/object/inclusion.rb +15 -0
  85. data/motion/core_ext/object/instance_variables.rb +28 -0
  86. data/motion/core_ext/object/to_param.rb +58 -0
  87. data/motion/core_ext/object/to_query.rb +26 -0
  88. data/motion/core_ext/object/try.rb +78 -0
  89. data/motion/core_ext/range/include_range.rb +23 -0
  90. data/motion/core_ext/range/overlaps.rb +8 -0
  91. data/motion/core_ext/regexp.rb +5 -0
  92. data/motion/core_ext/string/access.rb +104 -0
  93. data/motion/core_ext/string/behavior.rb +6 -0
  94. data/motion/core_ext/string/exclude.rb +11 -0
  95. data/motion/core_ext/string/filters.rb +55 -0
  96. data/motion/core_ext/string/indent.rb +43 -0
  97. data/motion/core_ext/string/inflections.rb +178 -0
  98. data/motion/core_ext/string/starts_ends_with.rb +4 -0
  99. data/motion/core_ext/string/strip.rb +24 -0
  100. data/motion/core_ext/time/acts_like.rb +8 -0
  101. data/motion/core_ext/time/calculations.rb +215 -0
  102. data/motion/core_ext/time/conversions.rb +52 -0
  103. data/motion/descendants_tracker.rb +50 -0
  104. data/motion/duration.rb +104 -0
  105. data/motion/hash_with_indifferent_access.rb +253 -0
  106. data/motion/inflections.rb +67 -0
  107. data/motion/inflector/inflections.rb +203 -0
  108. data/motion/inflector/methods.rb +321 -0
  109. data/motion/logger.rb +47 -0
  110. data/motion/number_helper.rb +54 -0
  111. data/motion/version.rb +3 -0
  112. data/motion_blender-support.gemspec +21 -0
  113. data/spec/motion-support/_helpers/constantize_test_cases.rb +75 -0
  114. data/spec/motion-support/_helpers/inflector_test_cases.rb +270 -0
  115. data/spec/motion-support/callback_spec.rb +702 -0
  116. data/spec/motion-support/concern_spec.rb +93 -0
  117. data/spec/motion-support/core_ext/array/access_spec.rb +29 -0
  118. data/spec/motion-support/core_ext/array/conversion_spec.rb +60 -0
  119. data/spec/motion-support/core_ext/array/extract_options_spec.rb +15 -0
  120. data/spec/motion-support/core_ext/array/grouping_spec.rb +85 -0
  121. data/spec/motion-support/core_ext/array/prepend_and_append_spec.rb +25 -0
  122. data/spec/motion-support/core_ext/array/wrap_spec.rb +19 -0
  123. data/spec/motion-support/core_ext/array_spec.rb +16 -0
  124. data/spec/motion-support/core_ext/class/attribute_accessor_spec.rb +127 -0
  125. data/spec/motion-support/core_ext/class/attribute_spec.rb +92 -0
  126. data/spec/motion-support/core_ext/date/acts_like_spec.rb +11 -0
  127. data/spec/motion-support/core_ext/date/calculation_spec.rb +186 -0
  128. data/spec/motion-support/core_ext/date/conversion_spec.rb +18 -0
  129. data/spec/motion-support/core_ext/date_and_time/calculation_spec.rb +336 -0
  130. data/spec/motion-support/core_ext/enumerable_spec.rb +130 -0
  131. data/spec/motion-support/core_ext/hash/deep_delete_if_spec.rb +19 -0
  132. data/spec/motion-support/core_ext/hash/deep_merge_spec.rb +32 -0
  133. data/spec/motion-support/core_ext/hash/except_spec.rb +43 -0
  134. data/spec/motion-support/core_ext/hash/key_spec.rb +236 -0
  135. data/spec/motion-support/core_ext/hash/reverse_merge_spec.rb +26 -0
  136. data/spec/motion-support/core_ext/hash/slice_spec.rb +61 -0
  137. data/spec/motion-support/core_ext/integer/inflection_spec.rb +23 -0
  138. data/spec/motion-support/core_ext/integer/multiple_spec.rb +19 -0
  139. data/spec/motion-support/core_ext/kernel/singleton_class_spec.rb +9 -0
  140. data/spec/motion-support/core_ext/metaclass_spec.rb +9 -0
  141. data/spec/motion-support/core_ext/module/aliasing_spec.rb +143 -0
  142. data/spec/motion-support/core_ext/module/anonymous_spec.rb +29 -0
  143. data/spec/motion-support/core_ext/module/attr_internal_spec.rb +104 -0
  144. data/spec/motion-support/core_ext/module/attribute_accessor_spec.rb +86 -0
  145. data/spec/motion-support/core_ext/module/delegation_spec.rb +136 -0
  146. data/spec/motion-support/core_ext/module/introspection_spec.rb +70 -0
  147. data/spec/motion-support/core_ext/module/reachable_spec.rb +61 -0
  148. data/spec/motion-support/core_ext/module/remove_method_spec.rb +25 -0
  149. data/spec/motion-support/core_ext/numeric/bytes_spec.rb +43 -0
  150. data/spec/motion-support/core_ext/numeric/conversions_spec.rb +40 -0
  151. data/spec/motion-support/core_ext/object/acts_like_spec.rb +21 -0
  152. data/spec/motion-support/core_ext/object/blank_spec.rb +54 -0
  153. data/spec/motion-support/core_ext/object/deep_dup_spec.rb +54 -0
  154. data/spec/motion-support/core_ext/object/duplicable_spec.rb +31 -0
  155. data/spec/motion-support/core_ext/object/inclusion_spec.rb +34 -0
  156. data/spec/motion-support/core_ext/object/instance_variable_spec.rb +19 -0
  157. data/spec/motion-support/core_ext/object/to_param_spec.rb +75 -0
  158. data/spec/motion-support/core_ext/object/to_query_spec.rb +37 -0
  159. data/spec/motion-support/core_ext/object/try_spec.rb +92 -0
  160. data/spec/motion-support/core_ext/range/include_range_spec.rb +31 -0
  161. data/spec/motion-support/core_ext/range/overlap_spec.rb +43 -0
  162. data/spec/motion-support/core_ext/regexp_spec.rb +7 -0
  163. data/spec/motion-support/core_ext/string/access_spec.rb +53 -0
  164. data/spec/motion-support/core_ext/string/behavior_spec.rb +7 -0
  165. data/spec/motion-support/core_ext/string/exclude_spec.rb +8 -0
  166. data/spec/motion-support/core_ext/string/filter_spec.rb +49 -0
  167. data/spec/motion-support/core_ext/string/indent_spec.rb +56 -0
  168. data/spec/motion-support/core_ext/string/inflection_spec.rb +142 -0
  169. data/spec/motion-support/core_ext/string/starts_end_with_spec.rb +14 -0
  170. data/spec/motion-support/core_ext/string/strip_spec.rb +34 -0
  171. data/spec/motion-support/core_ext/string_spec.rb +88 -0
  172. data/spec/motion-support/core_ext/time/acts_like_spec.rb +11 -0
  173. data/spec/motion-support/core_ext/time/calculation_spec.rb +201 -0
  174. data/spec/motion-support/core_ext/time/conversion_spec.rb +53 -0
  175. data/spec/motion-support/descendants_tracker_spec.rb +58 -0
  176. data/spec/motion-support/duration_spec.rb +107 -0
  177. data/spec/motion-support/hash_with_indifferent_access_spec.rb +605 -0
  178. data/spec/motion-support/inflector_spec.rb +504 -0
  179. data/spec/motion-support/ns_dictionary_spec.rb +89 -0
  180. data/spec/motion-support/ns_string_spec.rb +182 -0
  181. data/spec/motion-support/number_helper_spec.rb +55 -0
  182. metadata +352 -0
@@ -0,0 +1,142 @@
1
+ describe "String" do
2
+ describe "Inflections" do
3
+ describe "singularize" do
4
+ InflectorTestCases::SingularToPlural.each do |singular, plural|
5
+ it "should pluralize singular #{singular}" do
6
+ singular.pluralize.should == plural
7
+ end
8
+ end
9
+
10
+ it "should pluralize plural" do
11
+ "plurals".pluralize.should == "plurals"
12
+ end
13
+
14
+ it "should pluralize with number" do
15
+ "blargle".pluralize(0).should == "blargles"
16
+ "blargle".pluralize(1).should == "blargle"
17
+ "blargle".pluralize(2).should == "blargles"
18
+ end
19
+ end
20
+
21
+ describe "singularize" do
22
+ InflectorTestCases::SingularToPlural.each do |singular, plural|
23
+ it "should singularize plural #{plural}" do
24
+ plural.singularize.should == singular
25
+ end
26
+ end
27
+ end
28
+
29
+ describe "titleize" do
30
+ InflectorTestCases::MixtureToTitleCase.each do |before, titleized|
31
+ it "should titleize #{before}" do
32
+ before.titleize.should == titleized
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "camelize" do
38
+ InflectorTestCases::CamelToUnderscore.each do |camel, underscore|
39
+ it "should camelize #{underscore}" do
40
+ underscore.camelize.should == camel
41
+ end
42
+ end
43
+
44
+ it "should lower-camelize" do
45
+ 'Capital'.camelize(:lower).should == 'capital'
46
+ end
47
+ end
48
+
49
+ describe "dasherize" do
50
+ InflectorTestCases::UnderscoresToDashes.each do |underscored, dasherized|
51
+ it "should dasherize #{underscored}" do
52
+ underscored.dasherize.should == dasherized
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "underscore" do
58
+ InflectorTestCases::CamelToUnderscore.each do |camel, underscore|
59
+ it "should underscore #{camel}" do
60
+ camel.underscore.should == underscore
61
+ end
62
+ end
63
+
64
+ it "should underscore acronyms" do
65
+ "HTMLTidy".underscore.should == "html_tidy"
66
+ "HTMLTidyGenerator".underscore.should == "html_tidy_generator"
67
+ end
68
+
69
+ InflectorTestCases::UnderscoreToLowerCamel.each do |underscored, lower_camel|
70
+ it "should lower-camelize #{underscored}" do
71
+ underscored.camelize(:lower).should == lower_camel
72
+ end
73
+ end
74
+ end
75
+
76
+ it "should demodulize" do
77
+ "MyApplication::Billing::Account".demodulize.should == "Account"
78
+ end
79
+
80
+ it "should deconstantize" do
81
+ "MyApplication::Billing::Account".deconstantize.should == "MyApplication::Billing"
82
+ end
83
+
84
+ describe "foreign_key" do
85
+ InflectorTestCases::ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key|
86
+ it "should build foreign key from #{klass}" do
87
+ klass.foreign_key.should == foreign_key
88
+ end
89
+ end
90
+
91
+ InflectorTestCases::ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key|
92
+ it "should build foreign key from #{klass} without underscore" do
93
+ klass.foreign_key(false).should == foreign_key
94
+ end
95
+ end
96
+ end
97
+
98
+ describe "tableize" do
99
+ InflectorTestCases::ClassNameToTableName.each do |class_name, table_name|
100
+ it "should tableize #{class_name}" do
101
+ class_name.tableize.should == table_name
102
+ end
103
+ end
104
+ end
105
+
106
+ describe "classify" do
107
+ InflectorTestCases::ClassNameToTableName.each do |class_name, table_name|
108
+ it "should classify #{table_name}" do
109
+ table_name.classify.should == class_name
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "humanize" do
115
+ InflectorTestCases::UnderscoreToHuman.each do |underscore, human|
116
+ it "should humanize #{underscore}" do
117
+ underscore.humanize.should == human
118
+ end
119
+ end
120
+ end
121
+
122
+ describe "constantize" do
123
+ extend ConstantizeTestCases
124
+
125
+ it "should constantize" do
126
+ run_constantize_tests_on do |string|
127
+ string.constantize
128
+ end
129
+ end
130
+ end
131
+
132
+ describe "safe_constantize" do
133
+ extend ConstantizeTestCases
134
+
135
+ it "should safe_constantize" do
136
+ run_safe_constantize_tests_on do |string|
137
+ string.safe_constantize
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,14 @@
1
+ describe "String" do
2
+ describe "starts_with?/ends_with?" do
3
+ it "should have starts/ends_with? alias" do
4
+ s = "hello"
5
+ s.starts_with?('h').should.be.true
6
+ s.starts_with?('hel').should.be.true
7
+ s.starts_with?('el').should.be.false
8
+
9
+ s.ends_with?('o').should.be.true
10
+ s.ends_with?('lo').should.be.true
11
+ s.ends_with?('el').should.be.false
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ describe "String" do
2
+ describe "strip_heredoc" do
3
+ it "should strip heredoc on an empty string" do
4
+ ''.strip_heredoc.should == ''
5
+ end
6
+
7
+ it "should strip heredoc on a string with no lines" do
8
+ 'x'.strip_heredoc.should == 'x'
9
+ ' x'.strip_heredoc.should == 'x'
10
+ end
11
+
12
+ it "should strip heredoc on a heredoc with no margin" do
13
+ "foo\nbar".strip_heredoc.should == "foo\nbar"
14
+ "foo\n bar".strip_heredoc.should == "foo\n bar"
15
+ end
16
+
17
+ it "should strip heredoc on a regular indented heredoc" do
18
+ <<-EOS.strip_heredoc.should == "foo\n bar\nbaz\n"
19
+ foo
20
+ bar
21
+ baz
22
+ EOS
23
+ end
24
+
25
+ it "should strip heredoc on a regular indented heredoc with blank lines" do
26
+ <<-EOS.strip_heredoc.should == "foo\n bar\n\nbaz\n"
27
+ foo
28
+ bar
29
+
30
+ baz
31
+ EOS
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,88 @@
1
+ describe "string" do
2
+ describe "constantize" do
3
+ module Ace
4
+ module Base
5
+ class Case
6
+ class Dice
7
+ end
8
+ end
9
+ class Fase < Case
10
+ end
11
+ end
12
+ class Gas
13
+ include Base
14
+ end
15
+ end
16
+
17
+ class Object
18
+ module AddtlGlobalConstants
19
+ class Case
20
+ class Dice
21
+ end
22
+ end
23
+ end
24
+ include AddtlGlobalConstants
25
+ end
26
+
27
+ it "should lookup nested constant" do
28
+ "Ace::Base::Case".constantize.should == Ace::Base::Case
29
+ end
30
+
31
+ it "should lookup nested absolute constant" do
32
+ "::Ace::Base::Case".constantize.should == Ace::Base::Case
33
+ end
34
+
35
+ it "should lookup nested inherited constant" do
36
+ "Ace::Base::Fase::Dice".constantize.should == Ace::Base::Fase::Dice
37
+ end
38
+
39
+ it "should lookup nested included constant" do
40
+ "Ace::Gas::Case".constantize.should == Ace::Gas::Case
41
+ end
42
+
43
+ it "should lookup nested constant included into Object" do
44
+ "Case::Dice".constantize.should == Case::Dice
45
+ end
46
+
47
+ it "should lookup nested absolute constant included into Object" do
48
+ "Object::Case::Dice".constantize.should == Case::Dice
49
+ end
50
+
51
+ it "should lookup constant" do
52
+ "String".constantize.should == String
53
+ end
54
+
55
+ it "should lookup absolute constant" do
56
+ "::Ace".constantize.should == Ace
57
+ end
58
+
59
+ it "should return Object for empty string" do
60
+ "".constantize.should == Object
61
+ end
62
+
63
+ it "should return Object for double colon" do
64
+ "::".constantize.should == Object
65
+ end
66
+
67
+ it "should raise NameError for unknown constant" do
68
+ lambda { "UnknownClass".constantize }.should.raise NameError
69
+ end
70
+
71
+ it "should raise NameError for unknown nested constant" do
72
+ lambda { "UnknownClass::Ace".constantize }.should.raise NameError
73
+ lambda { "UnknownClass::Ace::Base".constantize }.should.raise NameError
74
+ end
75
+
76
+ it "should raise NameError for invalid string" do
77
+ lambda { "An invalid string".constantize }.should.raise NameError
78
+ lambda { "InvalidClass\n".constantize }.should.raise NameError
79
+ end
80
+
81
+ it "should raise NameError for nested unknown constant in known constant" do
82
+ lambda { "Ace::ConstantizeTestCases".constantize }.should.raise NameError
83
+ lambda { "Ace::Base::ConstantizeTestCases".constantize }.should.raise NameError
84
+ lambda { "Ace::Gas::Base".constantize }.should.raise NameError
85
+ lambda { "Ace::Gas::ConstantizeTestCases".constantize }.should.raise NameError
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,11 @@
1
+ describe "Time" do
2
+ describe "acts_like" do
3
+ it "should act like time" do
4
+ Time.now.should.acts_like(:time)
5
+ end
6
+
7
+ it "should not act like date" do
8
+ Time.now.should.not.acts_like(:date)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,201 @@
1
+ describe "Time" do
2
+ describe "calculations" do
3
+ describe "seconds_since_midnight" do
4
+ it "should calculate correctly" do
5
+ Time.local(2005,1,1,0,0,1).seconds_since_midnight.should == 1
6
+ Time.local(2005,1,1,0,1,0).seconds_since_midnight.should == 60
7
+ Time.local(2005,1,1,1,1,0).seconds_since_midnight.should == 3660
8
+ Time.local(2005,1,1,23,59,59).seconds_since_midnight.should == 86399
9
+ Time.local(2005,1,1,0,1,0,10).seconds_since_midnight.should == 60.00001
10
+ end
11
+ end
12
+
13
+ describe "seconds_until_end_of_day" do
14
+ it "should calculate correctly" do
15
+ Time.local(2005,1,1,23,59,59).seconds_until_end_of_day.should == 0
16
+ Time.local(2005,1,1,23,59,58).seconds_until_end_of_day.should == 1
17
+ Time.local(2005,1,1,23,58,59).seconds_until_end_of_day.should == 60
18
+ Time.local(2005,1,1,22,58,59).seconds_until_end_of_day.should == 3660
19
+ Time.local(2005,1,1,0,0,0).seconds_until_end_of_day.should == 86399
20
+ end
21
+ end
22
+
23
+ describe "beginning_of_day" do
24
+ it "should calculate correctly" do
25
+ Time.local(2005,2,4,10,10,10).beginning_of_day.should == Time.local(2005,2,4,0,0,0)
26
+ end
27
+ end
28
+
29
+ describe "beginning_of_hour" do
30
+ it "should calculate correctly" do
31
+ Time.local(2005,2,4,19,30,10).beginning_of_hour.should == Time.local(2005,2,4,19,0,0)
32
+ end
33
+ end
34
+
35
+ describe "beginning_of_minute" do
36
+ it "should calculate correctly" do
37
+ Time.local(2005,2,4,19,30,10).beginning_of_minute.should == Time.local(2005,2,4,19,30,0)
38
+ end
39
+ end
40
+
41
+ describe "end_of_day" do
42
+ it "should calculate correctly" do
43
+ Time.local(2007,8,12,10,10,10).end_of_day.should == Time.local(2007,8,12,23,59,59,Rational(999999999, 1000))
44
+ end
45
+ end
46
+
47
+ describe "end_of_hour" do
48
+ it "should calculate correctly" do
49
+ Time.local(2005,2,4,19,30,10).end_of_hour.should == Time.local(2005,2,4,19,59,59,Rational(999999999, 1000))
50
+ end
51
+ end
52
+
53
+ describe "end_of_minute" do
54
+ it "should calculate correctly" do
55
+ Time.local(2005,2,4,19,30,10).end_of_minute.should == Time.local(2005,2,4,19,30,59,Rational(999999999, 1000))
56
+ end
57
+ end
58
+
59
+ describe "last_year" do
60
+ it "should calculate correctly" do
61
+ Time.local(2005,6,5,10,0,0).last_year.should == Time.local(2004,6,5,10)
62
+ end
63
+ end
64
+
65
+ describe "ago" do
66
+ it "should calculate correctly" do
67
+ Time.local(2005,2,22,10,10,10).ago(1).should == Time.local(2005,2,22,10,10,9)
68
+ Time.local(2005,2,22,10,10,10).ago(3600).should == Time.local(2005,2,22,9,10,10)
69
+ Time.local(2005,2,22,10,10,10).ago(86400*2).should == Time.local(2005,2,20,10,10,10)
70
+ Time.local(2005,2,22,10,10,10).ago(86400*2 + 3600 + 25).should == Time.local(2005,2,20,9,9,45)
71
+ end
72
+ end
73
+
74
+ describe "since" do
75
+ it "should calculate correctly" do
76
+ Time.local(2005,2,22,10,10,10).since(1).should == Time.local(2005,2,22,10,10,11)
77
+ Time.local(2005,2,22,10,10,10).since(3600).should == Time.local(2005,2,22,11,10,10)
78
+ Time.local(2005,2,22,10,10,10).since(86400*2).should == Time.local(2005,2,24,10,10,10)
79
+ Time.local(2005,2,22,10,10,10).since(86400*2 + 3600 + 25).should == Time.local(2005,2,24,11,10,35)
80
+ end
81
+ end
82
+
83
+ describe "change" do
84
+ it "should calculate correctly" do
85
+ Time.local(2005,2,22,15,15,10).change(:year => 2006).should == Time.local(2006,2,22,15,15,10)
86
+ Time.local(2005,2,22,15,15,10).change(:month => 6).should == Time.local(2005,6,22,15,15,10)
87
+ Time.local(2005,2,22,15,15,10).change(:year => 2012, :month => 9).should == Time.local(2012,9,22,15,15,10)
88
+ Time.local(2005,2,22,15,15,10).change(:hour => 16).should == Time.local(2005,2,22,16)
89
+ Time.local(2005,2,22,15,15,10).change(:hour => 16, :min => 45).should == Time.local(2005,2,22,16,45)
90
+ Time.local(2005,2,22,15,15,10).change(:min => 45).should == Time.local(2005,2,22,15,45)
91
+
92
+ Time.local(2005,1,2,11,22,33,44).change(:hour => 5).should == Time.local(2005,1,2,5,0,0,0)
93
+ Time.local(2005,1,2,11,22,33,44).change(:min => 6).should == Time.local(2005,1,2,11,6,0,0)
94
+ Time.local(2005,1,2,11,22,33,44).change(:sec => 7).should == Time.local(2005,1,2,11,22,7,0)
95
+ Time.local(2005,1,2,11,22,33,44).change(:usec => 8).should == Time.local(2005,1,2,11,22,33,8)
96
+ end
97
+ end
98
+
99
+ describe "advance" do
100
+ it "should calculate correctly" do
101
+ Time.local(2005,2,28,15,15,10).advance(:years => 1).should == Time.local(2006,2,28,15,15,10)
102
+ Time.local(2005,2,28,15,15,10).advance(:months => 4).should == Time.local(2005,6,28,15,15,10)
103
+ Time.local(2005,2,28,15,15,10).advance(:weeks => 3).should == Time.local(2005,3,21,15,15,10)
104
+ Time.local(2005,2,28,15,15,10).advance(:weeks => 3.5).should == Time.local(2005,3,25,3,15,10)
105
+ Time.local(2005,2,28,15,15,10).advance(:days => 5).should == Time.local(2005,3,5,15,15,10)
106
+ Time.local(2005,2,28,15,15,10).advance(:days => 5.5).should == Time.local(2005,3,6,3,15,10)
107
+ Time.local(2005,2,28,15,15,10).advance(:years => 7, :months => 7).should == Time.local(2012,9,28,15,15,10)
108
+ Time.local(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :days => 5).should == Time.local(2013,10,3,15,15,10)
109
+ Time.local(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :weeks => 2, :days => 5).should == Time.local(2013,10,17,15,15,10)
110
+ Time.local(2005,2,28,15,15,10).advance(:years => -3, :months => -2, :days => -1).should == Time.local(2001,12,27,15,15,10)
111
+ Time.local(2004,2,29,15,15,10).advance(:years => 1).should == Time.local(2005,2,28,15,15,10)
112
+ Time.local(2005,2,28,15,15,10).advance(:hours => 5).should == Time.local(2005,2,28,20,15,10)
113
+ Time.local(2005,2,28,15,15,10).advance(:minutes => 7).should == Time.local(2005,2,28,15,22,10)
114
+ Time.local(2005,2,28,15,15,10).advance(:seconds => 9).should == Time.local(2005,2,28,15,15,19)
115
+ Time.local(2005,2,28,15,15,10).advance(:hours => 5, :minutes => 7, :seconds => 9).should == Time.local(2005,2,28,20,22,19)
116
+ Time.local(2005,2,28,15,15,10).advance(:hours => -5, :minutes => -7, :seconds => -9).should == Time.local(2005,2,28,10,8,1)
117
+ Time.local(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :weeks => 2, :days => 5, :hours => 5, :minutes => 7, :seconds => 9).should == Time.local(2013,10,17,20,22,19)
118
+ end
119
+
120
+ it "should advance with nsec" do
121
+ t = Time.at(0, Rational(108635108, 1000))
122
+ t.advance(:months => 0).should == t
123
+ end
124
+ end
125
+
126
+ describe "days_in_month" do
127
+ it "should calculate with year" do
128
+ Time.days_in_month(1, 2005).should == 31
129
+
130
+ Time.days_in_month(2, 2005).should == 28
131
+ Time.days_in_month(2, 2004).should == 29
132
+ Time.days_in_month(2, 2000).should == 29
133
+ Time.days_in_month(2, 1900).should == 28
134
+
135
+ Time.days_in_month(3, 2005).should == 31
136
+ Time.days_in_month(4, 2005).should == 30
137
+ Time.days_in_month(5, 2005).should == 31
138
+ Time.days_in_month(6, 2005).should == 30
139
+ Time.days_in_month(7, 2005).should == 31
140
+ Time.days_in_month(8, 2005).should == 31
141
+ Time.days_in_month(9, 2005).should == 30
142
+ Time.days_in_month(10, 2005).should == 31
143
+ Time.days_in_month(11, 2005).should == 30
144
+ Time.days_in_month(12, 2005).should == 31
145
+ end
146
+
147
+ it "should calculate for february in common year" do
148
+ Time.days_in_month(2, 2007).should == 28
149
+ end
150
+
151
+ it "should calculate for february in leap year" do
152
+ Time.days_in_month(2, 2008).should == 29
153
+ end
154
+ end
155
+
156
+ describe "last_month" do
157
+ it "should work on the 31st" do
158
+ Time.local(2004, 3, 31).last_month.should == Time.local(2004, 2, 29)
159
+ end
160
+ end
161
+
162
+ describe "<=>" do
163
+ it "should compare with time" do
164
+ (Time.utc(2000) <=> Time.utc(1999, 12, 31, 23, 59, 59, 999)).should == 1
165
+ (Time.utc(2000) <=> Time.utc(2000, 1, 1, 0, 0, 0)).should == 0
166
+ (Time.utc(2000) <=> Time.utc(2000, 1, 1, 0, 0, 0, 001)).should == -1
167
+ end
168
+ end
169
+
170
+ describe "all_day" do
171
+ it "should calculate correctly" do
172
+ Time.local(2011,6,7,10,10,10).all_day.should == (Time.local(2011,6,7,0,0,0)..Time.local(2011,6,7,23,59,59,Rational(999999999, 1000)))
173
+ end
174
+ end
175
+
176
+ describe "all_week" do
177
+ it "should calculate correctly" do
178
+ Time.local(2011,6,7,10,10,10).all_week.should == (Time.local(2011,6,6,0,0,0)..Time.local(2011,6,12,23,59,59,Rational(999999999, 1000)))
179
+ Time.local(2011,6,7,10,10,10).all_week(:sunday).should == (Time.local(2011,6,5,0,0,0)..Time.local(2011,6,11,23,59,59,Rational(999999999, 1000)))
180
+ end
181
+ end
182
+
183
+ describe "all_month" do
184
+ it "should calculate correctly" do
185
+ Time.local(2011,6,7,10,10,10).all_month.should == (Time.local(2011,6,1,0,0,0)..Time.local(2011,6,30,23,59,59,Rational(999999999, 1000)))
186
+ end
187
+ end
188
+
189
+ describe "all_quarter" do
190
+ it "should calculate correctly" do
191
+ Time.local(2011,6,7,10,10,10).all_quarter.should == (Time.local(2011,4,1,0,0,0)..Time.local(2011,6,30,23,59,59,Rational(999999999, 1000)))
192
+ end
193
+ end
194
+
195
+ describe "all_year" do
196
+ it "should calculate correctly" do
197
+ Time.local(2011,6,7,10,10,10).all_year.should == (Time.local(2011,1,1,0,0,0)..Time.local(2011,12,31,23,59,59,Rational(999999999, 1000)))
198
+ end
199
+ end
200
+ end
201
+ end
@@ -0,0 +1,53 @@
1
+ describe "Time" do
2
+ describe "conversions" do
3
+ describe "to_formatted_s" do
4
+ before do
5
+ @time = Time.utc(2005, 2, 21, 17, 44, 30.12345678901)
6
+ end
7
+
8
+ it "should use default conversion if no parameter is given" do
9
+ @time.to_s.should == @time.to_default_s
10
+ end
11
+
12
+ it "should use default conversion if parameter is unknown" do
13
+ @time.to_s(:doesnt_exist).should == @time.to_default_s
14
+ end
15
+
16
+ it "should convert to db format" do
17
+ @time.to_s(:db).should == "2005-02-21 17:44:30"
18
+ end
19
+
20
+ it "should convert to short format" do
21
+ @time.to_s(:short).should == "21 Feb 17:44"
22
+ end
23
+
24
+ it "should convert to time format" do
25
+ @time.to_s(:time).should == "17:44"
26
+ end
27
+
28
+ it "should convert to number format" do
29
+ @time.to_s(:number).should == "20050221174430"
30
+ end
31
+
32
+ it "should convert to nsec format" do
33
+ @time.to_s(:nsec).should == "20050221174430123456789"
34
+ end
35
+
36
+ it "should convert to long format" do
37
+ @time.to_s(:long).should == "February 21, 2005 17:44"
38
+ end
39
+
40
+ it "should convert to long_ordinal format" do
41
+ @time.to_s(:long_ordinal).should == "February 21st, 2005 17:44"
42
+ end
43
+ end
44
+
45
+ describe "custom date format" do
46
+ it "should convert to custom format" do
47
+ Time::DATE_FORMATS[:custom] = '%Y%m%d%H%M%S'
48
+ Time.local(2005, 2, 21, 14, 30, 0).to_s(:custom).should == '20050221143000'
49
+ Time::DATE_FORMATS.delete(:custom)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,58 @@
1
+ module DescendantsTrackerSpec
2
+ class Parent
3
+ extend MotionSupport::DescendantsTracker
4
+ end
5
+
6
+ class Child1 < Parent
7
+ end
8
+
9
+ class Child2 < Parent
10
+ end
11
+
12
+ class Grandchild1 < Child1
13
+ end
14
+
15
+ class Grandchild2 < Child1
16
+ end
17
+
18
+ ALL = [Parent, Child1, Child2, Grandchild1, Grandchild2]
19
+ end
20
+
21
+ describe "DescendatsTracker" do
22
+ describe "descendants" do
23
+ it "should get all descendants from parent class" do
24
+ [DescendantsTrackerSpec::Child1, DescendantsTrackerSpec::Child2, DescendantsTrackerSpec::Grandchild1, DescendantsTrackerSpec::Grandchild2].should == DescendantsTrackerSpec::Parent.descendants
25
+ end
26
+
27
+ it "should get descendants from subclass" do
28
+ [DescendantsTrackerSpec::Grandchild1, DescendantsTrackerSpec::Grandchild2].should == DescendantsTrackerSpec::Child1.descendants
29
+ end
30
+
31
+ it "should return empty array for leaf class" do
32
+ [].should == DescendantsTrackerSpec::Child2.descendants
33
+ end
34
+ end
35
+
36
+ describe "direct_descendants" do
37
+ it "should get direct descendants from parent class" do
38
+ [DescendantsTrackerSpec::Child1, DescendantsTrackerSpec::Child2].should == DescendantsTrackerSpec::Parent.direct_descendants
39
+ end
40
+
41
+ it "should get direct descendants from subclass" do
42
+ [DescendantsTrackerSpec::Grandchild1, DescendantsTrackerSpec::Grandchild2].should == DescendantsTrackerSpec::Child1.direct_descendants
43
+ end
44
+
45
+ it "should return empty array for leaf class" do
46
+ [].should == DescendantsTrackerSpec::Child2.direct_descendants
47
+ end
48
+ end
49
+
50
+ describe "clear" do
51
+ it "should remove all tracked descendants" do
52
+ MotionSupport::DescendantsTracker.clear
53
+ DescendantsTrackerSpec::ALL.each do |k|
54
+ MotionSupport::DescendantsTracker.descendants(k).should.be.empty
55
+ end
56
+ end
57
+ end
58
+ end