dragonfly 0.5.7 → 0.6.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 (77) hide show
  1. data/.gitignore +1 -0
  2. data/.yardopts +1 -0
  3. data/History.md +109 -0
  4. data/VERSION +1 -1
  5. data/config.rb +1 -1
  6. data/dragonfly.gemspec +19 -16
  7. data/extra_docs/ActiveRecord.md +8 -7
  8. data/extra_docs/Analysers.md +1 -1
  9. data/extra_docs/Encoding.md +1 -1
  10. data/extra_docs/ExampleUseCases.md +66 -73
  11. data/extra_docs/GettingStarted.md +1 -1
  12. data/extra_docs/Processing.md +1 -1
  13. data/extra_docs/Shortcuts.md +2 -2
  14. data/extra_docs/UsingWithRails.md +25 -37
  15. data/features/rails_2.3.5.feature +1 -8
  16. data/features/rails_3.0.0.beta3.feature +7 -0
  17. data/features/steps/rails_steps.rb +1 -11
  18. data/features/support/env.rb +1 -1
  19. data/fixtures/files/app/views/albums/show.html.erb +1 -0
  20. data/fixtures/files/config/initializers/{aaa_dragonfly_load_path.rb → dragonfly.rb} +1 -0
  21. data/fixtures/rails_2.3.5/template.rb +4 -6
  22. data/fixtures/rails_3.0.0.beta3/template.rb +16 -0
  23. data/lib/dragonfly.rb +23 -1
  24. data/lib/dragonfly/active_record_extensions/attachment.rb +1 -1
  25. data/lib/dragonfly/analyser_list.rb +4 -0
  26. data/lib/dragonfly/analysis/base.rb +1 -0
  27. data/lib/dragonfly/analysis/r_magick_analyser.rb +7 -0
  28. data/lib/dragonfly/app.rb +3 -11
  29. data/lib/dragonfly/belongs_to_app.rb +24 -0
  30. data/lib/dragonfly/config/heroku_rails_images.rb +23 -0
  31. data/lib/dragonfly/config/r_magick_images.rb +69 -0
  32. data/lib/dragonfly/config/r_magick_text.rb +25 -0
  33. data/lib/dragonfly/config/rails_defaults.rb +18 -0
  34. data/lib/dragonfly/config/rails_images.rb +13 -0
  35. data/lib/dragonfly/configurable.rb +4 -3
  36. data/lib/dragonfly/data_storage/base.rb +2 -0
  37. data/lib/dragonfly/data_storage/s3data_store.rb +11 -4
  38. data/lib/dragonfly/delegator.rb +22 -10
  39. data/lib/dragonfly/encoder_list.rb +4 -0
  40. data/lib/dragonfly/encoding/base.rb +1 -0
  41. data/lib/dragonfly/encoding/r_magick_encoder.rb +52 -8
  42. data/lib/dragonfly/extended_temp_object.rb +26 -27
  43. data/lib/dragonfly/processing/base.rb +1 -0
  44. data/lib/dragonfly/processing/r_magick_processor.rb +12 -127
  45. data/lib/dragonfly/processing/r_magick_text_processor.rb +155 -0
  46. data/lib/dragonfly/processor_list.rb +4 -0
  47. data/lib/dragonfly/rails/images.rb +2 -14
  48. data/lib/dragonfly/temp_object.rb +41 -34
  49. data/spec/dragonfly/active_record_extensions/migration.rb +7 -0
  50. data/spec/dragonfly/active_record_extensions/model_spec.rb +10 -11
  51. data/spec/dragonfly/active_record_extensions/spec_helper.rb +1 -1
  52. data/spec/dragonfly/analysis/r_magick_analyser_spec.rb +14 -1
  53. data/spec/dragonfly/belongs_to_app_spec.rb +55 -0
  54. data/spec/dragonfly/configurable_spec.rb +21 -6
  55. data/spec/dragonfly/data_storage/s3_data_store_spec.rb +1 -0
  56. data/spec/dragonfly/delegator_spec.rb +23 -12
  57. data/spec/dragonfly/extended_temp_object_spec.rb +13 -29
  58. data/spec/dragonfly/processing/{rmagick_processor_spec.rb → r_magick_processor_spec.rb} +8 -75
  59. data/spec/dragonfly/processing/r_magick_text_processor_spec.rb +84 -0
  60. data/spec/dragonfly/temp_object_spec.rb +126 -151
  61. data/spec/dragonfly_spec.rb +12 -0
  62. data/spec/ginger_scenarios.rb +2 -2
  63. data/spec/image_matchers.rb +2 -2
  64. data/yard/setup.rb +12 -2
  65. data/yard/templates/default/fulldoc/html/css/common.css +1 -2
  66. data/yard/templates/default/layout/html/layout.erb +3 -2
  67. metadata +21 -18
  68. data/History.txt +0 -75
  69. data/features/rails_3.0.0.beta.feature +0 -15
  70. data/fixtures/dragonfly_setup.rb +0 -1
  71. data/fixtures/rails +0 -22
  72. data/fixtures/rails_3.0.0.beta/template.rb +0 -13
  73. data/generators/dragonfly_app/USAGE +0 -16
  74. data/generators/dragonfly_app/dragonfly_app_generator.rb +0 -24
  75. data/generators/dragonfly_app/templates/initializer.erb +0 -35
  76. data/lib/dragonfly/r_magick_configuration.rb +0 -67
  77. data/spec/dragonfly/active_record_extensions/initializer.rb +0 -1
