gorillib 0.1.11 → 0.4.0pre

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 (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
@@ -0,0 +1,312 @@
1
+ # require 'spec_helper'
2
+ # require 'extlib/mash'
3
+ #
4
+ # class AwesomeHash < Hash
5
+ # end
6
+ #
7
+ # describe Mash do
8
+ # before(:each) do
9
+ # @hash = { "mash" => "indifferent", :hash => "different" }
10
+ # @sub = AwesomeHash.new("mash" => "indifferent", :hash => "different")
11
+ # end
12
+ #
13
+ # describe "#initialize" do
14
+ # it 'converts all keys into strings when param is a Hash' do
15
+ # mash = Mash.new(@hash)
16
+ #
17
+ # mash.keys.any? { |key| key.is_a?(Symbol) }.should be(false)
18
+ # end
19
+ #
20
+ # it 'converts all pure Hash values into Mashes if param is a Hash' do
21
+ # mash = Mash.new :hash => @hash
22
+ #
23
+ # mash["hash"].should be_an_instance_of(Mash)
24
+ # # sanity check
25
+ # mash["hash"]["hash"].should == "different"
26
+ # end
27
+ #
28
+ # it 'doesn not convert Hash subclass values into Mashes' do
29
+ # mash = Mash.new :sub => @sub
30
+ # mash["sub"].should be_an_instance_of(AwesomeHash)
31
+ # end
32
+ #
33
+ # it 'converts all value items if value is an Array' do
34
+ # mash = Mash.new :arry => { :hash => [@hash] }
35
+ #
36
+ # mash["arry"].should be_an_instance_of(Mash)
37
+ # # sanity check
38
+ # mash["arry"]["hash"].first["hash"].should == "different"
39
+ #
40
+ # end
41
+ #
42
+ # it 'delegates to superclass constructor if param is not a Hash' do
43
+ # mash = Mash.new("dash berlin")
44
+ #
45
+ # mash["unexisting key"].should == "dash berlin"
46
+ # end
47
+ # end # describe "#initialize"
48
+ #
49
+ #
50
+ #
51
+ # describe "#update" do
52
+ # it 'converts all keys into strings when param is a Hash' do
53
+ # mash = Mash.new(@hash)
54
+ # mash.update("starry" => "night")
55
+ #
56
+ # mash.keys.any? { |key| key.is_a?(Symbol) }.should be(false)
57
+ # end
58
+ #
59
+ # it 'converts all Hash values into Mashes if param is a Hash' do
60
+ # mash = Mash.new :hash => @hash
61
+ # mash.update(:hash => { :hash => "is buggy in Ruby 1.8.6" })
62
+ #
63
+ # mash["hash"].should be_an_instance_of(Mash)
64
+ # end
65
+ # end # describe "#update"
66
+ #
67
+ #
68
+ #
69
+ # describe "#[]=" do
70
+ # it 'converts key into string' do
71
+ # mash = Mash.new(@hash)
72
+ # mash[:hash] = { "starry" => "night" }
73
+ #
74
+ # mash.keys.any? { |key| key.is_a?(Symbol) }.should be(false)
75
+ # end
76
+ #
77
+ # it 'converts all Hash value into Mash' do
78
+ # mash = Mash.new :hash => @hash
79
+ # mash[:hash] = { :hash => "is buggy in Ruby 1.8.6" }
80
+ #
81
+ # mash["hash"].should be_an_instance_of(Mash)
82
+ # end
83
+ # end # describe "#[]="
84
+ #
85
+ #
86
+ #
87
+ # describe "#key?" do
88
+ # before(:each) do
89
+ # @mash = Mash.new(@hash)
90
+ # end
91
+ #
92
+ # it 'converts key before lookup' do
93
+ # @mash.key?("mash").should be(true)
94
+ # @mash.key?(:mash).should be(true)
95
+ #
96
+ # @mash.key?("hash").should be(true)
97
+ # @mash.key?(:hash).should be(true)
98
+ #
99
+ # @mash.key?(:rainclouds).should be(false)
100
+ # @mash.key?("rainclouds").should be(false)
101
+ # end
102
+ #
103
+ # it 'is aliased as include?' do
104
+ # @mash.include?("mash").should be(true)
105
+ # @mash.include?(:mash).should be(true)
106
+ #
107
+ # @mash.include?("hash").should be(true)
108
+ # @mash.include?(:hash).should be(true)
109
+ #
110
+ # @mash.include?(:rainclouds).should be(false)
111
+ # @mash.include?("rainclouds").should be(false)
112
+ # end
113
+ #
114
+ # it 'is aliased as member?' do
115
+ # @mash.member?("mash").should be(true)
116
+ # @mash.member?(:mash).should be(true)
117
+ #
118
+ # @mash.member?("hash").should be(true)
119
+ # @mash.member?(:hash).should be(true)
120
+ #
121
+ # @mash.member?(:rainclouds).should be(false)
122
+ # @mash.member?("rainclouds").should be(false)
123
+ # end
124
+ # end # describe "#key?"
125
+ #
126
+ #
127
+ # describe "#dup" do
128
+ # it 'returns instance of Mash' do
129
+ # Mash.new(@hash).dup.should be_an_instance_of(Mash)
130
+ # end
131
+ #
132
+ # it 'preserves keys' do
133
+ # mash = Mash.new(@hash)
134
+ # dup = mash.dup
135
+ #
136
+ # mash.keys.sort.should == dup.keys.sort
137
+ # end
138
+ #
139
+ # it 'preserves value' do
140
+ # mash = Mash.new(@hash)
141
+ # dup = mash.dup
142
+ #
143
+ # mash.values.sort.should == dup.values.sort
144
+ # end
145
+ # end
146
+ #
147
+ #
148
+ #
149
+ # describe "#to_hash" do
150
+ # it 'returns instance of Mash' do
151
+ # Mash.new(@hash).to_hash.should be_an_instance_of(Hash)
152
+ # end
153
+ #
154
+ # it 'preserves keys' do
155
+ # mash = Mash.new(@hash)
156
+ # converted = mash.to_hash
157
+ #
158
+ # mash.keys.sort.should == converted.keys.sort
159
+ # end
160
+ #
161
+ # it 'preserves value' do
162
+ # mash = Mash.new(@hash)
163
+ # converted = mash.to_hash
164
+ #
165
+ # mash.values.sort.should == converted.values.sort
166
+ # end
167
+ # end
168
+ #
169
+ #
170
+ #
171
+ # describe "#symbolize_keys" do
172
+ # it 'returns instance of Mash' do
173
+ # Mash.new(@hash).symbolize_keys.should be_an_instance_of(Hash)
174
+ # end
175
+ #
176
+ # it 'converts keys to symbols' do
177
+ # mash = Mash.new(@hash)
178
+ # converted = mash.symbolize_keys
179
+ #
180
+ # converted_keys = converted.keys.sort{|k1, k2| k1.to_s <=> k2.to_s}
181
+ # orig_keys = mash.keys.map{|k| k.to_sym}.sort{|i1, i2| i1.to_s <=> i2.to_s}
182
+ #
183
+ # converted_keys.should == orig_keys
184
+ # end
185
+ #
186
+ # it 'preserves value' do
187
+ # mash = Mash.new(@hash)
188
+ # converted = mash.symbolize_keys
189
+ #
190
+ # mash.values.sort.should == converted.values.sort
191
+ # end
192
+ # end
193
+ #
194
+ #
195
+ #
196
+ # describe "#delete" do
197
+ # it 'converts Symbol key into String before deleting' do
198
+ # mash = Mash.new(@hash)
199
+ #
200
+ # mash.delete(:hash)
201
+ # mash.key?("hash").should be(false)
202
+ # end
203
+ #
204
+ # it 'works with String keys as well' do
205
+ # mash = Mash.new(@hash)
206
+ #
207
+ # mash.delete("mash")
208
+ # mash.key?("mash").should be(false)
209
+ # end
210
+ # end
211
+ #
212
+ #
213
+ #
214
+ # describe "#except" do
215
+ # it "converts Symbol key into String before calling super" do
216
+ # mash = Mash.new(@hash)
217
+ #
218
+ # hashless_mash = mash.except(:hash)
219
+ # hashless_mash.key?("hash").should be(false)
220
+ # end
221
+ #
222
+ # it "works with String keys as well" do
223
+ # mash = Mash.new(@hash)
224
+ #
225
+ # mashless_mash = mash.except("mash")
226
+ # mashless_mash.key?("mash").should be(false)
227
+ # end
228
+ #
229
+ # it "works with multiple keys" do
230
+ # mash = Mash.new(@hash)
231
+ #
232
+ # mashless = mash.except("hash", :mash)
233
+ # mashless.key?(:hash).should be(false)
234
+ # mashless.key?("mash").should be(false)
235
+ # end
236
+ #
237
+ # it "should return a mash" do
238
+ # mash = Mash.new(@hash)
239
+ #
240
+ # hashless_mash = mash.except(:hash)
241
+ # hashless_mash.class.should be(Mash)
242
+ # end
243
+ # end
244
+ #
245
+ #
246
+ #
247
+ # describe "#merge" do
248
+ # before(:each) do
249
+ # @mash = Mash.new(@hash).merge(:no => "in between")
250
+ # end
251
+ #
252
+ # it 'returns instance of Mash' do
253
+ # @mash.should be_an_instance_of(Mash)
254
+ # end
255
+ #
256
+ # it 'merges in give Hash' do
257
+ # @mash["no"].should == "in between"
258
+ # end
259
+ # end
260
+ #
261
+ #
262
+ #
263
+ # describe "#fetch" do
264
+ # before(:each) do
265
+ # @mash = Mash.new(@hash).merge(:no => "in between")
266
+ # end
267
+ #
268
+ # it 'converts key before fetching' do
269
+ # @mash.fetch("no").should == "in between"
270
+ # end
271
+ #
272
+ # it 'returns alternative value if key lookup fails' do
273
+ # @mash.fetch("flying", "screwdriver").should == "screwdriver"
274
+ # end
275
+ # end
276
+ #
277
+ #
278
+ # describe "#default" do
279
+ # before(:each) do
280
+ # @mash = Mash.new(:yet_another_technical_revolution)
281
+ # end
282
+ #
283
+ # it 'returns default value unless key exists in mash' do
284
+ # @mash.default("peak oil is now behind, baby").should == :yet_another_technical_revolution
285
+ # end
286
+ #
287
+ # it 'returns existing value if key is Symbol and exists in mash' do
288
+ # @mash.update(:no => "in between")
289
+ # @mash.default(:no).should == "in between"
290
+ # end
291
+ # end
292
+ #
293
+ #
294
+ # describe "#values_at" do
295
+ # before(:each) do
296
+ # @mash = Mash.new(@hash).merge(:no => "in between")
297
+ # end
298
+ #
299
+ # it 'is indifferent to whether keys are strings or symbols' do
300
+ # @mash.values_at("hash", :mash, :no).should == ["different", "indifferent", "in between"]
301
+ # end
302
+ # end
303
+ #
304
+ #
305
+ # describe "#stringify_keys!" do
306
+ # it 'returns the mash itself' do
307
+ # mash = Mash.new(@hash)
308
+ #
309
+ # mash.stringify_keys!.object_id.should == mash.object_id
310
+ # end
311
+ # end
312
+ # end
@@ -1,7 +1,7 @@
1
- require File.expand_path('../spec_helper', File.dirname(__FILE__))
1
+ require 'spec_helper'
2
2
  require 'gorillib/array/compact_blank'
3
3
 
4
- describe Array do
4
+ describe Array, :simple_spec => true do
5
5
 
6
6
  describe '#compact_blank' do
7
7
  it 'with empty' do
@@ -1,4 +1,4 @@
1
- require File.expand_path('../spec_helper', File.dirname(__FILE__))
1
+ require 'spec_helper'
2
2
  require 'gorillib/array/extract_options'
3
3
 
4
4
  class HashSubclass < Hash
@@ -10,7 +10,7 @@ class ExtractableHashSubclass < Hash
10
10
  end
11
11
  end
12
12
 
13
- describe Array do
13
+ describe Array, :simple_spec => true do
14
14
  describe '#extract_options!' do
15
15
  it 'pulls empty hash from empty array' do
16
16
  [].extract_options!.should == {}
@@ -0,0 +1,187 @@
1
+ require 'spec_helper'
2
+
3
+ # libs under test
4
+ require 'gorillib/builder'
5
+ require 'gorillib/builder/field'
6
+
7
+ # testing helpers
8
+ require 'gorillib/hash/compact'
9
+ require 'model_test_helpers'
10
+
11
+ describe Gorillib::Builder, :model_spec => true, :builder_spec => true do
12
+ let(:example_val ){ mock('example val') }
13
+ subject{ car_class }
14
+
15
+ context 'examples:' do
16
+ subject{ car_class }
17
+ it 'type-converts values' do
18
+ obj = subject.receive( :name => 'wildcat', :make_model => 'Buick Wildcat', :year => "1968", :doors => "2" )
19
+ obj.attributes.should == { :name => :wildcat, :make_model => 'Buick Wildcat', :year => 1968, :doors => 2, :engine => nil }
20
+ end
21
+ it 'handles nested structures' do
22
+ obj = subject.receive(
23
+ :name => 'wildcat', :make_model => 'Buick Wildcat', :year => "1968", :doors => "2",
24
+ :engine => { :carburetor => 'edelbrock', :volume => "455", :cylinders => '8' })
25
+ obj.attributes.values_at(:name, :make_model, :year, :doors).should == [:wildcat, 'Buick Wildcat', 1968, 2 ]
26
+ obj.engine.attributes.values_at(:carburetor, :volume, :cylinders).should == [:edelbrock, 455, 8 ]
27
+ end
28
+ it 'lets you dive down' do
29
+ wildcat.engine.attributes.values_at(:carburetor, :volume, :cylinders).should == [:stock, 455, 8 ]
30
+ wildcat.engine(:cylinders => 6) do
31
+ volume 383
32
+ end
33
+ wildcat.engine.attributes.values_at(:carburetor, :volume, :cylinders).should == [:stock, 383, 6 ]
34
+ end
35
+ it 'lazily autovivifies members' do
36
+ ford_39.read_attribute(:engine).should be_nil
37
+ ford_39.engine(:cylinders => 6)
38
+ ford_39.read_attribute(:engine).should be_a(Gorillib::Test::Engine)
39
+ ford_39.engine.read_attribute(:cylinders).should == 6
40
+ end
41
+ end
42
+
43
+ context 'receive!' do
44
+ it 'accepts a configurate block' do
45
+ expect_7 = nil ; expect_obj = nil
46
+ wildcat.receive!({}){ expect_7 = 7 ; expect_obj = self }
47
+ expect_7.should == 7 ; expect_obj.should == wildcat
48
+ expect_7 = nil ; expect_obj = nil
49
+ wildcat.receive!({}){|c| expect_7 = 7 ; expect_obj = c }
50
+ expect_7.should == 7 ; expect_obj.should == wildcat
51
+ end
52
+ end
53
+
54
+ context ".field" do
55
+
56
+ context do
57
+ subject{ car_class.new }
58
+ let(:sample_val){ 'fiat' }
59
+ let(:raw_val ){ :fiat }
60
+ it_behaves_like "a model field", :make_model
61
+ it("#read_attribute is nil if never set"){ subject.read_attribute(:make_model).should == nil }
62
+ end
63
+
64
+ context 'calling the getset "#foo" method' do
65
+ subject{ wildcat }
66
+
67
+ it "with no args calls read_attribute(:foo)" do
68
+ subject.write_attribute(:doors, example_val)
69
+ subject.should_receive(:read_attribute).with(:doors).at_least(:once).and_return(example_val)
70
+ subject.doors.should == example_val
71
+ end
72
+ it "with an argument calls write_attribute(:foo)" do
73
+ subject.write_attribute(:doors, 'gone')
74
+ subject.should_receive(:write_attribute).with(:doors, example_val).and_return('returned')
75
+ result = subject.doors(example_val)
76
+ result.should == 'returned'
77
+ end
78
+ it "with multiple arguments is an error" do
79
+ expect{ subject.doors(1, 2) }.to raise_error(ArgumentError, "wrong number of arguments (2 for 0..1)")
80
+ end
81
+ end
82
+
83
+ it "does not create a writer method #foo=" do
84
+ subject{ car_class }
85
+ subject.should be_method_defined(:doors)
86
+ subject.should_not be_method_defined(:doors=)
87
+ end
88
+ end
89
+
90
+ context ".member" do
91
+ subject{ car_class.new }
92
+ let(:sample_val){ example_engine }
93
+ let(:raw_val ){ example_engine.attributes }
94
+ it_behaves_like "a model field", :engine
95
+ it("#read_attribute is nil if never set"){ subject.read_attribute(:engine).should == nil }
96
+
97
+ it "calling the getset method #foo with no args calls read_attribute(:foo)" do
98
+ wildcat.write_attribute(:doors, example_val)
99
+ wildcat.should_receive(:read_attribute).with(:doors).at_least(:once).and_return(example_val)
100
+ wildcat.doors.should == example_val
101
+ end
102
+ it "calling the getset method #foo with an argument calls write_attribute(:foo)" do
103
+ wildcat.write_attribute(:doors, 'gone')
104
+ wildcat.should_receive(:write_attribute).with(:doors, example_val).and_return('returned')
105
+ result = wildcat.doors(example_val)
106
+ result.should == 'returned'
107
+ end
108
+ it "calling the getset method #foo with multiple arguments is an error" do
109
+ ->{ wildcat.doors(1, 2) }.should raise_error(ArgumentError, "wrong number of arguments (2 for 0..1)")
110
+ end
111
+ it "does not create a writer method #foo=" do
112
+ wildcat.should respond_to(:doors)
113
+ wildcat.should_not respond_to(:doors=)
114
+ end
115
+ end
116
+
117
+ context 'collections' do
118
+ subject{ garage }
119
+ let(:sample_val){ Gorillib::Collection.receive([wildcat], car_class, :name) }
120
+ let(:raw_val ){ [ wildcat.attributes ] }
121
+ it_behaves_like "a model field", :cars
122
+ it("#read_attribute is an empty collection if never set"){ subject.read_attribute(:cars).should == Gorillib::Collection.new }
123
+
124
+ it 'a collection holds named objects' do
125
+ garage.cars.should be_empty
126
+
127
+ # create a car with a hash of attributes
128
+ garage.car(:cadzilla, :make_model => 'Cadillac, Mostly')
129
+ # ...and retrieve it by name
130
+ cadzilla = garage.car(:cadzilla)
131
+
132
+ # add a car explicitly
133
+ garage.car(:wildcat, wildcat)
134
+ garage.car(:wildcat).should equal(wildcat)
135
+
136
+ # duplicate a car
137
+ garage.car(:ford_39, ford_39.attributes.compact)
138
+ garage.car(:ford_39).should ==(ford_39)
139
+ garage.car(:ford_39).should_not equal(ford_39)
140
+
141
+ # examine the whole collection
142
+ garage.cars.keys.should == [:cadzilla, :wildcat, :ford_39]
143
+ garage.cars.should == Gorillib::Collection.receive([cadzilla, wildcat, ford_39], car_class, :name)
144
+ end
145
+ it 'lazily autovivifies collection items' do
146
+ garage.cars.should be_empty
147
+ garage.car(:chimera).should be_a(car_class)
148
+ garage.cars.should == Gorillib::Collection.receive([{:name => :chimera}], car_class, :name)
149
+ end
150
+
151
+ context 'collection getset method' do
152
+ it 'clxn(:name, existing_object) -- replaces with given object, does not call block' do
153
+ test = nil
154
+ subject.car(:wildcat, wildcat).should equal(wildcat){ test = 3 }
155
+ test.should be_nil
156
+ end
157
+ it 'clxn(:name) (missing & no attributes given) -- autovivifies' do
158
+ subject.car(:cadzilla).should == Gorillib::Test::Car.new(:name => :cadzilla)
159
+ end
160
+ it 'clxn(:name, &block) (missing & no attributes given) -- autovivifies, execs block' do
161
+ test = nil
162
+ subject.car(:cadzilla){ test = 7 }
163
+ test.should == 7
164
+ end
165
+ it 'clxn(:name, :attr => val) (missing, attributes given) -- creates item' do
166
+ subject.car(:cadzilla, :doors => 3).should == Gorillib::Test::Car.new(:name => :cadzilla, :doors => 3)
167
+ end
168
+ it 'clxn(:name, :attr => val) (missing, attributes given) -- creates item, execs block' do
169
+ test = nil
170
+ subject.car(:cadzilla, :doors => 3){ test = 7 }
171
+ test.should == 7
172
+ end
173
+ it 'clxn(:name, :attr => val) (present, attributes given) -- updates item' do
174
+ subject.car(:wildcat, wildcat)
175
+ subject.car(:wildcat, :doors => 9)
176
+ wildcat.doors.should == 9
177
+ end
178
+ it 'clxn(:name, :attr => val) (present, attributes given) -- updates item, execs block' do
179
+ subject.car(:wildcat, wildcat)
180
+ subject.car(:wildcat, :doors => 9){ self.make_model 'WILDCAT' }
181
+ wildcat.doors.should == 9
182
+ wildcat.make_model.should == 'WILDCAT'
183
+ end
184
+
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ #
3
+ require 'gorillib/model'
4
+ require 'gorillib/model/field'
5
+ require 'gorillib/model/defaults'
6
+ #
7
+ require 'model_test_helpers'
8
+
9
+ module Gorillib::Test ; end
10
+ module Meta::Gorillib::Test ; end
11
+
12
+ describe Gorillib::Collection, :model_spec => true do
13
+ it 'needs more tests'
14
+ let(:collection_with_mock_clxn) do
15
+ coll = described_class.new
16
+ coll.send(:define_singleton_method, :mock_clxn){ @clxn }
17
+ coll.send(:instance_variable_set, :@clxn, mock('clxn hash') )
18
+ end
19
+
20
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+ require 'gorillib/configurable'
3
+
4
+ describe Gorillib::Configurable do
5
+ include_context 'included_module'
6
+
7
+ it 'defines a config() method on the class' do
8
+ subject.should respond_to(:config)
9
+ end
10
+
11
+ it 'creates a class attribute :settings' do
12
+ subject.should respond_to(:settings)
13
+ subject.receive.should respond_to(:settings)
14
+ end
15
+
16
+ it 'creates a class attribute :configuration_scope' do
17
+ subject.should respond_to(:configuration_scope)
18
+ subject.receive.should respond_to(:configuration_scope)
19
+ end
20
+
21
+ context 'config' do
22
+ let(:test_field) { :gonads }
23
+ let(:test_default) { 'strife' }
24
+ let(:test_type) { String }
25
+
26
+ before(:each) { subject.config(test_field, test_type, :default => test_default) }
27
+
28
+ # it 'defines a field on settings with optional defaults' do
29
+ # subject.settings.send(test_field).should == test_default
30
+ # end
31
+
32
+ it 'defines a receiver for the given field' do
33
+ subject.receive.send(test_field).should == test_default
34
+ end
35
+
36
+ end
37
+
38
+ context 'receive' do
39
+
40
+ it 'resolves configuration in order' do
41
+ subject.settings.should_receive(:load_configuration_in_order!).and_return({})
42
+ subject.receive
43
+ end
44
+
45
+ before do
46
+ subject.class_eval do
47
+ config :yo_momma, String, :default => 'is so ugly'
48
+ config :she, String
49
+ end
50
+ subject.stub(:settings).and_return(test_settings)
51
+ end
52
+
53
+ let(:creation_attrs) { { yo_momma: 'is so fat', she: 'eats wheat THICKS' } }
54
+ let(:configuration) { { yo_momma: 'is so dumb', she: 'got hit by a parked car' } }
55
+ let(:test_settings) { double :settings, :load_configuration_in_order! => configuration }
56
+
57
+ it 'overrides attributes with configuration settings' do
58
+ subject.receive(creation_attrs).attributes.should include(creation_attrs.merge(configuration))
59
+ end
60
+ end
61
+ end
62
+
@@ -1,8 +1,8 @@
1
- require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
- require 'gorillib/datetime/flat'
1
+ require 'spec_helper'
2
+ require 'gorillib/datetime/to_flat'
3
3
  require 'gorillib/datetime/parse'
4
4
 
5
- describe DateTime do
5
+ describe Time, :datetime_spec => true do
6
6
  describe '#parse_safely' do
7
7
  before do
8
8
  @time_utc = Time.parse("2011-02-03T04:05:06 UTC")
@@ -1,8 +1,8 @@
1
- require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
- require 'gorillib/datetime/flat'
1
+ require 'spec_helper'
2
+ require 'gorillib/datetime/to_flat'
3
3
  require 'gorillib/datetime/parse'
4
4
 
5
- describe Time do
5
+ describe Time, :datetime_spec => true do
6
6
  describe '#to_flat' do
7
7
  before do
8
8
  @time_utc = Time.parse("2011-02-03T04:05:06 UTC")
@@ -23,7 +23,7 @@ describe Time do
23
23
  end
24
24
  end
25
25
 
26
- describe Date do
26
+ describe Date, :datetime_spec => true do
27
27
  describe '#to_flat' do
28
28
  before do
29
29
  @date = Date.new(2011, 2, 3)
@@ -1,12 +1,12 @@
1
- require File.expand_path('../spec_helper', File.dirname(__FILE__))
1
+ require 'spec_helper'
2
2
  require 'gorillib/enumerable/sum'
3
3
 
4
4
  Payment = Struct.new(:price)
5
5
  class SummablePayment < Payment
6
- def +(p) self.class.new(price + p.price) end
6
+ def +(val) self.class.new(price + val.price) end
7
7
  end
8
8
 
9
- describe Enumerable do
9
+ describe Enumerable, :simple_spec => true do
10
10
  describe '#sum' do
11
11
  it 'sums lists of numbers to a number' do
12
12
  [5, 15, 10].sum .should == 30
@@ -27,8 +27,8 @@ describe Enumerable do
27
27
 
28
28
  it 'sums object with a synthetic "+" method' do
29
29
  payments = [ SummablePayment.new(5), SummablePayment.new(15) ]
30
- payments.sum .should == SummablePayment.new(20)
31
- payments.sum{|p| p }.should == SummablePayment.new(20)
30
+ payments.sum .should == SummablePayment.new(20)
31
+ payments.sum{|val| val }.should == SummablePayment.new(20)
32
32
  end
33
33
 
34
34
  it 'handles nil sums' do