dragonfly 0.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of dragonfly might be problematic. Click here for more details.

Files changed (75) hide show
  1. data/.gitignore +7 -0
  2. data/LICENSE +20 -0
  3. data/README.markdown +95 -0
  4. data/Rakefile +60 -0
  5. data/VERSION +1 -0
  6. data/config.rb +7 -0
  7. data/config.ru +10 -0
  8. data/dragonfly-rails.gemspec +41 -0
  9. data/dragonfly.gemspec +137 -0
  10. data/features/dragonfly.feature +38 -0
  11. data/features/steps/common_steps.rb +8 -0
  12. data/features/steps/dragonfly_steps.rb +39 -0
  13. data/features/support/env.rb +25 -0
  14. data/features/support/image_helpers.rb +9 -0
  15. data/generators/dragonfly_app/USAGE +16 -0
  16. data/generators/dragonfly_app/dragonfly_app_generator.rb +54 -0
  17. data/generators/dragonfly_app/templates/custom_processing.erb +13 -0
  18. data/generators/dragonfly_app/templates/initializer.erb +7 -0
  19. data/generators/dragonfly_app/templates/metal_file.erb +32 -0
  20. data/irbrc.rb +20 -0
  21. data/lib/dragonfly/active_record_extensions/attachment.rb +117 -0
  22. data/lib/dragonfly/active_record_extensions/class_methods.rb +41 -0
  23. data/lib/dragonfly/active_record_extensions/instance_methods.rb +28 -0
  24. data/lib/dragonfly/active_record_extensions/validations.rb +19 -0
  25. data/lib/dragonfly/active_record_extensions.rb +12 -0
  26. data/lib/dragonfly/analysis/analyser.rb +45 -0
  27. data/lib/dragonfly/analysis/base.rb +10 -0
  28. data/lib/dragonfly/analysis/r_magick_analyser.rb +40 -0
  29. data/lib/dragonfly/app.rb +85 -0
  30. data/lib/dragonfly/app_configuration.rb +9 -0
  31. data/lib/dragonfly/configurable.rb +113 -0
  32. data/lib/dragonfly/core_ext/object.rb +8 -0
  33. data/lib/dragonfly/data_storage/base.rb +19 -0
  34. data/lib/dragonfly/data_storage/file_data_store.rb +72 -0
  35. data/lib/dragonfly/data_storage.rb +9 -0
  36. data/lib/dragonfly/encoding/base.rb +13 -0
  37. data/lib/dragonfly/encoding/r_magick_encoder.rb +17 -0
  38. data/lib/dragonfly/extended_temp_object.rb +94 -0
  39. data/lib/dragonfly/middleware.rb +27 -0
  40. data/lib/dragonfly/parameters.rb +152 -0
  41. data/lib/dragonfly/processing/processor.rb +14 -0
  42. data/lib/dragonfly/processing/r_magick_processor.rb +71 -0
  43. data/lib/dragonfly/r_magick_configuration.rb +47 -0
  44. data/lib/dragonfly/rails/images.rb +20 -0
  45. data/lib/dragonfly/temp_object.rb +118 -0
  46. data/lib/dragonfly/url_handler.rb +148 -0
  47. data/lib/dragonfly.rb +33 -0
  48. data/samples/beach.png +0 -0
  49. data/samples/egg.png +0 -0
  50. data/samples/round.gif +0 -0
  51. data/samples/taj.jpg +0 -0
  52. data/spec/argument_matchers.rb +29 -0
  53. data/spec/dragonfly/active_record_extensions/attachment_spec.rb +8 -0
  54. data/spec/dragonfly/active_record_extensions/initializer.rb +1 -0
  55. data/spec/dragonfly/active_record_extensions/migration.rb +21 -0
  56. data/spec/dragonfly/active_record_extensions/model_spec.rb +400 -0
  57. data/spec/dragonfly/active_record_extensions/models.rb +2 -0
  58. data/spec/dragonfly/active_record_extensions/spec_helper.rb +23 -0
  59. data/spec/dragonfly/analysis/analyser_spec.rb +85 -0
  60. data/spec/dragonfly/analysis/r_magick_analyser_spec.rb +35 -0
  61. data/spec/dragonfly/app_spec.rb +69 -0
  62. data/spec/dragonfly/configurable_spec.rb +193 -0
  63. data/spec/dragonfly/data_storage/data_store_spec.rb +47 -0
  64. data/spec/dragonfly/data_storage/file_data_store_spec.rb +93 -0
  65. data/spec/dragonfly/extended_temp_object_spec.rb +67 -0
  66. data/spec/dragonfly/middleware_spec.rb +44 -0
  67. data/spec/dragonfly/parameters_spec.rb +293 -0
  68. data/spec/dragonfly/processing/rmagick_processor_spec.rb +148 -0
  69. data/spec/dragonfly/temp_object_spec.rb +233 -0
  70. data/spec/dragonfly/url_handler_spec.rb +246 -0
  71. data/spec/dragonfly_spec.rb +4 -0
  72. data/spec/image_matchers.rb +31 -0
  73. data/spec/simple_matchers.rb +14 -0
  74. data/spec/spec_helper.rb +19 -0
  75. metadata +160 -0