@@ -177,17 +177,32 @@ describe Dragonfly::Configurable do
177
177
  end
178
178
 
179
179
  describe "configuring with a configurer" do
180
- it "should allow configuration by a configurer" do
181
- cool_configuration = Object.new
182
- def cool_configuration.apply_configuration(car)
180
+ before(:each) do
181
+ @cool_configuration = Object.new
182
+ def @cool_configuration.apply_configuration(car, colour=nil)
183
183
  car.configure do |c|
184
- c.colour = 'vermelho'
184
+ c.colour = (colour || 'vermelho')
185
185
  end
186
186
  end
187
- @car.configure_with(cool_configuration)
187
+ end
188
+
189
+ it "should allow configuration by a configurer" do
190
+ @car.configure_with(@cool_configuration)
188
191
  @car.colour.should == 'vermelho'
189
192
  @car.top_speed.should == 216
190
193
  end
194
+
195
+ it "should pass any args through to the configurer" do
196
+ @car.configure_with(@cool_configuration, 'preto')
197
+ @car.colour.should == 'preto'
198
+ end
199
+
200
+ it "should yield a block for any extra configuration" do
201
+ @car.configure_with(@cool_configuration) do |c|
202
+ c.colour = 'branco'
203
+ end
204
+ @car.colour.should == 'branco'
205
+ end
191
206
  end
192
207
 
193
- end
208
+ end
@@ -33,6 +33,7 @@ describe Dragonfly::DataStorage::S3DataStore do
33
33
  :secret_access_key => @data_store.secret_access_key
34
34
  )
35
35
  AWS::S3::Bucket.stub!(:create).with(@data_store.bucket_name)
36
+ AWS::S3::Service.stub!(:buckets).and_return([])
36
37
  AWS::S3::S3Object.stub!(:store).with(anything, anything, @data_store.bucket_name)
37
38
  AWS::S3::S3Object.stub!(:value).with(anything, @data_store.bucket_name)
38
39
  AWS::S3::S3Object.stub!(:delete).with(anything, @data_store.bucket_name)
@@ -15,6 +15,7 @@ class CarDriver
15
15
  end
16
16
 
17
17
  class LorryDriver
18
+ include Dragonfly::BelongsToApp
18
19
  include Dragonfly::Delegatable
19
20
 
20
21
  def drive(lorry)
@@ -54,8 +55,12 @@ describe Dragonfly::Delegator do
54
55
  lambda{ @delegator.drive }.should raise_error(NoMethodError)
55
56
  end
56
57
 
