gorillib 0.1.11 → 0.4.0pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. data/.gitignore +1 -0
  2. data/.rspec +1 -2
  3. data/.yardopts +9 -0
  4. data/{CHANGELOG.textile → CHANGELOG.md} +35 -9
  5. data/Gemfile +21 -14
  6. data/Guardfile +19 -0
  7. data/{LICENSE.textile → LICENSE.md} +43 -29
  8. data/README.md +47 -52
  9. data/Rakefile +31 -30
  10. data/TODO.md +32 -0
  11. data/VERSION +1 -1
  12. data/examples/builder/ironfan.rb +133 -0
  13. data/examples/model/simple.rb +17 -0
  14. data/gorillib.gemspec +106 -86
  15. data/lib/alt/kernel/call_stack.rb +56 -0
  16. data/lib/gorillib/array/wrap.rb +53 -0
  17. data/lib/gorillib/base.rb +3 -3
  18. data/lib/gorillib/builder/field.rb +5 -0
  19. data/lib/gorillib/builder.rb +260 -0
  20. data/lib/gorillib/collection/has_collection.rb +31 -0
  21. data/lib/gorillib/collection.rb +129 -0
  22. data/lib/gorillib/configurable.rb +28 -0
  23. data/lib/gorillib/datetime/{flat.rb → to_flat.rb} +0 -0
  24. data/lib/gorillib/exception/confidence.rb +17 -0
  25. data/lib/gorillib/exception/raisers.rb +78 -0
  26. data/lib/gorillib/hash/mash.rb +202 -0
  27. data/lib/gorillib/hashlike/slice.rb +53 -19
  28. data/lib/gorillib/hashlike.rb +5 -3
  29. data/lib/gorillib/io/system_helpers.rb +30 -0
  30. data/lib/gorillib/logger/log.rb +18 -0
  31. data/lib/gorillib/metaprogramming/concern.rb +124 -0
  32. data/lib/gorillib/model/active_model_conversion.rb +68 -0
  33. data/lib/gorillib/model/active_model_naming.rb +87 -0
  34. data/lib/gorillib/model/active_model_shim.rb +33 -0
  35. data/lib/gorillib/model/base.rb +341 -0
  36. data/lib/gorillib/model/defaults.rb +71 -0
  37. data/lib/gorillib/model/errors.rb +14 -0
  38. data/lib/gorillib/model/factories.rb +372 -0
  39. data/lib/gorillib/model/field.rb +146 -0
  40. data/lib/gorillib/model/named_schema.rb +53 -0
  41. data/lib/gorillib/{struct/hashlike_iteration.rb → model/overlay.rb} +0 -0
  42. data/lib/gorillib/model/record_schema.rb +9 -0
  43. data/lib/gorillib/model/serialization.rb +23 -0
  44. data/lib/gorillib/model/validate.rb +22 -0
  45. data/lib/gorillib/model.rb +23 -0
  46. data/lib/gorillib/pathname.rb +78 -0
  47. data/lib/gorillib/{serialization.rb → serialization/to_wire.rb} +0 -0
  48. data/lib/gorillib/some.rb +11 -9
  49. data/lib/gorillib/string/constantize.rb +21 -14
  50. data/lib/gorillib/string/inflections.rb +6 -76
  51. data/lib/gorillib/string/inflector.rb +192 -0
  52. data/lib/gorillib/string/simple_inflector.rb +267 -0
  53. data/lib/gorillib/type/extended.rb +52 -0
  54. data/lib/gorillib/utils/capture_output.rb +28 -0
  55. data/lib/gorillib/utils/console.rb +131 -0
  56. data/lib/gorillib/utils/nuke_constants.rb +9 -0
  57. data/lib/gorillib/utils/stub_module.rb +33 -0
  58. data/spec/examples/builder/ironfan_spec.rb +37 -0
  59. data/spec/extlib/hash_spec.rb +64 -0
  60. data/spec/extlib/mash_spec.rb +312 -0
  61. data/spec/{array → gorillib/array}/compact_blank_spec.rb +2 -2
  62. data/spec/{array → gorillib/array}/extract_options_spec.rb +2 -2
  63. data/spec/gorillib/builder_spec.rb +187 -0
  64. data/spec/gorillib/collection_spec.rb +20 -0
  65. data/spec/gorillib/configurable_spec.rb +62 -0
  66. data/spec/{datetime → gorillib/datetime}/parse_spec.rb +3 -3
  67. data/spec/{datetime/flat_spec.rb → gorillib/datetime/to_flat_spec.rb} +4 -4
  68. data/spec/{enumerable → gorillib/enumerable}/sum_spec.rb +5 -5
  69. data/spec/gorillib/exception/raisers_spec.rb +60 -0
  70. data/spec/{hash → gorillib/hash}/compact_spec.rb +2 -2
  71. data/spec/{hash → gorillib/hash}/deep_compact_spec.rb +3 -3
  72. data/spec/{hash → gorillib/hash}/deep_merge_spec.rb +2 -2
  73. data/spec/{hash → gorillib/hash}/keys_spec.rb +2 -2
  74. data/spec/{hash → gorillib/hash}/reverse_merge_spec.rb +2 -2
  75. data/spec/{hash → gorillib/hash}/slice_spec.rb +2 -2
  76. data/spec/{hash → gorillib/hash}/zip_spec.rb +2 -2
  77. data/spec/{hashlike → gorillib/hashlike}/behave_same_as_hash_spec.rb +6 -3
  78. data/spec/{hashlike → gorillib/hashlike}/deep_hash_spec.rb +2 -2
  79. data/spec/{hashlike → gorillib/hashlike}/hashlike_behavior_spec.rb +32 -30
  80. data/spec/{hashlike → gorillib/hashlike}/hashlike_via_accessors_spec.rb +3 -3
  81. data/spec/{hashlike_spec.rb → gorillib/hashlike_spec.rb} +3 -3
  82. data/spec/{logger → gorillib/logger}/log_spec.rb +2 -2
  83. data/spec/{metaprogramming → gorillib/metaprogramming}/aliasing_spec.rb +3 -3
  84. data/spec/{metaprogramming → gorillib/metaprogramming}/class_attribute_spec.rb +3 -3
  85. data/spec/{metaprogramming → gorillib/metaprogramming}/delegation_spec.rb +3 -3
  86. data/spec/{metaprogramming → gorillib/metaprogramming}/singleton_class_spec.rb +3 -3
  87. data/spec/gorillib/model/record/defaults_spec.rb +108 -0
  88. data/spec/gorillib/model/record/factories_spec.rb +321 -0
  89. data/spec/gorillib/model/record/overlay_spec.rb +46 -0
  90. data/spec/gorillib/model/serialization_spec.rb +48 -0
  91. data/spec/gorillib/model_spec.rb +281 -0
  92. data/spec/{numeric → gorillib/numeric}/clamp_spec.rb +2 -2
  93. data/spec/{object → gorillib/object}/blank_spec.rb +2 -2
  94. data/spec/{object → gorillib/object}/try_dup_spec.rb +2 -2
  95. data/spec/{object → gorillib/object}/try_spec.rb +3 -2
  96. data/spec/gorillib/pathname_spec.rb +114 -0
  97. data/spec/{string → gorillib/string}/constantize_spec.rb +2 -2
  98. data/spec/{string → gorillib/string}/human_spec.rb +2 -2
  99. data/spec/{string → gorillib/string}/inflections_spec.rb +4 -3
  100. data/spec/{string → gorillib/string}/inflector_test_cases.rb +0 -0
  101. data/spec/{string → gorillib/string}/truncate_spec.rb +4 -10
  102. data/spec/gorillib/type/extended_spec.rb +120 -0
  103. data/spec/gorillib/utils/capture_output_spec.rb +71 -0
  104. data/spec/spec_helper.rb +8 -11
  105. data/spec/support/gorillib_test_helpers.rb +66 -0
  106. data/spec/support/hashlike_fuzzing_helper.rb +31 -33
  107. data/spec/support/hashlike_helper.rb +3 -3
  108. data/spec/support/model_test_helpers.rb +81 -0
  109. data/spec/support/shared_examples/included_module.rb +20 -0
  110. metadata +177 -158
  111. data/lib/gorillib/array/average.rb +0 -13
  112. data/lib/gorillib/array/sorted_median.rb +0 -11
  113. data/lib/gorillib/array/sorted_percentile.rb +0 -11
  114. data/lib/gorillib/array/sorted_sample.rb +0 -12
  115. data/lib/gorillib/dsl_object.rb +0 -64
  116. data/lib/gorillib/hash/indifferent_access.rb +0 -207
  117. data/lib/gorillib/hash/tree_merge.rb +0 -4
  118. data/lib/gorillib/hashlike/tree_merge.rb +0 -49
  119. data/lib/gorillib/metaprogramming/cattr_accessor.rb +0 -79
  120. data/lib/gorillib/metaprogramming/mattr_accessor.rb +0 -61
  121. data/lib/gorillib/receiver/active_model_shim.rb +0 -32
  122. data/lib/gorillib/receiver/acts_as_hash.rb +0 -195
  123. data/lib/gorillib/receiver/acts_as_loadable.rb +0 -42
  124. data/lib/gorillib/receiver/locale/en.yml +0 -27
  125. data/lib/gorillib/receiver/tree_diff.rb +0 -74
  126. data/lib/gorillib/receiver/validations.rb +0 -30
  127. data/lib/gorillib/receiver.rb +0 -402
  128. data/lib/gorillib/receiver_model.rb +0 -21
  129. data/lib/gorillib/struct/acts_as_hash.rb +0 -108
  130. data/notes/fancy_hashes_and_receivers.textile +0 -120
  131. data/notes/hash_rdocs.textile +0 -97
  132. data/spec/array/average_spec.rb +0 -24
  133. data/spec/array/sorted_median_spec.rb +0 -18
  134. data/spec/array/sorted_percentile_spec.rb +0 -24
  135. data/spec/array/sorted_sample_spec.rb +0 -28
  136. data/spec/dsl_object_spec.rb +0 -99
  137. data/spec/hash/indifferent_access_spec.rb +0 -391
  138. data/spec/metaprogramming/cattr_accessor_spec.rb +0 -43
  139. data/spec/metaprogramming/mattr_accessor_spec.rb +0 -45
  140. data/spec/receiver/acts_as_hash_spec.rb +0 -295
  141. data/spec/receiver_spec.rb +0 -551
  142. data/spec/struct/acts_as_hash_fuzz_spec.rb +0 -71
  143. data/spec/struct/acts_as_hash_spec.rb +0 -422
