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
@@ -0,0 +1,321 @@
1
+ require 'spec_helper'
2
+
3
+ require 'gorillib/object/blank'
4
+ require 'gorillib/object/try_dup'
5
+ require 'gorillib/hash/slice'
6
+ require 'gorillib/metaprogramming/class_attribute'
7
+ require 'gorillib/string/inflector'
8
+ #
9
+ require 'gorillib/collection'
10
+ require 'gorillib/model/factories'
11
+
12
+ describe '', :model_spec => true do
13
+ let(:mock_factory){ Gorillib::Factory::BaseFactory.new }
14
+
15
+ def test_factory(*args, &block)
16
+ described_class.new(*args, &block)
17
+ end
18
+
19
+ describe Gorillib::Factory do
20
+ describe 'Factory()' do
21
+ it 'turns a proc into an ApplyProcFactory' do
22
+ ff = Gorillib::Factory( ->(obj){ "bob says #{obj}" } )
23
+ ff.receive(3).should == "bob says 3"
24
+ end
25
+ it 'uses a factory directly' do
26
+ ff = Gorillib::Factory::SymbolFactory.new
27
+ Gorillib::Factory(ff).should == ff
28
+ Gorillib::Factory(Gorillib::Factory::SymbolFactory).should == Gorillib::Factory::SymbolFactory
29
+ end
30
+ it 'looks up factories by typename' do
31
+ Gorillib::Factory(:symbol ).should == Gorillib::Factory::SymbolFactory
32
+ Gorillib::Factory(:identical).should == Gorillib::Factory::IdenticalFactory
33
+ end
34
+ it 'looks up factories by class' do
35
+ Gorillib::Factory(Symbol).should == Gorillib::Factory::SymbolFactory
36
+ Gorillib::Factory(String).should == Gorillib::Factory::StringFactory
37
+ end
38
+ it 'calls Gorillib::Factory.receive' do
39
+ x = mock
40
+ Gorillib::Factory.should_receive(:receive).with(x)
41
+ Gorillib::Factory(x)
42
+ end
43
+ end
44
+ end
45
+
46
+ shared_examples_for :it_converts do |conversion_mapping|
47
+ non_native_ok = conversion_mapping.delete(:non_native_ok)
48
+ conversion_mapping.each do |obj, expected_result|
49
+ it "#{obj.inspect} to #{expected_result.inspect}" do
50
+ subject.native?( obj).should be_false
51
+ subject.blankish?(obj).should be_false
52
+ actual_result = subject.receive(obj)
53
+ actual_result.should eql(expected_result)
54
+ unless non_native_ok then subject.native?(actual_result).should be_true ; end
55
+ end
56
+ end
57
+ end
58
+
59
+ shared_examples_for :it_considers_native do |*native_objs|
60
+ it native_objs.inspect do
61
+ native_objs.each do |obj|
62
+ subject.native?( obj).should be_true
63
+ actual_result = subject.receive(obj)
64
+ actual_result.should equal(obj)
65
+ end
66
+ end
67
+ end
68
+
69
+ shared_examples_for :it_considers_blankish do |*blankish_objs|
70
+ it blankish_objs.inspect do
71
+ blankish_objs.each do |obj|
72
+ subject.blankish?(obj).should be_true
73
+ subject.receive(obj).should be_nil
74
+ end
75
+ end
76
+ end
77
+
78
+ shared_examples_for :it_is_a_mismatch_for do |*mismatched_objs|
79
+ it mismatched_objs.inspect do
80
+ mismatched_objs.each do |obj|
81
+ ->{ subject.receive(obj) }.should raise_error(Gorillib::Factory::FactoryMismatchError)
82
+ end
83
+ end
84
+ end
85
+
86
+ shared_examples_for :it_is_registered_as do |*keys|
87
+ it "the factory for #{keys}" do
88
+ keys.each do |key|
89
+ Gorillib::Factory(key).should == described_class
90
+ end
91
+ Gorillib::Factory.send(:factories).to_hash.select{|key,val| val == described_class }.keys.should == keys
92
+ end
93
+ end
94
+
95
+ # __________________________________________________________________________
96
+ #
97
+ #
98
+ #
99
+ describe Gorillib::Factory::StringFactory do
100
+ it_behaves_like :it_considers_native, 'foo', ''
101
+ it_behaves_like :it_converts, :foo => 'foo', 3 => '3', false => "false", [] => "[]"
102
+ it_behaves_like :it_considers_blankish, nil
103
+ it_behaves_like :it_is_registered_as, :string, String
104
+ its(:typename){ should == :string }
105
+ end
106
+
107
+ describe Gorillib::Factory::SymbolFactory do
108
+ it_behaves_like :it_considers_native, :foo, :"symbol :with weird chars"
109
+ it_behaves_like :it_converts, 'foo' => :foo
110
+ it_behaves_like :it_considers_blankish, nil, ""
111
+ it_behaves_like :it_is_a_mismatch_for, 3, false, []
112
+ it_behaves_like :it_is_registered_as, :symbol, Symbol
113
+ its(:typename){ should == :symbol }
114
+ end
115
+
116
+ describe Gorillib::Factory::RegexpFactory do
117
+ it_behaves_like :it_considers_native, /foo/, //
118
+ it_behaves_like :it_converts, 'foo' => /foo/, ".*" => /.*/
119
+ it_behaves_like :it_considers_blankish, nil, ""
120
+ it_behaves_like :it_is_a_mismatch_for, :foo, 3, false, []
121
+ it_behaves_like :it_is_registered_as, :regexp, Regexp
122
+ its(:typename){ should == :regexp }
123
+ end
124
+
125
+ describe Gorillib::Factory::IntegerFactory do
126
+ it_behaves_like :it_considers_native, 1, -1
127
+ it_behaves_like :it_converts, 'one' => 0, '3blindmice' => 3, "0x10" => 0
128
+ it_behaves_like :it_converts, '1.0' => 1, '1' => 1, "0" => 0, "0L" => 0, "1_234_567" => 1234567, "1_234_567.0" => 1234567
129
+ it_behaves_like :it_converts, 1.0 => 1, 1.234567e6 => 1234567, Complex(1,0) => 1
130
+ it_behaves_like :it_considers_blankish, nil, ""
131
+ it_behaves_like :it_is_a_mismatch_for, :foo, false, [], Complex(1,3)
132
+ it_behaves_like :it_is_registered_as, :int, :integer, Integer
133
+ its(:typename){ should == :integer }
134
+ end
135
+
136
+ describe Gorillib::Factory::FloatFactory do
137
+ it_behaves_like :it_considers_native, 1.0, 1.234567e6
138
+ it_behaves_like :it_converts, 'one' => 0.0, '3blindmice' => 3.0, "0x10" => 0.0
139
+ it_behaves_like :it_converts, '1.0' => 1.0, '1' => 1.0, "0" => 0.0, "0L" => 0.0, "1_234_567" => 1234567.0, "1_234_567.0" => 1234567.0
140
+ it_behaves_like :it_converts, 1 => 1.0, -1 => -1.0, Complex(1,0) => 1.0
141
+ it_behaves_like :it_considers_blankish, nil, ""
142
+ it_behaves_like :it_is_a_mismatch_for, :foo, false, []
143
+ it_behaves_like :it_is_registered_as, :float, Float
144
+ its(:typename){ should == :float }
145
+ end
146
+
147
+ describe Gorillib::Factory::ComplexFactory do
148
+ cplx0 = Complex(0) ; cplx1 = Complex(1) ; cplx1f = Complex(1.0) ;
149
+ it_behaves_like :it_considers_native, Complex(1,3), Complex(1,0)
150
+ it_behaves_like :it_converts, 'one' => cplx0, '3blindmice' => Complex(3), "0x10" => cplx0
151
+ it_behaves_like :it_converts, '1.0' => cplx1f, '1' => cplx1, '0' => cplx0, '0L' => cplx0, '1_234_567' => Complex(1234567), '1_234_567.0' => Complex(1234567.0)
152
+ it_behaves_like :it_converts, 1.0 => cplx1f, 1 => cplx1, -1 => Complex(-1), Rational(3,2) => Complex(Rational(3,2),0)
153
+ it_behaves_like :it_considers_blankish, nil, ""
154
+ it_behaves_like :it_is_a_mismatch_for, :foo, false, []
155
+ it_behaves_like :it_is_registered_as, :complex, Complex
156
+ its(:typename){ should == :complex }
157
+ end
158
+
159
+ describe Gorillib::Factory::RationalFactory do
160
+ rat_0 = Rational(0) ; rat1 = Rational(1) ; rat3_2 = Rational(3, 2) ;
161
+ it_behaves_like :it_considers_native, Rational(1, 3), Rational(1, 7)
162
+ it_behaves_like :it_converts, 'one' => rat_0, '3blindmice' => Rational(3), "0x10" => rat_0
163
+ it_behaves_like :it_converts, '1.5' => rat3_2, '1' => rat1, '0' => rat_0, '0L' => rat_0, '1_234_567' => Rational(1234567), '1_234_567.0' => Rational(1234567)
164
+ it_behaves_like :it_converts, 1.5 => rat3_2, 1 => rat1, -1 => Rational(-1), Complex(1.5) => rat3_2
165
+ it_behaves_like :it_considers_blankish, nil, ""
166
+ it_behaves_like :it_is_a_mismatch_for, :foo, false, [], Complex(1.5,3)
167
+ it_behaves_like :it_is_registered_as, :rational, Rational
168
+ its(:typename){ should == :rational }
169
+ end
170
+
171
+ describe Gorillib::Factory::BooleanFactory do
172
+ it_behaves_like :it_considers_native, true, false
173
+ it_behaves_like :it_considers_blankish, nil
174
+ it_behaves_like :it_converts, "false" => false, :false => false
175
+ it_behaves_like :it_converts, "true" => true, :true => true, [] => true, :foo => true, [] => true, Complex(1.5,3) => true, Object.new => true
176
+ it_behaves_like :it_is_registered_as, :boolean
177
+ its(:typename){ should == :boolean }
178
+ end
179
+
180
+ describe Gorillib::Factory::IdenticalFactory do
181
+ it_behaves_like :it_considers_native, true, false, nil, 3, '', 'a string', :a_symbol, [], {}, ->(){ 'a proc' }, Module.new, Complex(1,3), Object.new
182
+ it_behaves_like :it_is_registered_as, :identical, :whatever
183
+ end
184
+ describe ::Whatever do
185
+ it{ described_class.should equal(Gorillib::Factory::IdenticalFactory) }
186
+ end
187
+
188
+ describe Gorillib::Factory::ModuleFactory do
189
+ it_behaves_like :it_considers_blankish, nil
190
+ it_behaves_like :it_considers_native, Module, Module.new, Class, Class.new, Object, String, BasicObject
191
+ it_behaves_like :it_is_a_mismatch_for, true, false, 3, '', 'a string', :a_symbol, [], {}, ->(){ 'a proc' }, Complex(1,3), Object.new
192
+ it_behaves_like :it_is_registered_as, :module, Module
193
+ its(:typename){ should == :module }
194
+ end
195
+
196
+ describe Gorillib::Factory::ClassFactory do
197
+ it_behaves_like :it_considers_blankish, nil
198
+ it_behaves_like :it_considers_native, Module, Class, Class.new, Object, String, BasicObject
199
+ it_behaves_like :it_is_a_mismatch_for, true, false, 3, '', 'a string', :a_symbol, [], {}, ->(){ 'a proc' }, Module.new, Complex(1,3), Object.new
200
+ it_behaves_like :it_is_registered_as, :class, Class
201
+ its(:typename){ should == :class }
202
+ end
203
+
204
+ describe Gorillib::Factory::NilFactory do
205
+ it_behaves_like :it_considers_native, nil
206
+ it_behaves_like :it_is_a_mismatch_for, true, false, 3, '', 'a string', :a_symbol, [], {}, ->(){ 'a proc' }, Module.new, Complex(1,3), Object.new
207
+ it_behaves_like :it_is_registered_as, :nil, NilClass
208
+ its(:typename){ should == :nil_class }
209
+ end
210
+
211
+ describe Gorillib::Factory::TrueFactory do
212
+ it_behaves_like :it_considers_native, true
213
+ it_behaves_like :it_is_a_mismatch_for, false, 3, '', 'a string', :a_symbol, [], {}, ->(){ 'a proc' }, Module.new, Complex(1,3), Object.new
214
+ it_behaves_like :it_is_registered_as, :true, TrueClass
215
+ end
216
+
217
+ describe Gorillib::Factory::FalseFactory do
218
+ it_behaves_like :it_considers_native, false
219
+ it_behaves_like :it_is_a_mismatch_for, true, 3, '', 'a string', :a_symbol, [], {}, ->(){ 'a proc' }, Module.new, Complex(1,3), Object.new
220
+ it_behaves_like :it_is_registered_as, :false, FalseClass
221
+ end
222
+
223
+ describe Gorillib::Factory::RangeFactory do
224
+ it_behaves_like :it_considers_blankish, nil, []
225
+ it_behaves_like :it_considers_native, (1..2), ('a'..'z')
226
+ it_behaves_like :it_is_a_mismatch_for, true, 3, '', 'a string', :a_symbol, [1,2], {}, ->(){ 'a proc' }, Module.new, Complex(1,3), Object.new
227
+ it_behaves_like :it_is_registered_as, :range, Range
228
+ its(:typename){ should == :range }
229
+ end
230
+
231
+ # __________________________________________________________________________
232
+
233
+ # hand it a collection with entries 1, 2, 3 please
234
+ shared_examples_for :an_enumerable_factory do
235
+ it "accepts a factory for its items" do
236
+ mock_factory.should_receive(:receive).with(1)
237
+ mock_factory.should_receive(:receive).with(2)
238
+ mock_factory.should_receive(:receive).with(3)
239
+ factory = test_factory(:items => mock_factory)
240
+ factory.receive( collection_123 )
241
+ end
242
+ it "can generate an empty collection" do
243
+ subject.empty_product.should == empty_collection
244
+ end
245
+ it "lets you override the empty collection" do
246
+ ep = mock; ep.should_receive(:try_dup).and_return 'hey'
247
+ subject = test_factory(:empty_product => ep)
248
+ subject.empty_product.should == 'hey'
249
+ subject = test_factory(:empty_product => ->{ 'yo' })
250
+ subject.empty_product.should == 'yo'
251
+ end
252
+ end
253
+
254
+ describe Gorillib::Factory::HashFactory do
255
+ let(:collection_123){ { 'a' => 1, :b => 2, 'c' => 3 } }
256
+ let(:empty_collection){ {} }
257
+
258
+ it_behaves_like :it_considers_blankish, nil
259
+ it_behaves_like :it_converts, { {} => {}, { "a" => 2 } => { 'a' => 2 }, { :a => 2 } => { :a => 2 }, :non_native_ok => true }
260
+ it_behaves_like :it_is_a_mismatch_for, [1,2,3]
261
+ it_behaves_like :an_enumerable_factory
262
+
263
+ it 'follows examples' do
264
+ test_factory.receive(collection_123).should == { 'a' => 1, :b => 2, 'c' => 3}
265
+ test_factory(:keys => :symbol).receive({'a' => 'x', :b => 'y', 'c' => :z}).should == {:a => 'x', :b => 'y', :c => :z}
266
+ test_factory(:items => :symbol).receive({'a' => 'x', :b => 'y', 'c' => :z}).should == {'a' => :x, :b => :y, 'c' => :z}
267
+ autov_factory = test_factory(:empty_product => Hash.new{|h,k| h[k] = {} })
268
+ result = autov_factory.receive({:a => :b}) ; result.should == { :a => :b }
269
+ result[:flanger][:modacity] = 11 ; result.should == { :a => :b, :flanger => { :modacity => 11 }}
270
+ result2 = autov_factory.receive({:x => :y}) ; result2.should == { :x => :y } ; result.should == { :a => :b, :flanger => { :modacity => 11 }}
271
+ end
272
+
273
+ it "accepts a factory for the keys" do
274
+ mock_factory.should_receive(:receive).with(3).and_return("converted!")
275
+ factory = test_factory(:keys => mock_factory)
276
+ factory.receive( { 3 => 4 } ).should == { 'converted!' => 4 }
277
+ end
278
+ it_behaves_like :it_is_registered_as, :hash, Hash
279
+ its(:typename){ should == :hash }
280
+ end
281
+
282
+ describe Gorillib::Factory::ArrayFactory do
283
+ let(:collection_123){ [1,2,3] }
284
+ let(:empty_collection){ [] }
285
+
286
+ it 'follows examples' do
287
+ test_factory.receive([1,2,3]).should == [1,2,3]
288
+ test_factory(:items => :symbol).receive(['a', 'b', :c]).should == [:a, :b, :c]
289
+ test_factory(:empty_product => [1,2,3]).receive([:a, :b, :c]).should == [1, 2, 3, :a, :b, :c]
290
+ end
291
+
292
+ it_behaves_like :it_considers_blankish, nil
293
+ it_behaves_like :it_converts, { [] => [], {} => [], [1,2,3] => [1,2,3], {:a => :b} => [[:a, :b]], [:a] => [:a], [[]] => [[]], :non_native_ok => true }
294
+ it_behaves_like :an_enumerable_factory
295
+ it_behaves_like :it_is_registered_as, :array, Array
296
+ its(:typename){ should == :array }
297
+ end
298
+
299
+ describe Gorillib::Factory::SetFactory do
300
+ let(:collection_123){ Set.new([1,2,3]) }
301
+ let(:empty_collection){ Set.new }
302
+
303
+ it 'follows examples' do
304
+ test_factory.receive([1,2,3]).should == collection_123
305
+ test_factory(:items => :symbol).receive(['a', 'b', :c]).should == [:a, :b, :c].to_set
306
+ test_factory(:empty_product => [1,2,3].to_set).receive([:a, :b, :c]).should == [1, 2, 3, :a, :b, :c].to_set
307
+
308
+ has_an_empty_array = test_factory.receive( [[]] )
309
+ has_an_empty_array.should == Set.new( [[]] )
310
+ has_an_empty_array.first.should == []
311
+ has_an_empty_array.size.should == 1
312
+ end
313
+
314
+ it_behaves_like :it_considers_blankish, nil
315
+ it_behaves_like :it_converts, { [] => Set.new, {} => Set.new, [1,2,3] => [1,2,3].to_set, {:a => :b} => Set.new({:a => :b}), [:a] => [:a].to_set, :non_native_ok => true }
316
+ it_behaves_like :an_enumerable_factory
317
+ it_behaves_like :it_is_registered_as, :set, Set
318
+ its(:typename){ should == :set }
319
+ end
320
+
321
+ end
@@ -0,0 +1,46 @@
1
+ # describe 'Gorillib::Model::Overlay' do
2
+ #
3
+ #
4
+ # context '.layer' do
5
+ # it 'behaves like a gettersetter collection method'
6
+ # end
7
+ #
8
+ # context '.layers' do
9
+ # it "returns the layered objects, highest-priority first."
10
+ # end
11
+ #
12
+ # context 'setting an attribute' do
13
+ # it 'sets the value on the object'
14
+ # it 'leaves the other layers intact'
15
+ # end
16
+ #
17
+ # context "reading an attribute (with no resolution block)" do
18
+ # it "if set, returns the object's value"
19
+ # it "if unset, returns the first value that is found"
20
+ # it "if unset, and no layer has a value, returns nil"
21
+ # end
22
+ #
23
+ # context "reading an attribute (with resolution block)" do
24
+ # it "calls the resolution block with the values from all layers in order"
25
+ # it "returns the value the block responds with"
26
+ # end
27
+ #
28
+ # end
29
+ #
30
+ #
31
+ # class Params
32
+ # end
33
+ #
34
+ # class CommandlineParams
35
+ # end
36
+ #
37
+ # class EnvVarParams
38
+ # end
39
+ #
40
+ # class CompositeParams
41
+ # layer CommandlineParams, [:x, :y, :z]
42
+ # layer EnvVarParams, [:x, :y, :z]
43
+ #
44
+ # field :x
45
+ # field :y
46
+ # end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ require 'model_test_helpers'
3
+
4
+ require 'json'
5
+
6
+ require 'multi_json'
7
+ #
8
+ require 'gorillib/model'
9
+ require 'gorillib/builder'
10
+ require 'gorillib/model/serialization'
11
+
12
+ describe Gorillib::Model, :model_spec => true do
13
+ subject do
14
+ garage.cars << wildcat
15
+ garage.cars << ford_39
16
+ garage
17
+ end
18
+ let :wired_garage_hash do
19
+ { :cars => [
20
+ {:name=>:wildcat, :make_model=>"Buick Wildcat", :year=>1968, :doors=>2, :engine=>{:name=>" engine", :carburetor=>:stock, :volume=>455, :cylinders=>8, :owner=>nil}},
21
+ {:name=>:ford_39, :make_model=>"Ford Tudor Sedan", :year=>1939, :doors=>2, :engine=>nil}, ] }
22
+ end
23
+
24
+ describe 'to_json' do
25
+ it 'recursively serializes' do
26
+ MultiJson.load(wildcat.to_json).should == {"name"=>"wildcat","make_model"=>"Buick Wildcat","year"=>1968,"doors"=>2,"engine"=>{"name"=>" engine","carburetor"=>"stock","volume"=>455,"cylinders"=>8,"owner"=>nil}}
27
+ end
28
+ it 'recursively serializes' do
29
+ subject.to_json.should == MultiJson.dump(wired_garage_hash)
30
+ end
31
+ end
32
+
33
+ describe 'to_wire' do
34
+ it 'calls to_wire recursively, passing options along' do
35
+ opts = {:dummy=>'options'}
36
+ ford_39.should_receive(:to_wire).with(opts)
37
+ wildcat.engine.should_receive(:to_wire).with(opts)
38
+ subject.to_wire(opts)
39
+ end
40
+
41
+ it 'returns a nested hash' do
42
+ subject.to_wire.should == wired_garage_hash
43
+ end
44
+ it 'aliases method as_json' do
45
+ subject.as_json.should == wired_garage_hash
46
+ end
47
+ end
48
+ end