57
- it "should should return callable_methods as an empty array" do
58
- @delegator.callable_methods.should == []
58
+ it "should should return delegatable_methods as an empty array" do
59
+ @delegator.delegatable_methods.should == []
60
+ end
61
+
62
+ it "should return the object registered when registering" do
63
+ @delegator.register(CarDriver).should be_a(CarDriver)
59
64
  end
60
65
 
61
66
  end
@@ -63,8 +68,9 @@ describe Dragonfly::Delegator do
63
68
  describe "after registering a number of classes" do
64
69
 
65
70
  before(:each) do
66
- @delegator.register(CarDriver)
67
- @delegator.register(LorryDriver)
71
+ @delegator.app = Dragonfly::App[:test]
72
+ @car_driver = @delegator.register(CarDriver)
73
+ @lorry_driver = @delegator.register(LorryDriver)
68
74
  end
69
75
 
70
76
  it "should raise an error when calling an unknown method" do
@@ -75,21 +81,25 @@ describe Dragonfly::Delegator do
75
81
  @delegator.open_boot.should == :open_boot
76
82
  @delegator.open_back_doors.should == :open_back_doors
77
83
  end
78
-
84
+
85
+ it "should allow delegating explicitly" do
86
+ @delegator.delegate(:open_boot).should == :open_boot
87
+ end
88
+
79
89
  it "should delegate to the last registered when more than one item implements the method" do
80
90
  @delegator.drive('fishmonger').should == "Driving lorry fishmonger"
81
91
  end
82
92
 
83
93
  it "should return all the callable methods" do
84
- @delegator.callable_methods.sort.should == %w(clean drive open_back_doors open_boot pick_up).map{|m| m.to_method_name }
94
+ @delegator.delegatable_methods.sort.should == %w(clean drive open_back_doors open_boot pick_up).map{|m| m.to_method_name }
85
95
  end
86
96
 
87
97
  it "should say if if has a callable method (as a string)" do
88
- @delegator.has_callable_method?('drive').should be_true
98
+ @delegator.has_delegatable_method?('drive').should be_true
89
99
  end
90
100
 
91
101
  it "should say if if has a callable method (as a symbol)" do
92
- @delegator.has_callable_method?(:drive).should be_true
102
+ @delegator.has_delegatable_method?(:drive).should be_true
93
103
  end
94
104
 
95
105
  it "should skip methods that throw :unable_to_handle" do
@@ -101,10 +111,11 @@ describe Dragonfly::Delegator do
101
111
  end
102
112
 
103
113
  it "should return registered objects" do
104
- objects = @delegator.registered_objects
105
- objects.length.should == 2
106
- objects[0].should be_a(CarDriver)
107
- objects[1].should be_a(LorryDriver)
114
+ @delegator.registered_objects.should == [@car_driver, @lorry_driver]
115
+ end
116
+
117
+ it "should set the registered object's app to its own if object should belong to an app" do
118
+ @lorry_driver.app.should == @delegator.app
108
119
  end
109
120
 
110
121
  it "should enable unregistering classes" do
@@ -2,72 +2,56 @@ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe Dragonfly::ExtendedTempObject do
4
4
 
5
- it "should raise an error if not configured with an app" do
6
- temp_object = Dragonfly::ExtendedTempObject.new('asdf')
7
- lambda{
8
- temp_object.process(:dummy)
9
- }.should raise_error(Dragonfly::ExtendedTempObject::NotConfiguredError)
10
- end
11
-
12
5
  describe "when configured correctly" do
13
6
 
14
7
  before(:each) do
15
- @analyser = mock('analyser', :has_callable_method? => false)
16
- @analyser.stub!(:has_callable_method?).with(:width).and_return(true)
17
- @processor = mock('processor')
18
- @encoder = mock('encoder')
19
- @app = mock('app', :analysers => @analyser, :processors => @processor, :encoders => @encoder)
20
- @klass = Class.new(Dragonfly::ExtendedTempObject)
21
- @klass.app = @app
8
+ @app = Dragonfly::App[:test]
22
9
  end
23
10
 
24
11
  describe "analysis" do
25
12
 
26
13
  before(:each) do