@@ -1,391 +0,0 @@
1
- require 'gorillib/hash/indifferent_access'
2
- require 'gorillib/hash/slice'
3
- require 'gorillib/hash/reverse_merge'
4
- require 'gorillib/hash/deep_merge'
5
- require 'gorillib/hash/deep_dup'
6
-
7
- describe Gorillib::HashWithIndifferentAccess do
8
- class IndifferentHash < Gorillib::HashWithIndifferentAccess
9
- end
10
-
11
- class SubclassingArray < Array
12
- end
13
-
14
- class SubclassingHash < Hash
15
- end
16
-
17
- class NonIndifferentHash < Hash
18
- def nested_under_indifferent_access
19
- self
20
- end
21
- end
22
-
23
- before do
24
- @strings = { 'a' => 1, 'b' => 2 }
25
- @symbols = { :a => 1, :b => 2 }
26
- @mixed = { :a => 1, 'b' => 2 }
27
- @fixnums = { 0 => 1, 1 => 2 }
28
- if RUBY_VERSION < '1.9.0'
29
- @illegal_symbols = { "\0" => 1, "" => 2, [] => 3 }
30
- else
31
- @illegal_symbols = { [] => 3 }
32
- end
33
- end
34
-
35
- it 'symbolize_keys_for_hash_with_indifferent_access' do
36
- @symbols.with_indifferent_access.symbolize_keys.should be_an_instance_of(Hash)
37
- @symbols.with_indifferent_access.symbolize_keys.should == @symbols
38
- @strings.with_indifferent_access.symbolize_keys.should == @symbols
39
- @mixed.with_indifferent_access.symbolize_keys.should == @symbols
40
- end
41
-
42
- it 'symbolize_keys_bang_for_hash_with_indifferent_access' do
43
- lambda{ @symbols.with_indifferent_access.dup.symbolize_keys! }.should raise_error(NoMethodError)
44
- lambda{ @strings.with_indifferent_access.dup.symbolize_keys! }.should raise_error(NoMethodError)
45
- lambda{ @mixed.with_indifferent_access.dup.symbolize_keys! }.should raise_error(NoMethodError)
46
- end
47
-
48
- it 'symbolize_keys_preserves_keys_that_cant_be_symbolized_for_hash_with_indifferent_access' do
49
- @illegal_symbols.with_indifferent_access.symbolize_keys.should == @illegal_symbols
50
- lambda{ @illegal_symbols.with_indifferent_access.dup.symbolize_keys! }.should raise_error(NoMethodError)
51
- end
52
-
53
- it 'symbolize_keys_preserves_fixnum_keys_for_hash_with_indifferent_access' do
54
- @fixnums.with_indifferent_access.symbolize_keys.should == @fixnums
55
- lambda{ @fixnums.with_indifferent_access.dup.symbolize_keys! }.should raise_error(NoMethodError)
56
- end
57
-
58
- it 'stringify_keys_for_hash_with_indifferent_access' do
59
- @symbols.with_indifferent_access.stringify_keys.should be_an_instance_of(Gorillib::HashWithIndifferentAccess)
60
- @symbols.with_indifferent_access.stringify_keys.should == @strings
61
- @strings.with_indifferent_access.stringify_keys.should == @strings
62
- @mixed.with_indifferent_access.stringify_keys.should == @strings
63
- end
64
-
65
- it 'stringify_keys_bang_for_hash_with_indifferent_access' do
66
- @symbols.with_indifferent_access.dup.stringify_keys!.should be_an_instance_of(Gorillib::HashWithIndifferentAccess)
67
- @symbols.with_indifferent_access.dup.stringify_keys!.should == @strings
68
- @strings.with_indifferent_access.dup.stringify_keys!.should == @strings
69
- @mixed.with_indifferent_access.dup.stringify_keys!.should == @strings
70
- end
71
-
72
- it 'nested_under_indifferent_access' do
73
- foo = { "foo" => SubclassingHash.new.tap { |h| h["bar"] = "baz" } }.with_indifferent_access
74
- foo["foo"].should be_a(Gorillib::HashWithIndifferentAccess)
75
-
76
- foo = { "foo" => NonIndifferentHash.new.tap { |h| h["bar"] = "baz" } }.with_indifferent_access
77
- foo["foo"].should be_a(NonIndifferentHash)
78
- end
79
-
80
- it 'indifferent_assorted' do
81
- @strings = @strings.with_indifferent_access
82
- @symbols = @symbols.with_indifferent_access
83
- @mixed = @mixed.with_indifferent_access
84
-
85
- @strings.__send__(:convert_key, :a).should == 'a'
86
-
87
- @strings.fetch('a').should == 1
88
- @strings.fetch(:a.to_s).should == 1
89
- @strings.fetch(:a).should == 1
90
-
91
- hashes = { :@strings => @strings, :@symbols => @symbols, :@mixed => @mixed }
92
- method_map = { :'[]' => 1, :fetch => 1, :values_at => [1],
93
- :has_key? => true, :include? => true, :key? => true,
94
- :member? => true }
95
-
96
- hashes.each do |name, hash|
97
- method_map.sort_by { |m| m.to_s }.each do |meth, expected|
98
- hash.__send__(meth, 'a').should == expected
99
- hash.__send__(meth, :a).should == expected
100
- end
101
- end
102
-
103
- @strings.values_at('a', 'b').should == [1, 2]
104
- @strings.values_at(:a, :b).should == [1, 2]
105
- @symbols.values_at('a', 'b').should == [1, 2]
106
- @symbols.values_at(:a, :b).should == [1, 2]
107
- @mixed.values_at('a', 'b').should == [1, 2]
108
- @mixed.values_at(:a, :b).should == [1, 2]
109
- end
110
-
111
- it 'indifferent_reading' do
112
- hash = Gorillib::HashWithIndifferentAccess.new
113
- hash["a"] = 1
114
- hash["b"] = true
115
- hash["c"] = false
116
- hash["d"] = nil
117
-
118
- hash[:a].should == 1
119
- hash[:b].should == true
120
- hash[:c].should == false
121
- hash[:d].should == nil
122
- hash[:e].should == nil
123
- end
124
-
125
- it 'indifferent_reading_with_nonnil_default' do
126
- hash = Gorillib::HashWithIndifferentAccess.new(1)
127
- hash["a"] = 1
128
- hash["b"] = true
129
- hash["c"] = false
130
- hash["d"] = nil
131
-
132
- hash[:a].should == 1
133
- hash[:b].should == true
134
- hash[:c].should == false
135
- hash[:d].should == nil
136
- hash[:e].should == 1
137
- end
138
-
139
- it 'indifferent_writing' do
140
- hash = Gorillib::HashWithIndifferentAccess.new
141
- hash[:a] = 1
142
- hash['b'] = 2
143
- hash[3] = 3
144
-
145
- 1.should == hash['a']
146
- 2.should == hash['b']
147
- 1.should == hash[:a]
148
- 2.should == hash[:b]
149
- 3.should == hash[3]
150
- end
151
-
152
- it 'indifferent_update' do
153
- hash = Gorillib::HashWithIndifferentAccess.new
154
- hash[:a] = 'a'
155
- hash['b'] = 'b'
156
-
157
- updated_with_strings = hash.update(@strings)
158
- updated_with_symbols = hash.update(@symbols)
159
- updated_with_mixed = hash.update(@mixed)
160
-
161
- 1.should == updated_with_strings[:a]
162
- 1.should == updated_with_strings['a']
163
- 2.should == updated_with_strings['b']
164
-
165
- 1.should == updated_with_symbols[:a]
166
- 2.should == updated_with_symbols['b']
167
- 2.should == updated_with_symbols[:b]
168
-
169
- 1.should == updated_with_mixed[:a]
170
- 2.should == updated_with_mixed['b']
171
-
172
- [updated_with_strings, updated_with_symbols, updated_with_mixed].all?{ |h| h.keys.size == 2 }.should be_true
173
- end
174
-
175
- it 'indifferent_merging' do
176
- hash = Gorillib::HashWithIndifferentAccess.new
177
- hash[:a] = 'failure'
178
- hash['b'] = 'failure'
179
-
180
- other = { 'a' => 1, :b => 2 }
181
-
182
- merged = hash.merge(other)
183
-
184
- merged.class.should == Gorillib::HashWithIndifferentAccess
185
- merged[:a].should == 1
186
- merged['b'].should == 2
187
-
188
- hash.update(other)
189
-
190
- hash[:a].should == 1
191
- hash['b'].should == 2
192
- end
193
-
194
- it 'indifferent_reverse_merging' do
195
- hash = Gorillib::HashWithIndifferentAccess.new('some' => 'value', 'other' => 'value')
196
- hash.reverse_merge!(:some => 'noclobber', :another => 'clobber')
197
- hash[:some].should == 'value'
198
- hash[:another].should == 'clobber'
199
- end
200
-
201
- it 'indifferent_deleting' do
202
- get_hash = proc{ { :a => 'foo' }.with_indifferent_access }
203
- hash = get_hash.call
204
- 'foo'.should == hash.delete(:a)
205
- nil.should == hash.delete(:a)
206
- hash = get_hash.call
207
- 'foo'.should == hash.delete('a')
208
- nil.should == hash.delete('a')
209
- end
210
-
211
- it 'indifferent_to_hash' do
212
- # Should convert to a Hash with String keys.
213
- @mixed.with_indifferent_access.to_hash.should == @strings
214
-
215
- # Should preserve the default value.
216
- mixed_with_default = @mixed.dup
217
- mixed_with_default.default = '1234'
218
- roundtrip = mixed_with_default.with_indifferent_access.to_hash
219
- roundtrip.should == @strings
220
- roundtrip.default.should == '1234'
221
- end
222
-
223
- it 'indifferent_hash_with_array_of_hashes' do
224
- hash = { "urls" => { "url" => [ { "address" => "1" }, { "address" => "2" } ] }}.with_indifferent_access
225
- hash[:urls][:url].first[:address].should == "1"
226
- end
227
-
228
- it 'should_preserve_array_subclass_when_value_is_array' do
229
- array = SubclassingArray.new
230
- array << { "address" => "1" }
231
- hash = { "urls" => { "url" => array }}.with_indifferent_access
232
- hash[:urls][:url].class.should == SubclassingArray
233
- end
234
-
235
- it 'should_preserve_array_class_when_hash_value_is_frozen_array' do
236
- array = SubclassingArray.new
237
- array << { "address" => "1" }
238
- hash = { "urls" => { "url" => array.freeze }}.with_indifferent_access
239
- hash[:urls][:url].class.should == SubclassingArray
240
- end
241
-
242
- it 'stringify_and_symbolize_keys_on_indifferent_preserves_hash' do
243
- h = Gorillib::HashWithIndifferentAccess.new
244
- h[:first] = 1
245
- h = h.stringify_keys
246
- h['first'].should == 1
247
- h = Gorillib::HashWithIndifferentAccess.new
248
- h['first'] = 1
249
- h = h.symbolize_keys
250
- h[:first].should == 1
251
- end
252
-
253
- it 'to_options_on_indifferent_preserves_hash' do
254
- h = Gorillib::HashWithIndifferentAccess.new
255
- h['first'] = 1
256
- h.to_options!
257
- h['first'].should == 1
258
- end
259
-
260
- it 'indifferent_subhashes' do
261
- h = {'user' => {'id' => 5}}.with_indifferent_access
262
- ['user', :user].each{|user| [:id, 'id'].each{|id| h[user][id].should == 5 }}
263
-
264
- h = {:user => {:id => 5}}.with_indifferent_access
265
- ['user', :user].each{|user| [:id, 'id'].each{|id| h[user][id].should == 5 }}
266
- end
267
-
268
- it 'indifferent_duplication' do
269
- # Should preserve default value
270
- h = Gorillib::HashWithIndifferentAccess.new
271
- h.default = '1234'
272
- h.dup.default.should == h.default
273
-
274
- # Should preserve class for subclasses
275
- h = IndifferentHash.new
276
- h.dup.class.should == h.class
277
- end
278
-
279
- it 'assorted_keys_not_stringified' do
280
- original = {Object.new => 2, 1 => 2, [] => true}
281
- indiff = original.with_indifferent_access
282
- indiff.keys.any?{|k| k.kind_of? String }.should_not be_true
283
- end
284
-
285
- it 'deep_merge' do
286
- hash_1 = { :a => "a", :b => "b", :c => { :c1 => "c1", :c2 => "c2", :c3 => { :d1 => "d1" } } }
287
- hash_2 = { :a => 1, :c => { :c1 => 2, :c3 => { :d2 => "d2" } } }
288
- expected = { :a => 1, :b => "b", :c => { :c1 => 2, :c2 => "c2", :c3 => { :d1 => "d1", :d2 => "d2" } } }
289
- hash_1.deep_merge(hash_2).should == expected
290
-
291
- hash_1.deep_merge!(hash_2)
292
- hash_1.should == expected
293
- end
294
-
295
- it 'deep_merge_on_indifferent_access' do
296
- hash_1 = Gorillib::HashWithIndifferentAccess.new({ :a => "a", :b => "b", :c => { :c1 => "c1", :c2 => "c2", :c3 => { :d1 => "d1" } } })
297
- hash_2 = Gorillib::HashWithIndifferentAccess.new({ :a => 1, :c => { :c1 => 2, :c3 => { :d2 => "d2" } } })
298
- hash_3 = { :a => 1, :c => { :c1 => 2, :c3 => { :d2 => "d2" } } }
299
- expected = { "a" => 1, "b" => "b", "c" => { "c1" => 2, "c2" => "c2", "c3" => { "d1" => "d1", "d2" => "d2" } } }
300
- hash_1.deep_merge(hash_2).should == expected
301
- hash_1.deep_merge(hash_3).should == expected
302
-
303
- hash_1.deep_merge!(hash_2)
304
- hash_1.should == expected
305
- end
306
-
307
- it 'deep_dup' do
308
- hash = { :a => { :b => 'b' } }
309
- dup = hash.deep_dup
310
- dup[:a][:c] = 'c'
311
- hash[:a][:c].should == nil
312
- dup[:a][:c].should == 'c'
313
- end
314
-
315
- it 'deep_dup_initialize' do
316
- zero_hash = Hash.new 0
317
- hash = { :a => zero_hash }
318
- dup = hash.deep_dup
319
- dup[:a][44].should == 0
320
- end
321
-
322
- it 'store_on_indifferent_access' do
323
- hash = Gorillib::HashWithIndifferentAccess.new
324
- hash.store(:test1, 1)
325
- hash.store('test1', 11)
326
- hash[:test2] = 2
327
- hash['test2'] = 22
328
- expected = { "test1" => 11, "test2" => 22 }
329
- hash.should == expected
330
- end
331
-
332
- it 'indifferent_slice' do
333
- original = { :a => 'x', :b => 'y', :c => 10 }.with_indifferent_access
334
- expected = { :a => 'x', :b => 'y' }.with_indifferent_access
335
-
336
- [['a', 'b'], [:a, :b]].each do |keys|
337
- # Should return a new hash with only the given keys.
338
- original.slice(*keys).should == expected
339
- original.should_not == expected
340
- end
341
- end
342
-
343
- it 'indifferent_slice_inplace' do
344
- original = { :a => 'x', :b => 'y', :c => 10 }.with_indifferent_access
345
- expected = { :c => 10 }.with_indifferent_access
346
-
347
- [['a', 'b'], [:a, :b]].each do |keys|
348
- # Should replace the hash with only the given keys.
349
- copy = original.dup
350
- copy.slice!(*keys).should == expected
351
- end
352
- end
353
-
354
- it 'indifferent_slice_access_with_symbols' do
355
- original = {'login' => 'bender', 'password' => 'shiny', 'stuff' => 'foo'}
356
- original = original.with_indifferent_access
357
-
358
- slice = original.slice(:login, :password)
359
-
360
- slice[:login].should == 'bender'
361
- slice['login'].should == 'bender'
362
- end
363
-
364
- it 'should_use_default_value_for_unknown_key' do
365
- hash_wia = Gorillib::HashWithIndifferentAccess.new(3)
366
- hash_wia[:new_key].should == 3
367
- end
368
-
369
- it 'should_use_default_value_if_no_key_is_supplied' do
370
- hash_wia = Gorillib::HashWithIndifferentAccess.new(3)
371
- hash_wia.default.should == 3
372
- end
373
-
374
- it 'should_nil_if_no_default_value_is_supplied' do
375
- hash_wia = Gorillib::HashWithIndifferentAccess.new
376
- hash_wia.default.should be_nil
377
- end
378
-
379
- it 'should_return_dup_for_with_indifferent_access' do
380
- hash_wia = Gorillib::HashWithIndifferentAccess.new
381
- hash_wia.with_indifferent_access.should == hash_wia
382
- hash_wia.with_indifferent_access.should_not equal(hash_wia)
383
- end
384
-
385
- it 'should_copy_the_default_value_when_converting_to_hash_with_indifferent_access' do
386
- hash = Hash.new(3)
387
- hash_wia = hash.with_indifferent_access
388
- hash_wia.default.should == 3
389
- end
390
-
391
- end
@@ -1,43 +0,0 @@
1
- require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
- require 'gorillib/metaprogramming/cattr_accessor'
3
-
4
- describe 'metaprogramming' do
5
- describe 'cattr_accessor' do
6
- before do
7
- @class = Class.new do
8
- cattr_accessor :foo
9
- cattr_accessor :bar, :instance_writer => false
10
- cattr_reader :shaq, :instance_reader => false
11
- end
12
- @object = @class.new
13
- end
14
-
15
- it 'does not have an effect if already provided by another library.'
16
-
17
- it 'uses mattr default' do
18
- @class.foo.should be_nil
19
- @object.foo.should be_nil
20
- end
21
-
22
- it 'sets mattr value' do
23
- @class.foo = :test
24
- @object.foo.should == :test
25
-
26
- @object.foo = :test2
27
- @class.foo.should == :test2
28
- end
29
-
30
- it 'with instance_writer => false, does not create an instance writer' do
31
- @class.should respond_to(:foo)
32
- @class.should respond_to(:foo=)
33
- @object.should respond_to(:bar)
34
- @object.should_not respond_to(:bar=)
35
- end
36
-
37
- it 'with instance_reader => false, does not create an instance reader' do
38
- @class.should respond_to(:shaq)
39
- @object.should_not respond_to(:shaq)
40
- @object.should_not respond_to(:shaq=)
41
- end
42
- end
43
- end
@@ -1,45 +0,0 @@
1
- require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
- require 'gorillib/metaprogramming/mattr_accessor'
3
-
4
- describe Module do
5
- describe 'mattr_accessor' do
6
- before do
7
- m = @module = Module.new do
8
- mattr_accessor :foo
9
- mattr_accessor :bar, :instance_writer => false
10
- mattr_reader :shaq, :instance_reader => false
11
- end
12
- @class = Class.new
13
- @class.instance_eval { include m }
14
- @object = @class.new
15
- end
16
-
17
- it 'does not have an effect if already provided by another library.'
18
-
19
- it 'uses mattr default' do
20
- @module.foo.should be_nil
21
- @object.foo.should be_nil
22
- end
23
-
24
- it 'sets mattr value' do
25
- @module.foo = :test
26
- @object.foo.should == :test
27
-
28
- @object.foo = :test2
29
- @module.foo.should == :test2
30
- end
31
-
32
- it 'with :instance_writer => false, does not create instance writer' do
33
- @module.should respond_to(:foo)
34
- @module.should respond_to(:foo=)
35
- @object.should respond_to(:bar)
36
- @object.should_not respond_to(:bar=)
37
- end
38
-
39
- it 'with :instance_writer => false, does not create instance reader' do
40
- @module.should respond_to(:shaq)
41
- @object.should_not respond_to(:shaq)
42
- @object.should_not respond_to(:shaq=)
43
- end
44
- end
45
- end