carrierwave-processor 1.0.1 → 1.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd48afff42e5cf9328e2c5f58c59066e89522bc1
4
- data.tar.gz: 5ac5d20101f1d9fded1066248db67c4ea18a7548
3
+ metadata.gz: 2939328ae65e5281e71838e54b9c6fa23682c00b
4
+ data.tar.gz: 854548f35fd7461d63069d0618866b072226a07d
5
5
  SHA512:
6
- metadata.gz: b98fae5d89e75817a44fcb29265563a1c1efdbf11f58318f70c91af24132de924120838dd286aea0e4a912b695ad034b149f3aef5950e23a9878c186da20ad7b
7
- data.tar.gz: 5cf9146bc008d34c8f0475c2123417968d41d1ad1b174e5c80605854366b5f601c19f9d2f023860ec361eda924d303950e675d4930ba42eb0d1f927ccfba1ea3
6
+ metadata.gz: 8f07d2dd154a5106d18c30d55da9ac9e7347d5dd78c7b3e0d75041b19602840898593a944a8525de89b38081e126d18e1bf0812ff6095794f67a7b0ab96e9e51
7
+ data.tar.gz: f2f263b44b5fe4030c8ab81497e0d4653111017506f2b33232652f23632505f40609bb3c009280ce5f8630ba024d285d752db91b755a8031189c852f042b7182
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem 'rails', :git => "git@github.com:rails/rails.git", :branch => "4-0-stable"
4
+
3
5
  # Specify your gem's dependencies in carrierwave-processing.gemspec
4
6
  gemspec
@@ -1,30 +1,12 @@
1
1
  module CarrierWave
2
2
  module Processor
3
3
  module Dsl
4
- def carrierwave_processor *args, &block
5
- options = args.extract_options!
6
- name = args.first
4
+ def carrierwave_processor name, options={}, &block
7
5
  if name
8
- processor = Node.new options
9
- processor.name = name
10
- processor.instance_eval &block if block
11
- if self.kind_of? CarrierWave::Processor::Node
12
- self.processors ||= {}
13
- self.processors[name] = processor
14
- else
15
- ::CarrierWave::Processor.processors ||= {}
16
- ::CarrierWave::Processor.processors[name] = processor
17
- end
18
- return processor
19
- end
20
- end
21
-
22
- def find_carrierwave_processor name
23
- if self.kind_of? CarrierWave::Processor::Node
24
- self.processors && self.processors[name.to_sym]
25
- else
26
- CarrierWave::Processor.processors && CarrierWave::Processor.processors[name.to_sym]
6
+ ::CarrierWave::Processor.processors ||= {}
7
+ return ::CarrierWave::Processor.processors[name] = {:block => block, :options => options}
27
8
  end
9
+ nil
28
10
  end
29
11
  end
30
12
  end
@@ -0,0 +1,46 @@
1
+ require 'delegate'
2
+ require 'active_support/core_ext/class'
3
+
4
+ class Injector < Module
5
+
6
+ def initialize uploader, opts = {}, &block
7
+ @uploader = uploader
8
+ @outer_version = opts.delete(:outer_version)
9
+ @options = opts
10
+ self.class_eval &block
11
+ @uploader.prepend self
12
+ end
13
+
14
+ def process *args, &block
15
+ processed_options = ::CarrierWave::Processor.arguments_merge *args
16
+ unless @outer_version
17
+ new_if = ::CarrierWave::Processor.conditions_merge(@options[:if], processed_options[:if])
18
+ processed_options[:if] = new_if if new_if
19
+ end
20
+ @uploader.process processed_options, &block
21
+ end
22
+
23
+ def version *args, &block
24
+ options = args.extract_options! || {}
25
+ version_options = @options.merge options
26
+ ifs_array = [@options[:if], options[:if]]
27
+ new_if = ::CarrierWave::Processor.conditions_merge *ifs_array
28
+ version_options[:if] = new_if
29
+ version_options.delete :if if version_options[:if].nil?
30
+ version_options[:from_version] = @outer_version if @outer_version
31
+ passing_options = {:if => ifs_array}
32
+ passing_options[:outer_version] = args.first if args.first
33
+ version_args = version_options.empty? ? args : (args + [version_options])
34
+ @uploader.version *version_args do
35
+ Injector.new(self, passing_options, &block)
36
+ end
37
+ end
38
+
39
+ def method_missing *args, &block
40
+ @uploader.send *args, &block
41
+ end
42
+
43
+ def delay *args, &block
44
+ end
45
+
46
+ end
@@ -6,53 +6,20 @@ module CarrierWave
6
6
 