27
- @object = @klass.new('asdf')
14
+ analyser_class = Class.new(Dragonfly::Analysis::Base) do
15
+ def width(temp_object); temp_object.data.size; end
16
+ end
17
+ @analyser = @app.register_analyser(analyser_class)
18
+ @object = Dragonfly::ExtendedTempObject.new('asdf', @app)
28
19
  end
29
20
 
30
21
  it "should respond to something that the analyser responds to" do
31
- @analyser.should_receive(:has_callable_method?).with(:some_method).and_return(true)
32
- @object.should respond_to(:some_method)
22
+ @object.should respond_to(:width)
33
23
  end
34
24
 
35
25
  it "should not respond to something that the analyser doesn't respond to" do
36
- @analyser.should_receive(:has_callable_method?).with(:some_method).and_return(false)
37
- @object.should_not respond_to(:some_method)
26
+ @object.should_not respond_to(:spaghetti)
38
27
  end
39
28
 
40
29
  it "should delegate the analysis to the analyser" do
41
- @analyser.should_receive(:width).with(@object).and_return(4)
42
30
  @object.width.should == 4
43
31
  end
44
32
 
45
33
  it "should cache the result so that it doesn't call it a second time" do
46
34
  @analyser.should_receive(:width).with(@object).and_return(4)
47
35
  @object.width.should == 4
48
-
49
36
  @analyser.should_not_receive(:width)
50
37
  @object.width.should == 4
51
38
  end
52
39
 
53
40
  it "should do the analysis again when it has been modified" do
54
- @analyser.should_receive(:width).with(@object).and_return(4)
55
41
  @object.width.should == 4
56
-
57
42
  @object.modify_self!('hellothisisnew')
58
-
59
- @analyser.should_receive(:width).with(@object).and_return(17)
60
- @object.width.should == 17
61
-
43
+ @object.width.should == 14
62
44
  @analyser.should_not_receive(:width)
63
- @object.width.should == 17
45
+ @object.width.should == 14
64
46
  end
65
47
 
66
48
  end
67
49
 
68
50
  describe "encoding" do
69
51
  before(:each) do
70
- @temp_object = @klass.new('abcde')
52
+ encoder_class = Class.new(Dragonfly::Encoding::Base)
53
+ @encoder = @app.register_encoder(encoder_class)
54
+ @temp_object = Dragonfly::ExtendedTempObject.new('abcde', @app)
71
55
  end
72
56
 
73
57
  it "should encode the data and return the new temp object" do
@@ -134,7 +134,14 @@ describe Dragonfly::Processing::RMagickProcessor do
134
134
  end
135
135
 
136
136
  end
137
-
137
+
138
+ describe "greyscale" do
139
+ it "should not raise an error" do
140
+ # Bit tricky to test
141
+ @processor.greyscale(@image)
142
+ end
143
+ end
144
+
138
145
  describe "resize_and_crop" do
139
146
 
140
147
  it "should do nothing if no args given" do
@@ -190,80 +197,6 @@ describe Dragonfly::Processing::RMagickProcessor do
190
197
  end
191
198
 
192
199
  end
193
-
194
- describe "text" do
195
- before(:each) do
196
- @text = Dragonfly::TempObject.new("mmm")
197
- end
198
-
199
- it "should create a text image, defaulted to png" do
200
- image = @processor.text(@text)
201
- image.should have_width(34)
202
- image.should have_height(14)
203
- image.should have_format('png')
204
- end
205
-
206
- # it "should ignore percent characters used by rmagick"
207
-
208
- describe "padding" do
209
- it "1 number shortcut" do
210
- image = @processor.text(@text, :padding => '10')
211
- image.should have_width(54)
212
- image.should have_height(34)
213
- end
214
- it "2 numbers shortcut" do
215
- image = @processor.text(@text, :padding => '10 5')
216
- image.should have_width(44)
217
- image.should have_height(34)
218
- end
219
- it "3 numbers shortcut" do
220
- image = @processor.text(@text, :padding => '10 5 8')
221
- image.should have_width(44)
222
- image.should have_height(32)
223
- end
224
- it "4 numbers shortcut" do
225
- image = @processor.text(@text, :padding => '1 2 3 4')
226
- image.should have_width(40)
227
- image.should have_height(18)
228
- end
229
- it "should override the general padding declaration with the specific one (e.g. 'padding-left')" do
230
- image = @processor.text(@text, :padding => '10', 'padding-left' => 9)
231
- image.should have_width(53)
232
- image.should have_height(34)
233
- end
234
- it "should ignore 'px' suffixes" do
235
- image = @processor.text(@text, :padding => '1px 2px 3px 4px')
236
- image.should have_width(40)
237
- image.should have_height(18)
238
- end
239
- it "bad padding string" do
240
- lambda{
241
- @processor.text(@text, :padding => '1 2 3 4 5')
242
- }.should raise_error(ArgumentError)
243
- end
244
- end
245
- end
246
-
247
200
  end
