bulldog 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,6 +1,11 @@
1
- == 0.0.2 2009-11-16
1
+ == 0.0.3 2009-11-16
2
+
3
+ * Add a way to respond to processing errors:
4
+ - #process returns true/false.
5
+ - #process! raises a AR::InvalidRecord if any errors.
6
+ - ...just like ActiveRecord::Base::save.
2
7
 
3
- Fixes:
8
+ == 0.0.2 2009-11-16
4
9
 
5
10
  * #process_once was stopping other processors from running.
6
11
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{bulldog}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["George Ogata"]
@@ -21,7 +21,9 @@ module Bulldog
21
21
  #
22
22
  # Run the processors for the named event.
23
23
  #
24
- def process(event_name, *args)
24
+ # Return true if no errors were encountered, false otherwise.
25
+ #
26
+ def process(event_name)
25
27
  reflection.events[event_name].each do |event|
26
28
  if (types = event.attachment_types)
27
29
  next unless types.include?(type)
@@ -29,8 +31,18 @@ module Bulldog
29
31
  processor_type = event.processor_type || default_processor_type
30
32
  processor_class = Processor.const_get(processor_type.to_s.camelize)
31
33
  processor = processor_class.new(self, styles_for_event(event), stream.path)
32
- processor.process(*args, &event.callback)
34
+ processor.process(&event.callback)
33
35
  end
36
+ record.errors.empty?
37
+ end
38
+
39
+ #
40
+ # Like #process, but raise ActiveRecord::RecordInvalid if there
41
+ # are any errors.
42
+ #
43
+ def process!(event_name, &block)
44
+ process(event_name, &block) or
45
+ raise ActiveRecord::RecordInvalid, record
34
46
  end
35
47
 
36
48
  #
@@ -30,7 +30,10 @@ module Bulldog
30
30
  def destroy
31
31
  end
32
32
 
33
- def process(event, *args)
33
+ def process(event, options={})
34
+ end
35
+
36
+ def process!(event, options={})
34
37
  end
35
38
  end
36
39
  end
@@ -61,12 +61,12 @@ module Bulldog
61
61
  # #style will be set to the current style each time the block is
62
62
  # called.
63
63
  #
64
- def process(*args, &block)
64
+ def process(&block)
65
65
  return if styles.empty?
66
66
  styles.each do |style|
67
67
  @style = style
68
68
  begin
69
- process_style(*args, &block)
69
+ process_style(&block)
70
70
  ensure
71
71
  @style = nil
72
72
  end
@@ -14,7 +14,7 @@ module Bulldog
14
14
  @still_frame_callbacks = style_list_map
15
15
  end
16
16
 
17
- def process(*args)
17
+ def process
18
18
  return if styles.empty?
19
19
  super
20
20
  run_ffmpeg
@@ -158,7 +158,8 @@ module Bulldog
158
158
  command = [self.class.ffmpeg_command]
159
159
  command << '-i' << input_file
160
160
  command.concat(arguments)
161
- Bulldog.run(*command)
161
+ Bulldog.run(*command) or
162
+ record.errors.add name, "convert failed (status #$?)"
162
163
  end
163
164
  end
164
165
 
@@ -16,7 +16,7 @@ module Bulldog
16
16
  @tree = ArgumentTree.new(styles)
17
17
  end
18
18
 
19
- def process(*args, &block)
19
+ def process
20
20
  return if styles.empty?
21
21
  super
22
22
  run_convert
@@ -105,7 +105,7 @@ module Bulldog
105
105
  def run_convert
106
106
  add_final_style_arguments
107
107
  output = run_convert_command and
108
- run_convert_callbacks(output)
108
+ run_convert_callbacks(output)
109
109
  end
110
110
 
111
111
  def add_final_style_arguments
@@ -120,7 +120,9 @@ module Bulldog
120
120
 
121
121
  def run_convert_command
122
122
  command = [self.class.convert_command, "#{input_file}[0]", *@tree.arguments].flatten
123
- Bulldog.run(*command)
123
+ output = Bulldog.run(*command) or
124
+ record.errors.add name, "convert failed (status #$?)"
125
+ output
124
126
  end
125
127
 
126
128
  def run_convert_callbacks(output)
@@ -6,9 +6,9 @@ module Bulldog
6
6
  @styles = StyleSet.new
7
7
  end
8
8
 
9
- def process(*args, &block)
9
+ def process(&block)
10
10
  @style = nil
11
- process_style(*args, &block)
11
+ process_style(&block)
12
12
  end
13
13
 
14
14
  def output_file(style_name)
@@ -219,6 +219,17 @@ describe Attachment::Base do
219
219
  end
220
220
  end
221
221
 
222
+ def with_test_processor(options, &block)
223
+ test_processor_class = Class.new(Processor::Base) do
224
+ define_method :process do
225
+ if options[:error]
226
+ record.errors.add name, "error"
227
+ end
228
+ end
229
+ end
230
+ with_temporary_constant_value(Processor, :Test, test_processor_class, &block)
231
+ end
232
+
222
233
  describe "#process" do
223
234
  use_model_class(:Thing)
224
235
 
@@ -280,6 +291,54 @@ describe Attachment::Base do
280
291
  styles.should be_a(StyleSet)
281
292
  styles.map(&:name).should == [:small]
282
293
  end