7
7
  def use_processor *args
8
8
  options = args.extract_options!
9
- conditions = [options.delete(:conditions) || options[:if]].flatten.compact
10
9
  args.each do |processor|
11
- passing_in_options = {}
12
- passing_in_options.merge!(:conditions => conditions) unless conditions.empty?
13
- if processor and processor = find_carrierwave_processor(processor)
14
- load_cw_processors processor, passing_in_options
15
- load_cw_versions processor, passing_in_options
10
+ if processor and not ::CarrierWave::Processor.processors.blank? and real_processor = ::CarrierWave::Processor.processors[processor] and real_processor[:block]
11
+ new_if = [options[:if], real_processor[:options][:if]]
12
+ merged_options = real_processor[:options].merge options
13
+ merged_options[:if] = new_if if new_if
14
+ Injector.new(self, merged_options, &real_processor[:block])
16
15
  else
17
16
  raise ProcessorNotFoundError, processor
18
17
  end
19
18
  end
20
19
  end
21
20
 
22
- private
21
+ alias_method :use_processors, :use_processor
23
22
 
24
- def load_cw_processors processor, options = {}
25
- conditions = options[:conditions] || []
26
- processor.cw_processors.each do |cw_processor|
27
- new_processors = ::CarrierWave::Processor.arguments_merge *cw_processor[:args]
28
- new_conditions = (conditions + [new_processors[:if]]).compact
29
- new_processors[:if] = ::CarrierWave::Processor.conditions_merge(*new_conditions) unless new_conditions.empty?
30
- process new_processors
31
- end
32
- end
33
-
34
- def load_cw_versions processor, options = {}
35
- conditions = options.delete(:conditions) || []
36
- processor.processors.each do |name, v|
37
- new_conditions = (conditions + [v.options[:if]]).compact
38
- condition = ::CarrierWave::Processor.conditions_merge(*new_conditions) unless new_conditions.empty?
39
- version_options = v.options
40
- version_options.merge! options if options
41
- version_options.merge!(:if => condition) if condition
42
- if version_options.empty?
43
- version name do
44
- load_cw_processors v
45
- end
46
- else
47
- version name, version_options do
48
- load_cw_processors v
49
- end
50
- end
51
- next_level_options = {:from_version => name}
52
- next_level_options.merge!(:conditions => new_conditions) unless conditions.empty?
53
- load_cw_versions v, next_level_options
54
- end
55
- end
56
23
  end
57
24
  end
58
25
  end
@@ -1,5 +1,5 @@
1
1
  module CarrierWave
2
2
  module Processor
3
- VERSION = "1.0.1"
3
+ VERSION = "1.0.2"
4
4
  end
5
5
  end
@@ -1,7 +1,7 @@
1
1
  require 'active_support'
2
2
  require 'carrierwave'
3
3
  require "carrierwave/processor/version"
4
- require 'carrierwave/processor/node'
4
+ require "carrierwave/processor/injector"
5
5
  require 'carrierwave/processor/uploader_dsl'
6
6
 
7
7
  module CarrierWave
@@ -15,9 +15,14 @@ module CarrierWave
15
15
  end
16
16
 
17
17
  def self.conditions_merge *args
18
+ args.flatten!
18
19
  args.compact!