248
201
 
249
202
  end
250
-
251
- describe Dragonfly::Processing::RMagickProcessor::HashWithCssStyleKeys do
252
- before(:each) do
253
- @hash = Dragonfly::Processing::RMagickProcessor::HashWithCssStyleKeys[
254
- :font_style => 'normal',
255
- :'font-weight' => 'bold',
256
- 'font_colour' => 'white',
257
- 'font-size' => 23,
258
- :hello => 'there'
259
- ]
260
- end
261
- describe "accessing using underscore symbol style" do
262
- it{ @hash[:font_style].should == 'normal' }
263
- it{ @hash[:font_weight].should == 'bold' }
264
- it{ @hash[:font_colour].should == 'white' }
265
- it{ @hash[:font_size].should == 23 }
266
- it{ @hash[:hello].should == 'there' }
267
- it{ @hash[:non_existent_key].should be_nil }
268
- end
269
- end
@@ -0,0 +1,84 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Dragonfly::Processing::RMagickTextProcessor do
4
+
5
+ describe "text" do
6
+ before(:each) do
7
+ @processor = Dragonfly::Processing::RMagickTextProcessor.new
8
+ @text = Dragonfly::TempObject.new("mmm")
9
+ end
10
+
11
+ it "should create a text image, defaulted to png" do
12
+ image = @processor.text(@text, :font_size => 12)
13
+ image.should have_width(20..40) # approximate
14
+ image.should have_height(10..20)
15
+ image.should have_format('png')
16
+ end
17
+
18
+ # it "should ignore percent characters used by rmagick"
19
+
20
+ describe "padding" do
21
+ before(:each) do
22
+ no_padding_text = @processor.text(@text, :font_size => 12)
23
+ @width = image_properties(no_padding_text)[:width].to_i
24
+ @height = image_properties(no_padding_text)[:height].to_i
25
+ end
26
+ it "1 number shortcut" do
27
+ image = @processor.text(@text, :padding => '10')
28
+ image.should have_width(@width + 20)
29
+ image.should have_height(@height + 20)
30
+ end
31
+ it "2 numbers shortcut" do
32
+ image = @processor.text(@text, :padding => '10 5')
33
+ image.should have_width(@width + 10)
34
+ image.should have_height(@height + 20)
35
+ end
36
+ it "3 numbers shortcut" do
37
+ image = @processor.text(@text, :padding => '10 5 8')
38
+ image.should have_width(@width + 10)
39
+ image.should have_height(@height + 18)
40
+ end
41
+ it "4 numbers shortcut" do
42
+ image = @processor.text(@text, :padding => '1 2 3 4')
43
+ image.should have_width(@width + 6)
44
+ image.should have_height(@height + 4)
45
+ end
46
+ it "should override the general padding declaration with the specific one (e.g. 'padding-left')" do
47
+ image = @processor.text(@text, :padding => '10', 'padding-left' => 9)
48
+ image.should have_width(@width + 19)
49
+ image.should have_height(@height + 20)
50
+ end
51
+ it "should ignore 'px' suffixes" do
52
+ image = @processor.text(@text, :padding => '1px 2px 3px 4px')
53
+ image.should have_width(@width + 6)
54
+ image.should have_height(@height + 4)
55
+ end
56
+ it "bad padding string" do
57
+ lambda{
58
+ @processor.text(@text, :padding => '1 2 3 4 5')
59
+ }.should raise_error(ArgumentError)
60
+ end
61
+ end
62
+ end
63
+
64
+ describe Dragonfly::Processing::RMagickTextProcessor::HashWithCssStyleKeys do
65
+ before(:each) do
66
+ @hash = Dragonfly::Processing::RMagickTextProcessor::HashWithCssStyleKeys[
67
+ :font_style => 'normal',
68
+ :'font-weight' => 'bold',
69
+ 'font_colour' => 'white',
70
+ 'font-size' => 23,
71
+ :hello => 'there'
72
+ ]
73
+ end
74
+ describe "accessing using underscore symbol style" do
75
+ it{ @hash[:font_style].should == 'normal' }
76
+ it{ @hash[:font_weight].should == 'bold' }
77
+ it{ @hash[:font_colour].should == 'white' }
78
+ it{ @hash[:font_size].should == 23 }
79
+ it{ @hash[:hello].should == 'there' }
80
+ it{ @hash[:non_existent_key].should be_nil }
81
+ end
82
+ end
83
+
84
+ end
@@ -4,14 +4,18 @@ describe Dragonfly::TempObject do
4
4
 