@@ -0,0 +1,293 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Dragonfly::Parameters do
4
+
5
+ def standard_attributes
6
+ {
7
+ :uid => 'ahaha',
8
+ :processing_method => :round,
9
+ :format => :gif,
10
+ :processing_options => {:radius => 5},
11
+ :encoding => {:flumps_per_minute => 56}
12
+ }
13
+ end
14
+
15
+ describe "initializing" do
16
+ it "should allow initializing without a hash" do
17
+ parameters = Dragonfly::Parameters.new
18
+ parameters.uid.should be_nil
19
+ end
20
+ it "should allow initializing with a hash" do
21
+ parameters = Dragonfly::Parameters.new(:uid => 'b')
22
+ parameters.uid.should == 'b'
23
+ end
24
+ it "should raise an error if initialized with a bad hash key" do
25
+ lambda{
26
+ Dragonfly::Parameters.new(:fridge => 'cold')
27
+ }.should raise_error(ArgumentError)
28
+ end
29
+ end
30
+
31
+ describe "accessors" do
32
+ before(:each) do
33
+ @parameters = Dragonfly::Parameters.new
34
+ end
35
+ it "should give the accessors the correct defaults" do
36
+ @parameters.uid.should be_nil
37
+ @parameters.processing_method.should be_nil
38
+ @parameters.format.should be_nil
39
+ @parameters.processing_options.should == {}
40
+ @parameters.encoding.should == {}
41
+ end
42
+ it "should provide writers too" do
43
+ @parameters.uid = 'hello'
44
+ @parameters.uid.should == 'hello'
45
+ end
46
+ end
47
+
48
+ describe "array style accessors" do
49
+ before(:each) do
50
+ @parameters = Dragonfly::Parameters.new(:uid => 'hello')
51
+ end
52
+ it "should be the same as calling the corresponding reader" do
53
+ @parameters[:uid].should == @parameters.uid
54
+ end
55
+ it "should be the same as calling the corresponding writer" do
56
+ @parameters[:uid] = 'goodbye'
57
+ @parameters.uid.should == 'goodbye'
58
+ end
59
+ end
60
+
61
+ describe "comparing" do
62
+ before(:each) do
63
+ @parameters1 = Dragonfly::Parameters.new(standard_attributes)
64
+ @parameters2 = Dragonfly::Parameters.new(standard_attributes)
65
+ end
66
+ it "should return true when two have all the same attributes" do
67
+ @parameters1.should == @parameters2
68
+ end
69
+ %w(uid processing_method format processing_options encoding).each do |attribute|
70
+ it "should return false when #{attribute} is different" do
71
+ @parameters2[attribute.to_sym] = 'fish'
72
+ @parameters1.should_not == @parameters2
73
+ end
74
+ end
75
+ end
76
+
77
+ describe "to_hash" do
78
+ it "should return the attributes as a hash" do
79
+ parameters = Dragonfly::Parameters.new(standard_attributes)
80
+ parameters.to_hash.should == standard_attributes
81
+ end
82
+ end
83
+
84
+ describe "custom parameters classes" do
85
+
86
+ before(:each) do
87
+ @parameters_class = Class.new(Dragonfly::Parameters)
88
+ end
89
+
90
+ describe "when defaults are not set" do
91
+ it "should return the standard defaults" do
92
+ parameters = @parameters_class.new
93
+ parameters.processing_method.should be_nil
94
+ parameters.processing_options.should == {}
95
+ parameters.format.should be_nil
96
+ parameters.encoding.should == {}
97
+ end
98
+ end
99
+
100
+ describe "when defaults are set" do
101
+ before(:each) do
102
+ @parameters_class.configure do |c|
103
+ c.default_processing_method = :resize
104
+ c.default_processing_options = {:scale => '0.5'}
105
+ c.default_format = :png
106
+ c.default_encoding = {:bit_rate => 24}
107
+ end
108
+ end
109
+ it "should return the default if not set on parameters" do
110
+ parameters = @parameters_class.new
111
+ parameters.processing_method.should == :resize
112
+ parameters.processing_options.should == {:scale => '0.5'}
113
+ parameters.format.should == :png
114
+ parameters.encoding.should == {:bit_rate => 24}
115
+ end
116
+ it "should return the correct parameter if set" do
117
+ parameters = @parameters_class.new(
118
+ :processing_method => :yo,
119
+ :processing_options => {:a => 'b'},
120
+ :format => :txt,
121
+ :encoding => {:ah => :arg}
122
+ )
123
+ parameters.processing_method.should == :yo
124
+ parameters.processing_options.should == {:a => 'b'}
125
+ parameters.format.should == :txt
126
+ parameters.encoding.should == {:ah => :arg}
127
+ end
128
+ end
129
+
130
+ end
131
+
132
+ describe "validate!" do
133
+ before(:each) do
134
+ @parameters = Dragonfly::Parameters.new(standard_attributes)
135
+ end
136
+ it "should not raise an error when parameters are ok" do
137
+ @parameters.validate!
138
+ end
139
+ it "should raise an error when the uid is not set" do
140
+ @parameters.uid = nil
141
+ lambda{
142
+ @parameters.validate!
143
+ }.should raise_error(Dragonfly::Parameters::InvalidParameters)
144
+ end
145
+ it "should raise an error when the format is not set" do
146
+ @parameters.format = nil
147
+ lambda{
148
+ @parameters.validate!
149
+ }.should raise_error(Dragonfly::Parameters::InvalidParameters)
150
+ end
151
+ it "should not raise an error when other parameters aren't set" do
152
+ parameters = Dragonfly::Parameters.new(:uid => 'asdf', :format => :jpg)
153
+ parameters.validate!
154
+ end
155
+ end
156
+
157
+ describe "shortcuts" do
158
+
159
+ before(:each) do
160
+ @parameters_class = Class.new(Dragonfly::Parameters)
161
+ end
162
+
163
+ it "should allow for setting simple shortcuts" do
164
+ attributes = {
165
+ :processing_method => :duncan,
166
+ :processing_options => {:bill => :gates},
167
+ :format => 'mamamia',
168
+ :encoding => {:doogie => :howser}
169
+ }
170
+ @parameters_class.add_shortcut(:doobie, attributes)
171
+ @parameters_class.from_shortcut(:doobie).should == Dragonfly::Parameters.new(attributes)
172
+ end
173
+
174
+ it "should raise an error if the shortcut doesn't exist" do
175
+ lambda{
176
+ @parameters_class.from_shortcut(:idontexist)
177
+ }.should raise_error(Dragonfly::Parameters::InvalidShortcut)
178
+ end
179
+
180
+ describe "block shortcuts" do
181
+
182
+ before(:each) do
183
+ @parameters_class.add_shortcut(/^hello.*$/, Symbol) do |processing_method, format, matches|
184
+ {:processing_method => processing_method, :format => format}
185
+ end
186
+ end
187
+
188
+ it "should allow for more complex shortcuts by using a block and matching args" do
189
+ parameters = Dragonfly::Parameters.new(:processing_method => 'hellothere', :format => :tif)
190
+ @parameters_class.from_shortcut('hellothere', :tif).should == parameters
191
+ end
192
+
193
+ it "should raise an error if the shortcut doesn't match properly" do
194
+ lambda{
195
+ @parameters_class.from_shortcut('hellothere', 'tif')
196
+ }.should raise_error(Dragonfly::Parameters::InvalidShortcut)
197
+ end
198
+
199
+ it "should raise an error if the shortcut matches but has the wrong number of args" do
200
+ lambda{
201
+ @parameters_class.from_shortcut('hellothere', :tif, 'YO')
202
+ }.should raise_error(Dragonfly::Parameters::InvalidShortcut)
203
+ end
204
+
205
+ end
206
+
207
+ describe "single regexp shortcuts" do
208
+
209
+ it "should yield regexp match data if the args is just one regexp" do
210
+ @parameters_class.add_shortcut(/^hello(.*)$/) do |arg, match_data|
211
+ {:processing_options => {:arg => arg, :match_data => match_data}}
212
+ end
213
+ processing_options = @parameters_class.from_shortcut('hellothere').processing_options
214
+ processing_options[:arg].should == 'hellothere'
215
+ processing_options[:match_data].should be_a(MatchData)
216
+ processing_options[:match_data][1].should == 'there'
217
+ end
218
+
219
+ end
220
+
221
+ end
222
+
223
+ describe ".from_args" do
224
+
225
+ before(:each) do
226
+ @parameters_class = Class.new(Dragonfly::Parameters)
227
+ end
228
+
229
+ it "should be the same as 'new' if empty args" do
230
+ @parameters_class.from_args.should == @parameters_class.new
231
+ end
232
+
233
+ it "should treat the arguments as actual parameter values if args is a single hash" do
234
+ @parameters_class.from_args(:uid => 'some_uid', :processing_method => :resize).
235
+ should == @parameters_class.new(:uid => 'some_uid', :processing_method => :resize)
236
+ end
237
+
238
+ it "should simply return the same parameters if args is a single parameters object" do
239
+ @parameters_class.from_args(@parameters_class.new(:uid => 'some_uid', :processing_method => :resize)).
240
+ should == @parameters_class.new(:uid => 'some_uid', :processing_method => :resize)
241
+ end
242
+
243
+ it "should treat the arguments as shortcut arguments otherwise" do
244
+ @parameters_class.should_receive(:from_shortcut).with('innit').and_return(parameters = mock('parameters'))
245
+ @parameters_class.from_args('innit').should == parameters
246
+ end
247
+
248
+ end
249
+
250
+ describe "unique_signature" do
251
+
252
+ before(:each) do
253
+ @parameters = Dragonfly::Parameters.new(standard_attributes)
254
+ @parameters2 = Dragonfly::Parameters.new(standard_attributes)
255
+ end
256
+
257
+ it "should a unique identifier based on its attributes" do
258
+ @parameters.unique_signature.should be_a(String)
259
+ @parameters.unique_signature.length.should > 0
260
+ end
261
+
262
+ it "should be the same if the attributes are the same" do
263
+ @parameters.unique_signature.should == @parameters2.unique_signature
264
+ end
265
+
266
+ it "should be different when the uid is changed" do
267
+ @parameters2.uid = 'different yo'
268
+ @parameters.unique_signature.should_not == @parameters2.unique_signature
269
+ end
270
+
271
+ it "should be different when the format is changed" do
272
+ @parameters2.format = :tif
273
+ @parameters.unique_signature.should_not == @parameters2.unique_signature
274
+ end
275
+
276
+ it "should be different when the processing_method is changed" do
277
+ @parameters2.processing_method = :doogie
278
+ @parameters.unique_signature.should_not == @parameters2.unique_signature
279
+ end
280
+
281
+ it "should be different when the processing_options are changed" do
282
+ @parameters2.processing_options[:slumdog] = 'millionaire'
283
+ @parameters.unique_signature.should_not == @parameters2.unique_signature
284
+ end
285
+
286
+ it "should be different when the encoding options are changed" do
287
+ @parameters2.encoding[:flumps_per_minute] = 50.3
288
+ @parameters.unique_signature.should_not == @parameters2.unique_signature
289
+ end
290
+
291
+ end
292
+
293
+ end
@@ -0,0 +1,148 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Dragonfly::Processing::RMagickProcessor do
4
+
5
+ before(:each) do
6
+ sample_file = File.dirname(__FILE__) + '/../../../samples/beach.png' # 280x355
7
+ @image = Dragonfly::TempObject.new(File.new(sample_file))
8
+ @processor = Object.new
9
+ @processor.extend Dragonfly::Processing::RMagickProcessor
10
+ end
11
+
12
+ describe "#resize" do
13
+
14
+ it "should work correctly with xNN" do
15
+ image = @processor.resize(@image, :geometry => 'x30')
16
+ image.should have_width(24)
17
+ image.should have_height(30)
18
+ end
19
+
20
+ it "should work correctly with NNx" do
21
+ image = @processor.resize(@image, :geometry => '30x')
22
+ image.should have_width(30)
23
+ image.should have_height(38)
24
+ end
25
+
26
+ it "should work correctly with NNxNN" do
27
+ image = @processor.resize(@image, :geometry => '30x30')
28
+ image.should have_width(24)
29
+ image.should have_height(30)
30
+ end
31
+
32
+ it "should work correctly with NNxNN!" do
33
+ image = @processor.resize(@image, :geometry => '30x30!')
34
+ image.should have_width(30)
35
+ image.should have_height(30)
36
+ end
37
+
38
+ it "should work correctly with NNxNN%" do
39
+ image = @processor.resize(@image, :geometry => '25x50%')
40
+ image.should have_width(70)
41
+ image.should have_height(178)
42
+ end
43
+
44
+ describe "NNxNN>" do
45
+
46
+ it "should not resize if the image is smaller than specified" do
47
+ image = @processor.resize(@image, :geometry => '1000x1000>')
48
+ image.should have_width(280)
49
+ image.should have_height(355)
50
+ end
51
+
52
+ it "should resize if the image is larger than specified" do
53
+ image = @processor.resize(@image, :geometry => '30x30>')
54
+ image.should have_width(24)
55
+ image.should have_height(30)
56
+ end
57
+
58
+ end
59
+
60
+ describe "NNxNN<" do
61
+
62
+ it "should not resize if the image is larger than specified" do
63
+ image = @processor.resize(@image, :geometry => '10x10<')
64
+ image.should have_width(280)
65
+ image.should have_height(355)
66
+ end
67
+
68
+ it "should resize if the image is smaller than specified" do
69
+ image = @processor.resize(@image, :geometry => '400x400<')
70
+ image.should have_width(315)
71
+ image.should have_height(400)
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+
78
+ describe "crop" do # Difficult to test here other than dimensions
79
+
80
+ it "should not crop if no args given" do
81
+ image = @processor.crop(@image)
82
+ image.should have_width(280)
83
+ image.should have_height(355)
84
+ end
85
+
86
+ it "should crop using the offset given" do
87
+ image = @processor.crop(@image, :x => '7', :y => '12')
88
+ image.should have_width(273)
89
+ image.should have_height(343)
90
+ end
91
+
92
+ it "should crop using the dimensions given" do
93
+ image = @processor.crop(@image, :width => '10', :height => '20')
94
+ image.should have_width(10)
95
+ image.should have_height(20)
96
+ end
97
+
98
+ it "should crop in one dimension if given" do
99
+ image = @processor.crop(@image, :width => '10')
100
+ image.should have_width(10)
101
+ image.should have_height(355)
102
+ end
103
+
104
+ it "should take into account the gravity given" do
105
+ image1 = @processor.crop(@image, :width => '10', :height => '10', :gravity => 'nw')
106
+ image2 = @processor.crop(@image, :width => '10', :height => '10', :gravity => 'se')
107
+ image1.should_not == image2
108
+ end
109
+
110
+ it "should clip bits of the image outside of the requested crop area when not nw gravity" do
111
+ # Rmagick was previously throwing an error when the cropping area was outside the image size, when
112
+ # using a gravity other than nw
113
+ image = @processor.crop(@image, :width => '500', :height => '1000', :x => '100', :y => '200', :gravity => 'se')
114
+ image.should have_width(180)
115
+ image.should have_height(155)
116
+ end
117
+
118
+ end
119
+
120
+ describe "resize_and_crop" do
121
+
122
+ it "should do nothing if no args given" do
123
+ image = @processor.resize_and_crop(@image)
124
+ image.should have_width(280)
125
+ image.should have_height(355)
126
+ end
127
+
128
+ it "should crop to the correct dimensions" do
129
+ image = @processor.resize_and_crop(@image, :width => '100', :height => '100')
130
+ image.should have_width(100)
131
+ image.should have_height(100)
132
+ end
133
+
134
+ it "should allow cropping in one dimension" do
135
+ image = @processor.resize_and_crop(@image, :width => '100')
136
+ image.should have_width(100)
137
+ image.should have_height(355)
138
+ end
139
+
140
+ it "should take into account the gravity given" do
141
+ image1 = @processor.resize_and_crop(@image, :width => '10', :height => '10', :gravity => 'nw')
142
+ image2 = @processor.resize_and_crop(@image, :width => '10', :height => '10', :gravity => 'se')
143
+ image1.should_not == image2
144
+ end
145
+
146
+ end
147
+
148
+ end
@@ -0,0 +1,233 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Dragonfly::TempObject do
4
+
5
+ ####### Helper Methods #######
6
+
7
+ def new_tempfile(data = File.read(SAMPLES_DIR + '/round.gif'))
8
+ tempfile = Tempfile.new('test')
9
+ tempfile.write(data)
10
+ tempfile.close
11
+ tempfile
12
+ end
13
+
14
+ def new_file(data = File.read(SAMPLES_DIR + '/round.gif'))
15
+ File.open('/tmp/test_file', 'w') do |f|
16
+ f.write(data)
17
+ end
18
+ File.new('/tmp/test_file')
19
+ end
20
+
21
+ def get_parts(temp_object)
22
+ parts = []
23
+ temp_object.each do |bytes|
24
+ parts << bytes
25
+ end
26
+ parts.length.should >= 2 # Sanity check to check that the sample file is adequate for this test
27
+ parts
28
+ end
29
+
30
+ ###############################
31
+
32
+ it "should raise an error if initialized with a non-string/file/tempfile" do
33
+ lambda{
34
+ Dragonfly::TempObject.new(3)
35
+ }.should raise_error(ArgumentError)
36
+ end
37
+
38
+ describe "common behaviour for #each", :shared => true do
39
+
40
+ it "should yield 8192 bytes each time" do
41
+ parts = get_parts(@temp_object)
42
+ parts[0...-1].each do |part|
43
+ part.length.should == 8192
44
+ end
45
+ parts.last.length.should <= 8192
46
+ end
47
+
48
+ end
49
+
50
+ describe "configuring #each" do
51
+
52
+ it "should yield the number of bytes specified in the class configuration" do
53
+ temp_object_class = Class.new(Dragonfly::TempObject)
54
+ temp_object_class.block_size = 3001
55
+ temp_object = temp_object_class.new(new_tempfile)
56
+ parts = get_parts(temp_object)
57
+ parts[0...-1].each do |part|
58
+ part.length.should == 3001
59
+ end
60
+ parts.last.length.should <= 3001
61
+ end
62
+
63
+ end
64
+
65
+ describe "initializing from a string" do
66
+ before(:each) do
67
+ @gif_string = File.read(SAMPLES_DIR + '/round.gif')
68
+ @temp_object = Dragonfly::TempObject.new(@gif_string)
69
+ end
70
+ describe "data" do
71
+ it "should return the data correctly" do
72
+ @temp_object.data.should == @gif_string
73
+ end
74
+ end
75
+ describe "file" do
76
+ it "should lazily create a closed tempfile" do
77
+ @temp_object.file.should be_a(Tempfile)
78
+ @temp_object.file.should be_closed
79
+ end
80
+ it "should contain the correct data" do
81
+ @temp_object.file.open.read.should == @gif_string
82
+ end
83
+ end
84
+ describe "each" do
85
+ it "should not create a file" do
86
+ @temp_object.should_not_receive(:tempfile)
87
+ @temp_object.each{}
88
+ end
89
+ it_should_behave_like "common behaviour for #each"
90
+ end
91
+ end
92
+
93
+ describe "initializing from a tempfile" do
94
+ before(:each) do
95
+ @gif_string = File.read(SAMPLES_DIR + '/round.gif')
96
+ @tempfile = new_tempfile(@gif_string)
97
+ @temp_object = Dragonfly::TempObject.new(@tempfile)
98
+ end
99
+ describe "data" do
100
+ it "should lazily return the correct data" do
101
+ @temp_object.data.should == @gif_string
102
+ end
103
+ end
104
+ describe "file" do
105
+ it "should return the closed tempfile" do
106
+ @temp_object.file.should be_a(Tempfile)
107
+ @temp_object.file.should be_closed
108
+ @temp_object.file.path.should == @tempfile.path
109
+ end
110
+ end
111
+ describe "each" do
112
+ it "should not create a data string" do
113
+ @temp_object.should_not_receive(:data)
114
+ @temp_object.each{}
115
+ end
116
+ it_should_behave_like "common behaviour for #each"
117
+ end
118
+ end
119
+
120
+ describe "initializing from a file" do
121
+ before(:each) do
122
+ @file = File.new(SAMPLES_DIR + '/beach.png')
123
+ @temp_object = Dragonfly::TempObject.new(@file)
124
+ end
125
+ after(:each) do
126
+ @file.close
127
+ end
128
+ describe "data" do
129
+ it "should lazily return the correct data" do
130
+ @temp_object.data.should == @file.read
131
+ end
132
+ end
133
+ describe "file" do
134
+ it "should lazily return a closed tempfile" do
135
+ @temp_object.file.should be_a(Tempfile)
136
+ @temp_object.file.should be_closed
137
+ end
138
+ it "should contain the correct data" do
139
+ @temp_object.file.open.read.should == @file.read
140
+ end
141
+ end
142
+ describe "each" do
143
+ it "should not create a data string" do
144
+ @temp_object.should_not_receive(:data)
145
+ @temp_object.each{}
146
+ end
147
+ it_should_behave_like "common behaviour for #each"
148
+ end
149
+ end
150
+
151
+ describe "initializing from another temp object" do
152
+ before(:each) do
153
+ @temp_object1 = Dragonfly::TempObject.new(new_tempfile('hello'))
154
+ @temp_object2 = Dragonfly::TempObject.new(@temp_object1)
155
+ end
156
+ it "should not be the same object" do
157
+ @temp_object1.should_not == @temp_object2
158
+ end
159
+ it "should have the same data" do
160
+ @temp_object1.data.should == @temp_object2.data
161
+ end
162
+ it "should have a different file path" do
163
+ @temp_object1.path.should_not == @temp_object2.path
164
+ end
165
+ end
166
+
167
+ describe "path" do
168
+ it "should return the absolute file path" do
169
+ temp_object = Dragonfly::TempObject.new(File.new(SAMPLES_DIR + '/beach.png'))
170
+ temp_object.path.should == temp_object.file.path
171
+ end
172
+ end
173
+
174
+ describe "modify_self!" do
175
+
176
+ before(:each) do
177
+ @temp_object = Dragonfly::TempObject.new('DATA_ONE')
178
+ @temp_object.data # Make sure internal stuff is initialized
179
+ @temp_object.file #
180
+ end
181
+ it "should modify itself" do
182
+ @temp_object.modify_self!('DATA_TWO')
183
+ @temp_object.data.should == 'DATA_TWO'
184
+ end
185
+ it "should return itself" do
186
+ @temp_object.modify_self!('DATA_TWO').should == @temp_object
187
+ end
188
+ it "should modify itself when the new object is a file" do
189
+ @temp_object.modify_self!(File.new(SAMPLES_DIR + '/beach.png'))
190
+ @temp_object.data.should == File.read(SAMPLES_DIR + '/beach.png')
191
+ end
192
+ it "should modify itself when the new object is a tempfile" do
193
+ tempfile = new_tempfile
194
+ data = tempfile.open.read
195
+ @temp_object.modify_self!(tempfile)
196
+ @temp_object.data.should == data
197
+ end
198
+
199
+ end
200
+
201
+ describe "size" do
202
+
203
+ before(:each) do
204
+ @gif_string = File.read(SAMPLES_DIR + '/round.gif')
205
+ end
206
+
207
+ it "should return the size in bytes when initialized with a string" do
208
+ Dragonfly::TempObject.new(@gif_string).size.should == 61346
209
+ end
210
+ it "should return the size in bytes when initialized with a tempfile" do
211
+ Dragonfly::TempObject.new(new_tempfile(@gif_string)).size.should == 61346
212
+ end
213
+ it "should return the size in bytes when initialized with a file" do
214
+ Dragonfly::TempObject.new(new_file(@gif_string)).size.should == 61346
215
+ end
216
+ end
217
+
218
+ describe "name" do
219
+ before(:each) do
220
+ @obj = new_tempfile
221
+ end
222
+ it "should set the name if the initial object responds to 'original filename'" do
223
+ def @obj.original_filename
224
+ 'jimmy.page'
225
+ end
226
+ Dragonfly::TempObject.new(@obj).name.should == 'jimmy.page'
227
+ end
228
+ it "should not set the name if the initial object doesn't respond to 'original filename'" do
229
+ Dragonfly::TempObject.new(@obj).name.should be_nil
230
+ end
231
+ end
232
+
233
+ end