19
20
  return nil if args.empty?
20
21
  return args.first if args.length == 1
22
+ self.merge_multiple_conditions *args
23
+ end
24
+
25
+ def self.merge_multiple_conditions *args
21
26
  lambda do |uploader, options|
22
27
  args.inject(true) do |accum, condition|
23
28
  break false unless accum
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ require 'active_support/core_ext/class'
3
+
4
+ describe "ActiveSupport::Concern" do
5
+ it "prepending module does not break class_attribute" do
6
+
7
+ class SomeClass
8
+ prepend Module.new
9
+
10
+ def tester
11
+ "instance"
12
+ end
13
+
14
+ class_attribute :tester, :instance_reader => false, :instance_writer => false
15
+ self.tester = "class attribute"
16
+ end
17
+
18
+ expect(SomeClass.send :singleton_class?).to eq(false)
19
+
20
+ expect(SomeClass.new.tester).to eq("instance")
21
+ end
22
+
23
+ it "prepending module does not break singleton_class check" do
24
+ class Ana
25
+ end
26
+
27
+ Ana.prepend Module.new
28
+
29
+ expect(Ana.send :singleton_class?).to eq(false)
30
+ end
31
+
32
+ end
data/spec/dsl_spec.rb CHANGED
@@ -8,7 +8,7 @@ describe CarrierWave::Processor::Dsl do
8
8
 
9
9
  it "returns processor node on carrierwave_processor call" do
10
10
  processor = carrierwave_processor(:some_processor){}
11
- processor.should be_kind_of CarrierWave::Processor::Node
11
+ processor.should be_kind_of Hash
12
12
  end
13
13
 
14
14
  it "stores processor to centralized hash storage" do
@@ -31,12 +31,5 @@ describe CarrierWave::Processor::Dsl do
31
31
  ::CarrierWave::Processor.processors.should_not have_key :another_processor
32
32
  end
33
33
 
34
- it "saves carrierwave processor arguments to cw_processors" do
35
- processor = carrierwave_processor :some do
36
- process :nya
37
- end
38
- processor.cw_processors.first.should == {:args => [:nya], :block => nil}
39
- end
40
-
41
34
 
42
35
  end
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,6 @@ require 'bundler/setup'
3
3
  require 'carrierwave'
4
4
  $:.unshift File.dirname(__FILE__) + '/../lib'
5
5
  require 'carrierwave/processor'
6
- require 'carrierwave_test_helper'
7
6
 
8
7
  RSpec.configure do |config|
9
8
  config.treat_symbols_as_metadata_keys_with_true_values = true
@@ -2,14 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  describe CarrierWave::Processor::UploaderDsl do
4
4
  before :each do
5
- CarrierWave::Processor.stub(:conditions_merge) do |*args|
6
- if args.empty?
7
- nil
8
- elsif (args.length == 1)
9
- args.first
10
- else
11
- args
12
- end
5
+ CarrierWave::Processor.stub(:merge_multiple_conditions) do |*args|
6
+ args
13
7
  end
14
8
  end
15
9
  before :each do
@@ -17,6 +11,14 @@ describe CarrierWave::Processor::UploaderDsl do
17
11
  Object.send(:remove_const, :FooUploader)
18
12
  end
19
13
  class FooUploader < CarrierWave::Uploader::Base
14
+ version :alalas do
15
+ def chacha
16
+ end
17
+ end
18
+
19
+ def test_me
20
+ "original"
21
+ end
20
22
  end
21
23
  end
22
24
 
@@ -36,9 +38,6 @@ describe CarrierWave::Processor::UploaderDsl do
36
38
  end
37
39
 
38
40
  it "merges processor condition with use_processor condition" do
39
- CarrierWave::Processor.stub(:conditions_merge) do |*args|
40
- args
41
- end
42
41
  combinations = [:method1, ->(u,o){true}].product([:method2, ->u,o{false}])