5
5
  ####### Helper Methods #######
6
6
 
7
- def new_tempfile(data = File.read(SAMPLES_DIR + '/round.gif'))
7
+ def sample_path(filename)
8
+ File.dirname(__FILE__) + '/../../samples/' + filename
9
+ end
10
+
11
+ def new_tempfile(data='HELLO')
8
12
  tempfile = Tempfile.new('test')
9
13
  tempfile.write(data)
10
14
  tempfile.rewind
11
15
  tempfile
12
16
  end
13
17
 
14
- def new_file(data = File.read(SAMPLES_DIR + '/round.gif'))
18
+ def new_file(data='HELLO')
15
19
  File.open('/tmp/test_file', 'w') do |f|
16
20
  f.write(data)
17
21
  end
@@ -35,116 +39,149 @@ describe Dragonfly::TempObject do
35
39
  }.should raise_error(ArgumentError)
36
40
  end
37
41
 
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.bytesize.should == 8192
44
- end
45
- parts.last.bytesize.should <= 8192
46
- end
47
-
48
- end
49
-
50
- describe "configuring #each" do
42
+ describe "common behaviour", :shared => true do
51
43
 
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
44
  before(:each) do
67
- @gif_string = File.read(SAMPLES_DIR + '/round.gif')
68
- @temp_object = Dragonfly::TempObject.new(@gif_string)
45
+ @temp_object = new_temp_object('HELLO')
69
46
  end
47
+
70
48
  describe "data" do
71
49
  it "should return the data correctly" do
72
- @temp_object.data.should == @gif_string
50
+ @temp_object.data.should == 'HELLO'
73
51
  end
74
52
  end
53
+
75
54
  describe "file" do
76
- it "should lazily create an unclosed tempfile" do
77
- @temp_object.file.should be_a(Tempfile)
78
- @temp_object.file.should_not be_closed
55
+ it "should return a readable file" do
56
+ @temp_object.file.should be_a(File)
79
57
  end
80
58
  it "should contain the correct data" do
81
- @temp_object.file.read.should == @gif_string
59
+ @temp_object.file.read.should == 'HELLO'
60
+ end
61
+ it "should yield a file then close it if a block is given" do
62
+ @temp_object.file do |f|
63
+ f.read.should == 'HELLO'
64
+ f.should_receive :close
65
+ end
66
+ end
67
+ it "should return whatever is returned from the block if a block is given" do
68
+ @temp_object.file do |f|
69
+ 'doogie'
70
+ end.should == 'doogie'
82
71
  end
83
72
  end
