carrierwave-processor 0.0.1.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f9b477ba217d1235ada6e864ecbeb8148df8ee6d
4
+ data.tar.gz: 6beeca6c3a3541c60c2dc3ebbb588bf046fe139b
5
+ SHA512:
6
+ metadata.gz: 4de4ca8e40e2dfa36c271d7e2989420ef54ffd3cf13ca5d47d8ac32d1a03babbe95a43a01d355bfa0292cc9bd597c01d21033016cb0787fc01be8149627994e6
7
+ data.tar.gz: fdee6a44fba1ab78fdd8eb683505276eff764a54e24cb1f77260c9640391c50cccb978a65c2bacffa7f7307c409abb6bec6d0e6d025340ddfa945ca142e71190
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in carrierwave-processing.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alexander Kostrov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Carrierwave::Processing
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'carrierwave-processing'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install carrierwave-processing
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'carrierwave/processor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "carrierwave-processor"
8
+ spec.version = CarrierWave::Processor::VERSION
9
+ spec.authors = ["Alexander Kostrov"]
10
+ spec.email = ["bombazook@gmail.com"]
11
+ spec.description = "Simple dsl cover for carrierwave distinct processing declaration"
12
+ spec.summary = "Carrierwave distinct processing declaration"
13
+ spec.homepage = "http://github.com/bombazook/carrierwave-processor"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib", "lib/carrierwave"]
20
+
21
+ spec.add_dependency 'carrierwave'
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "factory_girl"
26
+ end
@@ -0,0 +1,31 @@
1
+ module CarrierWave
2
+ module Processor
3
+ module Dsl
4
+ def carrierwave_processor *args, &block
5
+ options = args.extract_options!
6
+ name = args.first
7
+ 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[name.to_sym]
25
+ else
26
+ CarrierWave::Processor.processors[name.to_sym]
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
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
@@ -0,0 +1,58 @@
1
+ require_relative 'dsl'
2
+
3
+ module CarrierWave
4
+ module Processor
5
+ module UploaderDsl
6
+
7
+ def use_processor *args
8
+ options = args.extract_options!
9
+ conditions = [options.delete(:conditions) || options[:if]].flatten.compact
10
+ 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
16
+ else
17
+ raise ProcessorNotFoundError, processor
18
+ end
19
+ end
20
+ end
21
+
22
+ private
23
+
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[:conditions] || []
36
+ processor.processors.each do |name, version|
37
+ new_conditions = (conditions + [version.options[:if]]).compact
38
+ condition = ::CarrierWave::Processor.conditions_merge(*new_conditions) unless new_conditions.empty?
39
+ version_options = version.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 version
45
+ end
46
+ else
47
+ version name, version_options do
48
+ load_cw_processors version
49
+ end
50
+ end
51
+ next_level_options = {:from_version => version.name}
52
+ next_level_options.merge!(:conditions => new_conditions) unless conditions.empty?
53
+ load_cw_versions version, next_level_options
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+ module CarrierWave
2
+ module Processor
3
+ VERSION = "0.0.1.pre"
4
+ end
5
+ end
@@ -0,0 +1,42 @@
1
+ require 'active_support'
2
+ require 'carrierwave'
3
+ require "carrierwave/processor/version"
4
+ require 'carrierwave/processor/node'
5
+ require 'carrierwave/processor/uploader_dsl'
6
+
7
+ module CarrierWave
8
+ module Processor
9
+
10
+ class ProcessorNotFoundError < ::StandardError
11
+ end
12
+
13
+ class << self
14
+ attr_accessor :processors
15
+ end
16
+
17
+ def self.conditions_merge *args
18
+ args.compact!
19
+ return nil if args.empty?
20
+ lambda do |uploader, options|
21
+ args.inject(true) do |accum, condition|
22
+ break false unless accum
23
+ condition_result = if condition.respond_to?(:call)
24
+ accum && condition.call(self, options)
25
+ else
26
+ accum && uploader.send(condition, options[:file])
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ def self.arguments_merge *args
33
+ args.inject({}) do |hash, arg|
34
+ arg = { arg => [] } unless arg.is_a?(Hash)
35
+ hash.merge!(arg)
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ Object.send :include, CarrierWave::Processor::Dsl
42
+ CarrierWave::Uploader::Base.extend CarrierWave::Processor::UploaderDsl
@@ -0,0 +1,76 @@
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
data/spec/dsl_spec.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe CarrierWave::Processor::Dsl do
4
+
5
+ before :each do
6
+ ::CarrierWave::Processor.processors = {}
7
+ end
8
+
9
+ it "returns processor node on carrierwave_processor call" do
10
+ processor = carrierwave_processor(:some_processor){}
11
+ processor.should be_kind_of CarrierWave::Processor::Node
12
+ end
13
+
14
+ it "stores processor to centralized hash storage" do
15
+ carrierwave_processor(:another_processor){}
16
+ ::CarrierWave::Processor.processors.should have_key :another_processor
17
+ end
18
+
19
+ it "saves new processor to storage if same key given" do
20
+ first_processor = carrierwave_processor(:processor){}
21
+ ::CarrierWave::Processor.processors[:processor].should be_equal first_processor
22
+ second_processor = carrierwave_processor(:processor){}
23
+ ::CarrierWave::Processor.processors[:processor].should_not be_equal first_processor
24
+ ::CarrierWave::Processor.processors[:processor].should be_equal second_processor
25
+ end
26
+
27
+ it "does not save inner processor to global storage but save it to local storage" do
28
+ outer_processor = carrierwave_processor(:processor) do
29
+ carrierwave_processor(:another_processor){}
30
+ end
31
+ ::CarrierWave::Processor.processors.should_not have_key :another_processor
32
+ end
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
+
42
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'carrierwave'
4
+ $:.unshift File.dirname(__FILE__) + '/../lib'
5
+ require 'carrierwave/processor'
6
+ require 'carrierwave_test_helper'
7
+
8
+ RSpec.configure do |config|
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.run_all_when_everything_filtered = true
11
+ config.filter_run :focus
12
+ config.order = 'random'
13
+ end
14
+
15
+
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe CarrierWave::Processor::UploaderDsl do
4
+ before :each do
5
+ if Object.constants.include?(:FooUploader)
6
+ Object.send(:remove_const, :FooUploader)
7
+ end
8
+ class FooUploader < CarrierWave::Uploader::Base
9
+ end
10
+ end
11
+
12
+ it "CarrierWave Uploader should respond to use_processor" do
13
+ CarrierWave::Uploader::Base.should respond_to :use_processor
14
+ FooUploader.should respond_to :use_processor
15
+ end
16
+
17
+ it "calls each of outer processors in processor" do
18
+ carrierwave_processor :some_processor do
19
+ process :resize_to_fit => [800, 800]
20
+ process :something
21
+ end
22
+ FooUploader.should_receive(:process).with(:resize_to_fit => [800, 800])
23
+ FooUploader.should_receive(:process).with(:something => [])
24
+ FooUploader.send(:use_processor, :some_processor)
25
+ end
26
+
27
+ it "merges processor condition with use_processor condition" do
28
+ CarrierWave::Processor.stub(:conditions_merge) do |*args|
29
+ args
30
+ end
31
+ combinations = [:method1, ->(u,o){true}].product([:method2, ->u,o{false}])
32
+ combinations.each do |a, b|
33
+ carrierwave_processor :some_processor do
34
+ process :test, :if => a
35
+ end
36
+ FooUploader.should_receive(:process).with(:test => [], :if => CarrierWave::Processor.conditions_merge(b,a))
37
+ FooUploader.send(:use_processor, :some_processor, :if => b)
38
+ end
39
+ end
40
+
41
+ it "doesnt raise an exception when version block not given" do
42
+ expect do
43
+ carrierwave_processor :some_processor do
44
+ version :some_version
45
+ end
46
+ end.not_to raise_exception
47
+ end
48
+
49
+ it "calls version method for version declaration" do
50
+ carrierwave_processor :some_processor do
51
+ version :some_version
52
+ end
53
+ FooUploader.should_receive(:version)
54
+ FooUploader.send(:use_processor, :some_processor)
55
+ end
56
+
57
+ it "calls inner version method with :from_version key" do
58
+ carrierwave_processor :some_processor do
59
+ version :some_version do
60
+ version :another_version
61
+ end
62
+ end
63
+
64
+ FooUploader.should_receive(:version).with(:some_version)
65
+ FooUploader.should_receive(:version).with(:another_version, :from_version => :some_version)
66
+ FooUploader.send(:use_processor, :some_processor)
67
+ end
68
+
69
+ it "calls inner version processors without if option merge" do
70
+ carrierwave_processor :some_processor do
71
+ version :some_version do
72
+ process :another_version, :if => :abrakadabra
73
+ end
74
+ end
75
+ FooUploader.should_receive(:version).with(:some_version)
76
+ end
77
+
78
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-processor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Kostrov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: carrierwave
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: factory_girl
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Simple dsl cover for carrierwave distinct processing declaration
84
+ email:
85
+ - bombazook@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - carrierwave-processor.gemspec
97
+ - lib/carrierwave/processor.rb
98
+ - lib/carrierwave/processor/dsl.rb
99
+ - lib/carrierwave/processor/node.rb
100
+ - lib/carrierwave/processor/uploader_dsl.rb
101
+ - lib/carrierwave/processor/version.rb
102
+ - spec/carrierwave_test_helper.rb
103
+ - spec/dsl_spec.rb
104
+ - spec/spec_helper.rb
105
+ - spec/uploader_dsl_spec.rb
106
+ homepage: http://github.com/bombazook/carrierwave-processor
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ - lib/carrierwave
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">"
123
+ - !ruby/object:Gem::Version
124
+ version: 1.3.1
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.1.11
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Carrierwave distinct processing declaration
131
+ test_files:
132
+ - spec/carrierwave_test_helper.rb
133
+ - spec/dsl_spec.rb
134
+ - spec/spec_helper.rb
135
+ - spec/uploader_dsl_spec.rb