43
42
  combinations.each do |a, b|
44
43
  carrierwave_processor :some_processor do
@@ -68,11 +67,12 @@ describe CarrierWave::Processor::UploaderDsl do
68
67
  it "calls inner version method with :from_version key" do
69
68
  carrierwave_processor :some_processor do
70
69
  version :some_version do
71
- version :another_version
70
+ version :another_version do
71
+ end
72
72
  end
73
73
  end
74
74
 
75
- FooUploader.should_receive(:version).with(:some_version)
75
+ FooUploader.should_receive(:version).with(:some_version).and_call_original
76
76
  FooUploader.should_receive(:version).with(:another_version, :from_version => :some_version)
77
77
  FooUploader.send(:use_processor, :some_processor)
78
78
  end
@@ -141,4 +141,75 @@ describe CarrierWave::Processor::UploaderDsl do
141
141
  expect {FooUploader.send(:use_processor, :some_processor) }.to raise_error(CarrierWave::Processor::ProcessorNotFoundError)
142
142
  end
143
143
 
144
+ it "includes declared methods in each version" do
145
+ carrierwave_processor :some_proc do
146
+ def some_method
147
+ end
148
+
149
+ version :test do
150
+ def some_method2
151
+ end
152
+ end
153
+ end
154
+
155
+ FooUploader.send(:use_processor, :some_proc)
156
+ FooUploader.new.should respond_to :some_method
157
+ FooUploader.new.should_not respond_to :some_method2
158
+ FooUploader.versions[:test][:uploader].new.should respond_to :some_method2
159
+ end
160
+
161
+ it 'redefine default methods' do
162
+
163
+ carrierwave_processor :some_proc do
164
+ def test_me
165
+ "overriden"
166
+ end
167
+
168
+ version :test do
169
+ def test_me
170
+ "overriden internal"
171
+ end
172
+ end
173
+ end
174
+
175
+ FooUploader.send(:use_processor, :some_proc)
176
+ FooUploader.new.test_me.should == "overriden"
177
+ FooUploader.versions[:test][:uploader].new.test_me.should == "overriden internal"
178
+ end
179
+
180
+ it "doesnt include injector's method_missing into uploader" do
181
+ carrierwave_processor :some_proc do
182
+ def test_me
183
+ "overriden"
184
+ end
185
+
186
+ version :test do
187
+ def test_me
188
+ "overriden internal"
189
+ end
190
+ end
191
+ end
192
+
193
+ FooUploader.send(:use_processor, :some_proc)
194
+ FooUploader.new.should_not respond_to :method_missing
195
+ FooUploader.versions[:test][:uploader].new.should_not respond_to :method_missing
196
+ end
197
+
198
+ it "doesnt break carrrierwave uploader instance versions" do
199
+ carrierwave_processor :some_proc do
200
+ def test_me
201
+ "overriden"
202
+ end
203
+
204
+ version :test do
205
+ def test_me
206
+ "overriden internal"
207
+ end
208
+ end
209
+ end
210
+ FooUploader.send(:use_processor, :some_proc)
211
+ FooUploader.new.versions.map{|name, uploader| uploader.should be_kind_of FooUploader}
212
+ end
213
+
214
+
144
215
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave-processor
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Kostrov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-26 00:00:00.000000000 Z
11
+ date: 2014-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carrierwave
@@ -96,10 +96,10 @@ files:
96
96
  - carrierwave-processor.gemspec
97
97
  - lib/carrierwave/processor.rb
98
98
  - lib/carrierwave/processor/dsl.rb
99
- - lib/carrierwave/processor/node.rb
99
+ - lib/carrierwave/processor/injector.rb
100
100
  - lib/carrierwave/processor/uploader_dsl.rb
101
101
  - lib/carrierwave/processor/version.rb
102
- - spec/carrierwave_test_helper.rb
102
+ - spec/active_support_spec.rb
103
103
  - spec/dsl_spec.rb