84
- describe "each" do
85
- it "should not create a file" do
86
- @temp_object.should_not_receive(:tempfile)
87
- @temp_object.each{}
73
+
74
+ describe "tempfile" do
75
+ it "should create a closed tempfile" do
76
+ @temp_object.tempfile.should be_a(Tempfile)
77
+ @temp_object.tempfile.should be_closed
78
+ end
79
+ it "should contain the correct data" do
80
+ @temp_object.tempfile.open.read.should == 'HELLO'
88
81
  end
89
- it_should_behave_like "common behaviour for #each"
90
82
  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)
83
+
84
+ describe "path" do
85
+ it "should return the absolute file path" do
86
+ @temp_object.path.should == @temp_object.tempfile.path
87
+ end
98
88
  end
99
- describe "data" do
100
- it "should lazily return the correct data" do
101
- @temp_object.data.should == @gif_string
89
+
90
+ describe "size" do
91
+ it "should return the size in bytes" do
92
+ @temp_object.size.should == 5
102
93
  end
103
94
  end
104
- describe "file" do
105
- it "should return the unclosed tempfile" do
106
- @temp_object.file.should be_a(Tempfile)
107
- @temp_object.file.should_not be_closed
108
- @temp_object.file.path.should == @tempfile.path
95
+
96
+ describe "to_file" do
97
+ before(:each) do
98
+ @filename = 'eggnog.txt'
99
+ FileUtils.rm(@filename) if File.exists?(@filename)
100
+ end
101
+ after(:each) do
102
+ FileUtils.rm(@filename) if File.exists?(@filename)
103
+ end
104
+ it "should write to a file" do
105
+ @temp_object.to_file(@filename)
106
+ File.exists?(@filename).should be_true
107
+ end
108
+ it "should write the correct data to the file" do
109
+ @temp_object.to_file(@filename)
110
+ File.read(@filename).should == 'HELLO'
111
+ end
112
+ it "should return a readable file" do
113
+ file = @temp_object.to_file(@filename)
114
+ file.should be_a(File)
115
+ file.read.should == 'HELLO'
109
116
  end
110
117
  end
118
+
111
119
  describe "each" do
112
- it "should not create a data string" do
113
- @temp_object.should_not_receive(:data)
114
- @temp_object.each{}
120
+ it "should yield 8192 bytes each time" do
121
+ temp_object = new_temp_object(File.read(sample_path('round.gif')))
122
+ parts = get_parts(temp_object)
123
+ parts[0...-1].each do |part|
124
+ part.bytesize.should == 8192
125
+ end
126
+ parts.last.bytesize.should <= 8192
127
+ end
128
+ it "should yield the number of bytes specified in the class configuration" do
129
+ klass = Class.new(Dragonfly::TempObject)
130
+ temp_object = new_temp_object(File.read(sample_path('round.gif')), klass)
131
+ klass.block_size = 3001
132
+ parts = get_parts(temp_object)
133
+ parts[0...-1].each do |part|
134
+ part.length.should == 3001
135
+ end
136
+ parts.last.length.should <= 3001
115
137
  end
116
- it_should_behave_like "common behaviour for #each"
117
138
  end
139
+
140
+
118
141
  end
119
142
 
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)
143
+ describe "initializing from a string" do
144
+
145
+ def new_temp_object(data, klass=Dragonfly::TempObject)
146
+ klass.new(data)
124
147
  end
125
- after(:each) do
126
- @file.close
148
+
149
+ it_should_behave_like "common behaviour"
150
+
151
+ it "should not create a file when calling each" do
152
+ temp_object = new_temp_object('HELLO')
153
+ temp_object.should_not_receive(:tempfile)
154
+ temp_object.each{}
127
155
  end
128
- describe "data" do
129
- it "should lazily return the correct data" do
130
- @temp_object.data.should == @file.read
131
- end
156
+ end
157
+
158
+ describe "initializing from a tempfile" do
159
+
160
+ def new_temp_object(data, klass=Dragonfly::TempObject)
161
+ klass.new(new_tempfile(data))
132
162
  end