294
+
295
+ it "should return true if no errors were encountered" do
296
+ with_test_processor(:error => false) do
297
+ Thing.has_attachment :attachment do
298
+ style :one
299
+ process(:on => :event, :with => :test){}
300
+ end
301
+ thing = Thing.new(:attachment => test_empty_file)
302
+ thing.attachment.process(:event).should be_true
303
+ end
304
+ end
305
+
306
+ it "should return false if an error was encountered" do
307
+ with_test_processor(:error => true) do
308
+ Thing.has_attachment :attachment do
309
+ style :one
310
+ process(:on => :event, :with => :test){}
311
+ end
312
+ thing = Thing.new(:attachment => test_empty_file)
313
+ thing.attachment.process(:event).should be_false
314
+ end
315
+ end
316
+ end
317
+
318
+ describe "#process!" do
319
+ use_model_class(:Thing)
320
+
321
+ it "should raise ActiveRecord::RecordInvalid if there are any errors present" do
322
+ with_test_processor(:error => true) do
323
+ Thing.has_attachment :attachment do
324
+ style :one
325
+ process :on => :event, :with => :test
326
+ end
327
+ thing = Thing.new(:attachment => test_empty_file)
328
+ lambda{thing.attachment.process!(:event)}.should raise_error(ActiveRecord::RecordInvalid)
329
+ end
330
+ end
331
+
332
+ it "should not raise ActiveRecord::RecordInvalid if there are no errors present" do
333
+ with_test_processor(:error => false) do
334
+ Thing.has_attachment :attachment do
335
+ style :one
336
+ process :on => :event, :with => :test
337
+ end
338
+ thing = Thing.new(:attachment => test_empty_file)
339
+ lambda{thing.attachment.process!(:event)}.should_not raise_error(ActiveRecord::RecordInvalid)
340
+ end
341
+ end
283
342
  end
284
343
 
285
344
  describe "storable attributes" do
@@ -66,14 +66,14 @@ describe HasAttachment do
66
66
 
67
67
  describe "when there is an attachment set" do
68
68
  it "should trigger the configured callbacks" do
69
- args = nil
69
+ calls = []
70
70
  Thing.has_attachment :photo do
71
71
  style :normal
72
- process(:on => :my_event){|*args|}
72
+ process(:on => :my_event){calls << 1}
73
73
  end
74
74
  thing = Thing.new(:photo => uploaded_file)
75
- thing.process_attachment(:photo, :my_event, 1, 2)
76
- args.should == [1, 2]
75
+ thing.process_attachment(:photo, :my_event)
76
+ calls.should == [1]
77
77
  end
78
78
  end
79
79
 
@@ -85,20 +85,19 @@ describe HasAttachment do
85
85
  process(:on => :my_event){|*args|}
86
86
  end
87
87
  thing = Thing.new(:photo => nil)
88
- thing.process_attachment(:photo, :my_event, 1, 2)
88
+ thing.process_attachment(:photo, :my_event)
89
89
  args.should be_nil
90
90
  end
91
91
  end
92
92
 
93
93
  it "should raise an ArgumentError if the attachment name is invalid" do
94
- args = nil
95
94
  Thing.has_attachment :photo do
96
95
  style :normal
97
- process(:on => :my_event){|*args|}
96
+ process(:on => :my_event){}
98
97
  end
99
98
  thing = Thing.new
100
99
  lambda do
101
- thing.process_attachment(:fail, :my_event, 1, 2)
100
+ thing.process_attachment(:fail, :my_event)
102
101
  end.should raise_error(ArgumentError)
103
102
  end
104
103
 
@@ -54,7 +54,7 @@ describe Processor::Ffmpeg do
54
54
  @thing.video.process(:event)
55
55
  end
56
56
 
57
- describe "when a simple conversion is performed" do
57
+ describe "#process" do
58
58
  before do
59
59
  video_style :output
60
60
  end
@@ -72,6 +72,20 @@ describe Processor::Ffmpeg do
72
72
  end
73
73
  File.read(log_path).should include("[Bulldog] Running: #{ffmpeg}")
74
74
  end
75
+
76
+ it "should add an error to the record if convert fails" do
77
+ video_style :output
78
+ Bulldog.stubs(:run).returns(nil)
79
+ process_video
80
+ @thing.errors.should be_present
81
+ end
82
+
83
+ it "should not add an error to the record if convert succeeds" do
84
+ video_style :output
85
+ Bulldog.stubs(:run).returns('')
86
+ process_video
87
+ @thing.errors.should_not be_present
88
+ end
75
89
  end
76
90
 
77
91
  describe "#encode" do
@@ -78,6 +78,20 @@ describe Processor::ImageMagick do
78
78
  process
79
79
  end
80
80
 
81
+ it "should add an error to the record if convert fails" do
82
+ style :output
83
+ Bulldog.stubs(:run).returns(nil)
84
+ process
85
+ @thing.errors.should be_present
86
+ end
87
+
88
+ it "should not add an error to the record if convert succeeds" do
89
+ style :output
90
+ Bulldog.stubs(:run).returns('')
91
+ process
92
+ @thing.errors.should_not be_present
93
+ end
94
+
81
95
  it "should use the given :quality style attribute" do
82
96
  style :output, :quality => 50
83
97
  Bulldog.expects(:run).once.with(convert, "#{original_path}[0]", '-quality', '50', output_path).returns('')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bulldog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - George Ogata