carrierwave-size-validator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in carrierwave-size-validator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Anton Rogov
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,31 @@
1
+ # Carrierwave size validator
2
+
3
+ Gemified file size validator based on https://gist.github.com/chrisbloom7/1009861.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'carrierwave-size-validator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install carrierwave-size-validator
18
+
19
+ ## Usage
20
+
21
+ ``` ruby
22
+ validates :image, file_size: { maximum: 5.megabytes.to_i }
23
+ ```
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'carrierwave-size-validator/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "carrierwave-size-validator"
8
+ gem.version = Carrierwave::Size::Validator::VERSION
9
+ gem.authors = ["Anton Rogov"]
10
+ gem.email = ["antonrogov@me.com"]
11
+ gem.description = %q{Gemified file size validator based on https://gist.github.com/chrisbloom7/1009861}
12
+ gem.summary = %q{Gemified file size validator based on https://gist.github.com/chrisbloom7/1009861}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,75 @@
1
+ # Based on: https://gist.github.com/795665
2
+
3
+ module ActiveModel
4
+ module Validations
5
+ class FileSizeValidator < EachValidator
6
+ MESSAGES = { :is => :wrong_size, :minimum => :size_too_small, :maximum => :size_too_big }.freeze
7
+ CHECKS = { :is => :==, :minimum => :>=, :maximum => :<= }.freeze
8
+
9
+ DEFAULT_TOKENIZER = lambda { |value| value.split(//) }
10
+ RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :tokenizer, :too_short, :too_long]
11
+
12
+ def initialize(options)
13
+ if range = (options.delete(:in) || options.delete(:within))
14
+ raise ArgumentError, ":in and :within must be a Range" unless range.is_a?(Range)
15
+ options[:minimum], options[:maximum] = range.begin, range.end
16
+ options[:maximum] -= 1 if range.exclude_end?
17
+ end
18
+
19
+ super
20
+ end
21
+
22
+ def check_validity!
23
+ keys = CHECKS.keys & options.keys
24
+
25
+ if keys.empty?
26
+ raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.'
27
+ end
28
+
29
+ keys.each do |key|
30
+ value = options[key]
31
+
32
+ unless value.is_a?(Integer) && value >= 0
33
+ raise ArgumentError, ":#{key} must be a nonnegative Integer"
34
+ end
35
+ end
36
+ end
37
+
38
+ def validate_each(record, attribute, value)
39
+ unless value.kind_of? CarrierWave::Uploader::Base
40
+ raise ArgumentError, "A CarrierWave::Uploader::Base object was expected"
41
+ end
42
+
43
+ if value.kind_of?(String)
44
+ value = (options[:tokenizer] || DEFAULT_TOKENIZER).call(value)
45
+ end
46
+
47
+ CHECKS.each do |key, validity_check|
48
+ next unless check_value = options[key]
49
+
50
+ value ||= [] if key == :maximum
51
+
52
+ value_size = value.size
53
+ next if value_size.send(validity_check, check_value)
54
+
55
+ errors_options = options.except(*RESERVED_OPTIONS)
56
+ errors_options[:file_size] = help.number_to_human_size check_value
57
+
58
+ default_message = options[MESSAGES[key]]
59
+ errors_options[:message] ||= default_message if default_message
60
+
61
+ record.errors.add(attribute, MESSAGES[key], errors_options)
62
+ end
63
+ end
64
+
65
+ def help
66
+ Helper.instance
67
+ end
68
+
69
+ class Helper
70
+ include Singleton
71
+ include ActionView::Helpers::NumberHelper
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,2 @@
1
+ require 'carrierwave-size-validator/version'
2
+ require 'active_model/validations/file_size_validator'
@@ -0,0 +1,7 @@
1
+ module Carrierwave
2
+ module Size
3
+ module Validator
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-size-validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anton Rogov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Gemified file size validator based on https://gist.github.com/chrisbloom7/1009861
15
+ email:
16
+ - antonrogov@me.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - carrierwave-size-validator.gemspec
27
+ - lib/active_model/validations/file_size_validator.rb
28
+ - lib/carrierwave-size-validator.rb
29
+ - lib/carrierwave-size-validator/version.rb
30
+ homepage: ''
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.23
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Gemified file size validator based on https://gist.github.com/chrisbloom7/1009861
54
+ test_files: []