133
- describe "file" do
134
- it "should lazily return an unclosed tempfile" do
135
- @temp_object.file.should be_a(Tempfile)
136
- @temp_object.file.should_not be_closed
137
- end
138
- it "should contain the correct data" do
139
- @temp_object.file.read.should == @file.read
140
- end
163
+
164
+ it_should_behave_like "common behaviour"
165
+
166
+ it "should not create a data string when calling each" do
167
+ temp_object = new_temp_object('HELLO')
168
+ temp_object.should_not_receive(:data)
169
+ temp_object.each{}
141
170
  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"
171
+ end
172
+
173
+ describe "initializing from a file" do
174
+
175
+ def new_temp_object(data, klass=Dragonfly::TempObject)
176
+ klass.new(new_file(data))
177
+ end
178
+
179
+ it_should_behave_like "common behaviour"
180
+
181
+ it "should not create a data string when calling each" do
182
+ temp_object = new_temp_object('HELLO')
183
+ temp_object.should_not_receive(:data)
184
+ temp_object.each{}
148
185
  end
149
186
  end
150
187
 
@@ -164,13 +201,6 @@ describe Dragonfly::TempObject do
164
201
  end
165
202
  end
166
203
 
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
204
  describe "modify_self!" do
175
205
 
176
206
  before(:each) do
@@ -199,23 +229,10 @@ describe Dragonfly::TempObject do
199
229
  @temp_object.modify_self!(@temp_object)
200
230
  @temp_object.data.should == 'DATA_ONE'
201
231
  end
202
-
203
- end
204
-
205
- describe "size" do
206
-
207
- before(:each) do
208
- @gif_string = File.read(SAMPLES_DIR + '/round.gif')
209
- end
210
-
211
- it "should return the size in bytes when initialized with a string" do
212
- Dragonfly::TempObject.new(@gif_string).size.should == 61346
213
- end
214
- it "should return the size in bytes when initialized with a tempfile" do
215
- Dragonfly::TempObject.new(new_tempfile(@gif_string)).size.should == 61346
216
- end
217
- it "should return the size in bytes when initialized with a file" do
218
- Dragonfly::TempObject.new(new_file(@gif_string)).size.should == 61346
232
+ it "should keep the same name" do
233
+ @temp_object.name = 'billy.bob'
234
+ @temp_object.modify_self!('WASSUP PUNk')
235
+ @temp_object.name.should == 'billy.bob'
219
236
  end
220
237
  end
221
238
 
@@ -279,47 +296,5 @@ describe Dragonfly::TempObject do
279
296
  @temp_object.basename.should be_nil
280
297
  end
281
298
  end
282
-
283
- describe "to_file" do
284
-
285
- describe "common behaviour for to_file", :shared => true do
286
- before(:each) do
287
- @filename = 'eggnog.txt'
288
- FileUtils.rm(@filename) if File.exists?(@filename)
289
- end
290
- after(:each) do
291
- FileUtils.rm(@filename) if File.exists?(@filename)
292
- end
293
- it "should write to a file" do
294
- @temp_object.to_file(@filename)
295
- File.exists?(@filename).should be_true
296
- end
297
- it "should write the correct data to the file" do
298
- @temp_object.to_file(@filename)
299
- File.read(@filename).should == 'HELLO'
300
- end
301
- it "should return a readable file" do
302
- file = @temp_object.to_file(@filename)
303
- file.should be_a(File)
304
- file.read.should == 'HELLO'
305
- end
306
- end
307
-
308
- describe "when initialized with a string" do
309
- before(:each){ @temp_object = Dragonfly::TempObject.new('HELLO') }
310
- it_should_behave_like "common behaviour for to_file"
311
- end
312
-
313
- describe "when initialized with a file" do
314
- before(:each){ @temp_object = Dragonfly::TempObject.new(new_tempfile('HELLO')) }
315
- it_should_behave_like "common behaviour for to_file"
316
- end
317
-
318
- describe "when initialized with a tempfile" do
319
- before(:each){ @temp_object = Dragonfly::TempObject.new(new_file('HELLO')) }
320
- it_should_behave_like "common behaviour for to_file"
321
- end
322
299
 
323
- end
324
-
325
- end
300
+ end