motion-support 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,143 @@
1
+ module Aliasing
2
+ class File
3
+ public
4
+ def open(file)
5
+ end
6
+ def open_with_internet(url)
7
+ open_without_internet(url)
8
+ end
9
+ alias_method_chain :open, :internet
10
+
11
+ protected
12
+ def exist?(file)
13
+ end
14
+ def exist_with_internet?(url)
15
+ exist_without_internet?(url)
16
+ end
17
+ alias_method_chain :exist?, :internet
18
+
19
+ private
20
+ def remove!(file)
21
+ end
22
+ def remove_with_internet!(url)
23
+ remove_without_internet!(url)
24
+ end
25
+ alias_method_chain :remove!, :internet
26
+ end
27
+
28
+ class Content
29
+ attr_accessor :title, :Data
30
+
31
+ def initialize
32
+ @title, @Data = nil, nil
33
+ end
34
+
35
+ def title?
36
+ !title.nil?
37
+ end
38
+
39
+ def Data?
40
+ !self.Data.nil?
41
+ end
42
+ end
43
+
44
+ class Email < Content
45
+ alias_attribute :subject, :title
46
+ alias_attribute :body, :Data
47
+ end
48
+ end
49
+
50
+ describe "module" do
51
+ describe "alias_method_chain" do
52
+ it "should define 'without' method" do
53
+ Aliasing::File.new.should.respond_to :open_without_internet
54
+ end
55
+
56
+ it "should respond to original method" do
57
+ Aliasing::File.new.should.respond_to :open
58
+ end
59
+
60
+ it "should respond to 'with' method" do
61
+ Aliasing::File.new.should.respond_to :open_with_internet
62
+ end
63
+
64
+ it "should preserve question punctuation" do
65
+ Aliasing::File.new.should.respond_to :exist_without_internet?
66
+ end
67
+
68
+ it "should preserve bang punctuation" do
69
+ # Ruby objects don't respond to private methods
70
+ Aliasing::File.new.should.not.respond_to :remove_without_internet!
71
+ end
72
+
73
+ it "should keep public methods public" do
74
+ Aliasing::File.new.public_methods.should.include :"open_without_internet:"
75
+ end
76
+
77
+ it "should keep protected methods protected" do
78
+ Aliasing::File.new.protected_methods.should.include :"exist_without_internet?:"
79
+ end
80
+
81
+ it "should keep private methods private" do
82
+ Aliasing::File.new.private_methods.should.include :"remove_without_internet!:"
83
+ end
84
+ end
85
+
86
+ describe "alias_attribute" do
87
+ before do
88
+ @email = Aliasing::Email.new
89
+ end
90
+
91
+ describe "getter" do
92
+ it "should exist" do
93
+ lambda { @email.subject }.should.not.raise
94
+ end
95
+
96
+ it "should read" do
97
+ @email.title = "hello"
98
+ @email.subject.should == "hello"
99
+ end
100
+
101
+ it "should read from uppercase method" do
102
+ @email.body = "world"
103
+ @email.Data.should == "world"
104
+ end
105
+ end
106
+
107
+ describe "setter" do
108
+ it "should exist" do
109
+ lambda { @email.subject = "hello" }.should.not.raise
110
+ end
111
+
112
+ it "should write" do
113
+ @email.subject = "hello"
114
+ @email.title.should == "hello"
115
+ end
116
+
117
+ it "should write to uppercase method" do
118
+ @email.Data = "world"
119
+ @email.body.should == "world"
120
+ end
121
+ end
122
+
123
+ describe "question method" do
124
+ it "should exist" do
125
+ lambda { @email.subject? }.should.not.raise
126
+ end
127
+
128
+ it "should return false for nil attribute" do
129
+ @email.subject?.should == false
130
+ end
131
+
132
+ it "should return true for non-nil attribute" do
133
+ @email.title = "hello"
134
+ @email.subject?.should == true
135
+ end
136
+
137
+ it "should return true for non-nil uppercase attribute" do
138
+ @email.body = "hello"
139
+ @email.Data?.should == true
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,29 @@
1
+ describe "module" do
2
+ describe "anonymous?" do
3
+ it "should return true for an anonymous module" do
4
+ Module.new.anonymous?.should == true
5
+ end
6
+
7
+ it "should return true for an anonymous class" do
8
+ Class.new.anonymous?.should == true
9
+ end
10
+
11
+ it "should return false for a named module" do
12
+ Kernel.anonymous?.should == false
13
+ end
14
+
15
+ it "should return false for a named class" do
16
+ Object.anonymous?.should == false
17
+ end
18
+
19
+ it "should return false for module that acquired a name" do
20
+ NamedModule = Module.new
21
+ NamedModule.anonymous?.should == false
22
+ end
23
+
24
+ it "should return false for module that acquired a name" do
25
+ NamedClass = Class.new
26
+ NamedClass.anonymous?.should == false
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,104 @@
1
+ describe "module" do
2
+ describe "attr_internal" do
3
+ before do
4
+ @target = Class.new
5
+ @instance = @target.new
6
+ end
7
+
8
+ describe "reader" do
9
+ it "should define reader" do
10
+ lambda { @target.attr_internal_reader :foo }.should.not.raise
11
+ end
12
+
13
+ describe "defined" do
14
+ before do
15
+ @target.attr_internal_reader :foo
16
+ end
17
+
18
+ it "should not have instance variable at first" do
19
+ @instance.instance_variable_defined?('@_foo').should.not.be.true
20
+ end
21
+
22
+ it "should not define writer" do
23
+ lambda { @instance.foo = 1 }.should.raise NoMethodError
24
+ end
25
+
26
+ it "should return value of internal reader" do
27
+ @instance.instance_variable_set('@_foo', 1)
28
+ lambda { @instance.foo.should == 1 }.should.not.raise
29
+ end
30
+ end
31
+ end
32
+
33
+ describe "writer" do
34
+ it "should define writer" do
35
+ lambda { @target.attr_internal_writer :foo }.should.not.raise
36
+ end
37
+
38
+ describe "defined" do
39
+ before do
40
+ @target.attr_internal_writer :foo
41
+ end
42
+
43
+ it "should not have instance variable at first" do
44
+ @instance.instance_variable_defined?('@_foo').should.not.be.true
45
+ end
46
+
47
+ it "should set instance variable" do
48
+ lambda { @instance.foo = 1 }.should.not.raise
49
+ @instance.instance_variable_defined?('@_foo').should.be.true
50
+ @instance.instance_variable_get('@_foo').should == 1
51
+ end
52
+
53
+ it "should not define reader" do
54
+ lambda { @instance.foo }.should.raise NoMethodError
55
+ end
56
+ end
57
+ end
58
+
59
+ describe "accessor" do
60
+ it "should define accessor" do
61
+ lambda { @target.attr_internal :foo }.should.not.raise
62
+ end
63
+
64
+ describe "defined" do
65
+ before do
66
+ @target.attr_internal :foo
67
+ end
68
+
69
+ it "should not have instance variable at first" do
70
+ @instance.instance_variable_defined?('@_foo').should.not.be.true
71
+ end
72
+
73
+ it "should set instance variable" do
74
+ lambda { @instance.foo = 1 }.should.not.raise
75
+ @instance.instance_variable_defined?('@_foo').should.be.true
76
+ @instance.instance_variable_get('@_foo').should == 1
77
+ end
78
+
79
+ it "should read from instance variable" do
80
+ @instance.instance_variable_set('@_foo', 1)
81
+ lambda { @instance.foo }.should.not.raise
82
+ end
83
+ end
84
+ end
85
+
86
+ describe "naming format" do
87
+ it "should allow custom naming format for instance variable" do
88
+ begin
89
+ Module.attr_internal_naming_format.should == '@_%s'
90
+ lambda { Module.attr_internal_naming_format = '@abc%sdef' }.should.not.raise
91
+ @target.attr_internal :foo
92
+
93
+ @instance.instance_variable_defined?('@_foo').should.be.false
94
+ @instance.instance_variable_defined?('@abcfoodef').should.be.false
95
+ lambda { @instance.foo = 1 }.should.not.raise
96
+ @instance.instance_variable_defined?('@_foo').should.not.be.true
97
+ @instance.instance_variable_defined?('@abcfoodef').should.be.true
98
+ ensure
99
+ Module.attr_internal_naming_format = '@_%s'
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,86 @@
1
+ describe "module" do
2
+ describe "attribute accessors" do
3
+ before do
4
+ m = @module = Module.new
5
+ @module.instance_eval do
6
+ mattr_accessor :foo
7
+ mattr_accessor :bar, :instance_writer => false
8
+ mattr_reader :shaq, :instance_reader => false
9
+ mattr_accessor :camp, :instance_accessor => false
10
+ end
11
+ @class = Class.new
12
+ @class.instance_eval { include m }
13
+ @object = @class.new
14
+ end
15
+
16
+ describe "reader" do
17
+ it "should return nil by default" do
18
+ @module.foo.should.be.nil
19
+ end
20
+ end
21
+
22
+ describe "writer" do
23
+ it "should set value" do
24
+ @module.foo = :test
25
+ @module.foo.should == :test
26
+ end
27
+
28
+ it "should set value through instance writer" do
29
+ @object.foo = :bar
30
+ @object.foo.should == :bar
31
+ end
32
+
33
+ it "should set instance reader's value through module's writer" do
34
+ @module.foo = :test
35
+ @object.foo.should == :test
36
+ end
37
+
38
+ it "should set module reader's value through instances's writer" do
39
+ @object.foo = :bar
40
+ @module.foo.should == :bar
41
+ end
42
+ end
43
+
44
+ describe "instance_writer => false" do
45
+ it "should not create instance writer" do
46
+ @module.should.respond_to :foo
47
+ @module.should.respond_to :foo=
48
+ @object.should.respond_to :bar
49
+ @object.should.not.respond_to :bar=
50
+ end
51
+ end
52
+
53
+ describe "instance_reader => false" do
54
+ it "should not create instance reader" do
55
+ @module.should.respond_to :shaq
56
+ @object.should.not.respond_to :shaq
57
+ end
58
+ end
59
+
60
+ describe "instance_accessor => false" do
61
+ it "should not create reader or writer" do
62
+ @module.should.respond_to :camp
63
+ @object.should.not.respond_to :camp
64
+ @object.should.not.respond_to :camp=
65
+ end
66
+ end
67
+ end
68
+
69
+ describe "invalid attribute accessors" do
70
+ it "should raise NameError when creating an invalid reader" do
71
+ lambda do
72
+ Class.new do
73
+ mattr_reader "invalid attribute name"
74
+ end
75
+ end.should.raise NameError
76
+ end
77
+
78
+ it "should raise NameError when creating an invalid writer" do
79
+ lambda do
80
+ Class.new do
81
+ mattr_writer "invalid attribute name"
82
+ end
83
+ end.should.raise NameError
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,136 @@
1
+ module ModuleDelegation
2
+ class Delegator
3
+ def receive
4
+ "result"
5
+ end
6
+ end
7
+
8
+ class InstanceVariable
9
+ def initialize
10
+ @instance_variable = Delegator.new
11
+ end
12
+ delegate :receive, :to => '@instance_variable'
13
+ end
14
+
15
+ class ClassVariable
16
+ def initialize
17
+ @@class_variable = Delegator.new
18
+ end
19
+ delegate :receive, :to => '@@class_variable'
20
+ end
21
+
22
+ class RelativeConstant
23
+ Receiver = Delegator.new
24
+ delegate :receive, :to => 'Receiver'
25
+ end
26
+
27
+ class AbsoluteConstant
28
+ Receiver = Delegator.new
29
+ delegate :receive, :to => 'ModuleDelegation::AbsoluteConstant::Receiver'
30
+ end
31
+
32
+ class Method
33
+ def method
34
+ Delegator.new
35
+ end
36
+ delegate :receive, :to => :method
37
+ end
38
+
39
+ class MethodChain
40
+ def first
41
+ self
42
+ end
43
+
44
+ def second
45
+ Delegator.new
46
+ end
47
+ delegate :receive, :to => 'first.second'
48
+ end
49
+
50
+ class ConstantSingleton
51
+ class Nested
52
+ def self.method
53
+ Delegator.new
54
+ end
55
+ end
56
+ delegate :receive, :to => 'Nested.method'
57
+ end
58
+
59
+ class AllowNil
60
+ delegate :receive, :to => '@undefined', :allow_nil => true
61
+ end
62
+
63
+ class DisallowNil
64
+ delegate :receive, :to => '@undefined'
65
+ end
66
+
67
+ class NonexistentMethod
68
+ delegate :receive, :to => :nonexistent_method
69
+ end
70
+
71
+ class Prefix
72
+ def method
73
+ Delegator.new
74
+ end
75
+ delegate :receive, :to => :method, :prefix => 'foo'
76
+ end
77
+
78
+ class AutoPrefix
79
+ def method
80
+ Delegator.new
81
+ end
82
+ delegate :receive, :to => :method, :prefix => true
83
+ end
84
+ end
85
+
86
+ describe "module" do
87
+ describe "delegation" do
88
+ it "should delegate to instance variable" do
89
+ ModuleDelegation::InstanceVariable.new.receive.should == "result"
90
+ end
91
+
92
+ it "should delegate to class variable" do
93
+ ModuleDelegation::ClassVariable.new.receive.should == "result"
94
+ end
95
+
96
+ it "should delegate to relative constant" do
97
+ ModuleDelegation::RelativeConstant.new.receive.should == "result"
98
+ end
99
+
100
+ it "should delegate to absolute constant" do
101
+ ModuleDelegation::AbsoluteConstant.new.receive.should == "result"
102
+ end
103
+
104
+ it "should delegate to method" do
105
+ ModuleDelegation::Method.new.receive.should == "result"
106
+ end
107
+
108
+ it "should delegate to method chain" do
109
+ ModuleDelegation::MethodChain.new.receive.should == "result"
110
+ end
111
+
112
+ it "should delegate to constant's singleton method" do
113
+ ModuleDelegation::ConstantSingleton.new.receive.should == "result"
114
+ end
115
+
116
+ it "should delegate to nil without raising an error when allowed" do
117
+ ModuleDelegation::AllowNil.new.receive.should.be.nil
118
+ end
119
+
120
+ it "should raise an error when delegating to nil and not allowed" do
121
+ lambda { ModuleDelegation::DisallowNil.new.receive }.should.raise
122
+ end
123
+
124
+ it "should raise an error when delegating to non-existent method" do
125
+ lambda { ModuleDelegation::NonexistentMethod.new.receive }.should.raise NoMethodError
126
+ end
127
+
128
+ it "should delegate with prefix" do
129
+ ModuleDelegation::Prefix.new.foo_receive.should == "result"
130
+ end
131
+
132
+ it "should delegate to method with automatic prefix" do
133
+ ModuleDelegation::AutoPrefix.new.method_receive.should == "result"
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,70 @@
1
+ module ModuleIntrospection
2
+ module Foo
3
+ module Bar
4
+ end
5
+ end
6
+ module Baz
7
+ end
8
+ end
9
+ ModuleIntrospectionAlias = ModuleIntrospection::Foo
10
+
11
+ describe "module" do
12
+ describe "introspection" do
13
+ describe "parent_name" do
14
+ it "should return nil for top-level module" do
15
+ ModuleIntrospection.parent_name.should.be.nil
16
+ end
17
+
18
+ it "should return direct parent for nested module" do
19
+ ModuleIntrospection::Foo.parent_name.should == "ModuleIntrospection"
20
+ end
21
+
22
+ it "should return nil for anonymous module" do
23
+ Module.new.parent_name.should.be.nil
24
+ end
25
+
26
+ it "should return original parent name for module alias" do
27
+ ModuleIntrospectionAlias.parent_name.should == "ModuleIntrospection"
28
+ end
29
+ end
30
+
31
+ describe "parent" do
32
+ it "should return Object for top-level module" do
33
+ ModuleIntrospection.parent.should == Object
34
+ end
35
+
36
+ it "should return direct parent for nested module" do
37
+ ModuleIntrospection::Foo.parent.should == ModuleIntrospection
38
+ end
39
+
40
+ it "should return nil for anonymous module" do
41
+ Module.new.parent.should == Object
42
+ end
43
+
44
+ it "should return original parent for module alias" do
45
+ ModuleIntrospectionAlias.parent.should == ModuleIntrospection
46
+ end
47
+ end
48
+
49
+ describe "parents" do
50
+ it "should return Object for top-level module" do
51
+ ModuleIntrospection.parents.should == [Object]
52
+ end
53
+
54
+ it "should return parents for nested module" do
55
+ ModuleIntrospection::Foo::Bar.parents.should == [ModuleIntrospection::Foo, ModuleIntrospection, Object]
56
+ end
57
+
58
+ it "should return original parents for module alias" do
59
+ ModuleIntrospectionAlias.parents.should == [ModuleIntrospection, Object]
60
+ end
61
+ end
62
+
63
+ describe "local_constants" do
64
+ it "should return all direct constants within the module as symbols" do
65
+ ModuleIntrospection.local_constants.should.include :Foo
66
+ ModuleIntrospection.local_constants.should.include :Baz
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,61 @@
1
+ describe "Module" do
2
+ describe "reachable?" do
3
+ it "should be false for an anonymous module" do
4
+ Module.new.should.not.be.reachable
5
+ end
6
+
7
+ it "should be false for an anonymous class" do
8
+ Class.new.should.not.be.reachable
9
+ end
10
+
11
+ it "should be true for a named module" do
12
+ Kernel.should.be.reachable
13
+ end
14
+
15
+ it "should be true for a named class" do
16
+ Object.should.be.reachable
17
+ end
18
+
19
+ it "should be false for a named class whose constant has gone" do
20
+ class C; end
21
+ c = C
22
+
23
+ Object.send(:remove_const, :C)
24
+
25
+ c.should.not.be.reachable
26
+ end
27
+
28
+ it "should be false for a named module whose constant has gone" do
29
+ module M; end
30
+ m = M
31
+
32
+ Object.send(:remove_const, :M)
33
+
34
+ m.should.not.be.reachable
35
+ end
36
+
37
+ it "should be false for a named class whose constant was redefined" do
38
+ class C; end
39
+ c = C
40
+
41
+ Object.send(:remove_const, :C)
42
+
43
+ class C; end
44
+
45
+ C.should.be.reachable
46
+ c.should.not.be.reachable
47
+ end
48
+
49
+ it "should be false for a named module whose constant was redefined" do
50
+ module M; end
51
+ m = M
52
+
53
+ Object.send(:remove_const, :M)
54
+
55
+ module M; end
56
+
57
+ M.should.be.reachable
58
+ m.should.not.be.reachable
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,25 @@
1
+ module RemoveMethodSpec
2
+ class A
3
+ def do_something
4
+ return 1
5
+ end
6
+ end
7
+ end
8
+
9
+ describe "module" do
10
+ describe "remove_method" do
11
+ it "should remove a method from an object" do
12
+ RemoveMethodSpec::A.class_eval {
13
+ self.remove_possible_method(:do_something)
14
+ }
15
+ RemoveMethodSpec::A.new.should.not.respond_to :do_something
16
+ end
17
+
18
+ def test_redefine_method_in_an_object
19
+ RemoveMethodSpec::A.class_eval {
20
+ self.redefine_method(:do_something) { return 100 }
21
+ }
22
+ RemoveMethodSpec::A.new.do_something.should == 100
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,43 @@
1
+ describe "Numeric" do
2
+ describe "bytes" do
3
+ it "should calculate kilobytes" do
4
+ 1.kilobyte.should == 1024.bytes
5
+ 3.kilobytes.should == 3072
6
+ 3.kilobyte.should == 3072
7
+ end
8
+
9
+ it "should calculate megabytes" do
10
+ 1.megabyte.should == 1024.kilobytes
11
+ 3.5.megabytes.should == 3584.0.kilobytes
12
+ 3.megabytes.should == 1024.kilobytes + 2.megabytes
13
+ 512.megabytes.should == 2.gigabytes / 4
14
+ 3.megabytes.should == 3145728
15
+ 3.megabyte.should == 3145728
16
+ end
17
+
18
+ it "should calculate gigabytes" do
19
+ 3.5.gigabytes.should == 3584.0.megabytes
20
+ 10.gigabytes.should == 256.megabytes * 20 + 5.gigabytes
21
+ 3.gigabytes.should == 3221225472
22
+ 3.gigabyte.should == 3221225472
23
+ end
24
+
25
+ it "should calculate terabytes" do
26
+ 1.terabyte.should == 1.kilobyte ** 4
27
+ 3.terabytes.should == 3298534883328
28
+ 3.terabyte.should == 3298534883328
29
+ end
30
+
31
+ it "should calculate petabytes" do
32
+ 1.petabyte.should == 1.kilobyte ** 5
33
+ 3.petabytes.should == 3377699720527872
34
+ 3.petabyte.should == 3377699720527872
35
+ end
36
+
37
+ it "should calculate exabytes" do
38
+ 1.exabyte.should == 1.kilobyte ** 6
39
+ 3.exabytes.should == 3458764513820540928
40
+ 3.exabyte.should == 3458764513820540928
41
+ end
42
+ end
43
+ end