104
104
  - spec/spec_helper.rb
105
105
  - spec/uploader_dsl_spec.rb
@@ -124,12 +124,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
124
  version: '0'
125
125
  requirements: []
126
126
  rubyforge_project:
127
- rubygems_version: 2.1.11
127
+ rubygems_version: 2.2.0
128
128
  signing_key:
129
129
  specification_version: 4
130
130
  summary: Carrierwave distinct processing declaration
131
131
  test_files:
132
- - spec/carrierwave_test_helper.rb
132
+ - spec/active_support_spec.rb
133
133
  - spec/dsl_spec.rb
134
134
  - spec/spec_helper.rb
135
135
  - spec/uploader_dsl_spec.rb
@@ -1,31 +0,0 @@
1
- require_relative './dsl'
2
-
3
- module CarrierWave
4
- module Processor
5
- class Node
6
- include Dsl
7
-
8
- attr_accessor :name, :options
9
- attr_accessor :processors
10
- attr_reader :cw_processors
11
-
12
- def initialize opts={}
13
- @cw_processors = []
14
- @processors = {}
15
- @options = opts
16
- end
17
-
18
- def process *args, &block
19
- processor = {:args => args, :block => block}
20
- @cw_processors << processor
21
- end
22
-
23
- def background *args, &block
24
- end
25
-
26
- alias_method :version, :carrierwave_processor
27
-
28
-
29
- end
30
- end
31
- end
@@ -1,76 +0,0 @@
1
- module CarrierWave
2
- module Test
3
- module MockStorage
4
- def mock_storage(kind)
5
- storage = mock("storage for #{kind} uploader")
6
- storage.stub!(:setup!)
7
- storage
8
- end
9
- end
10
-
11
- module MockFiles
12
- def stub_merb_tempfile(filename)
13
- raise "#{path} file does not exist" unless File.exist?(file_path(filename))
14
-
15
- t = Tempfile.new(filename)
16
- FileUtils.copy_file(file_path(filename), t.path)
17
-
18
- return t
19
- end
20
-
21
- def stub_tempfile(filename, mime_type=nil, fake_name=nil)
22
- raise "#{path} file does not exist" unless File.exist?(file_path(filename))
23
-
24
- t = Tempfile.new(filename)
25
- FileUtils.copy_file(file_path(filename), t.path)
26
-
27
- # This is stupid, but for some reason rspec won't play nice...
28
- eval <<-EOF
29
- def t.original_filename; '#{fake_name || filename}'; end
30
- def t.content_type; '#{mime_type}'; end
31
- def t.local_path; path; end
32
- EOF
33
-
34
- return t
35
- end
36
-
37
- def stub_stringio(filename, mime_type=nil, fake_name=nil)
38
- if filename
39
- t = StringIO.new( IO.read( file_path( filename ) ) )
40
- else
41
- t = StringIO.new
42
- end
43
- t.stub!(:local_path => "",
44
- :original_filename => filename || fake_name,
45
- :content_type => mime_type)
46
- return t
47
- end
48
-
49
- def stub_file(filename, mime_type=nil, fake_name=nil)
50
- f = File.open(file_path(filename))
51
- return f
52
- end
53
- end
54
-
55
- module I18nHelpers
56
- def change_locale_and_store_translations(locale, translations, &block)
57
- current_locale = I18n.locale
58
- begin
59
- I18n.backend.store_translations locale, translations
60
- I18n.locale = locale
61
- yield
62
- ensure
63
- I18n.reload!
64
- I18n.locale = current_locale
65
- end
66
- end
67
- end
68
-
69
- module ManipulationHelpers
70
- def color_of_pixel(path, x, y)
71
- image = ::MiniMagick::Image.open(path)
72
- color = image.run_command("convert", "#{image.path}[1x1+#{x}+#{y}]", "-depth", "8", "txt:").split("\n")[1]
73
- end
74
- end
75
- end
76
- end