i_am_valid 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README.md +4 -0
- data/Rakefile +1 -0
- data/lib/i_am_valid.rb +6 -0
- data/lib/i_am_valid/file_size_validator.rb +68 -0
- data/lib/i_am_valid/version.rb +3 -0
- metadata +54 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jeff Felchner
|
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
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/i_am_valid.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# lib/file_size_validator.rb
|
2
|
+
# Based on: https://gist.github.com/795665
|
3
|
+
|
4
|
+
class FileSizeValidator < ActiveModel::EachValidator
|
5
|
+
MESSAGES = { :is => :wrong_size, :minimum => :size_too_small, :maximum => :size_too_big }.freeze
|
6
|
+
CHECKS = { :is => :==, :minimum => :>=, :maximum => :<= }.freeze
|
7
|
+
|
8
|
+
DEFAULT_TOKENIZER = lambda { |value| value.split(//) }
|
9
|
+
RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :tokenizer, :too_short, :too_long]
|
10
|
+
|
11
|
+
def initialize(options)
|
12
|
+
if range = (options.delete(:in) || options.delete(:within))
|
13
|
+
raise ArgumentError, ":in and :within must be a Range" unless range.is_a?(Range)
|
14
|
+
options[:minimum], options[:maximum] = range.begin, range.end
|
15
|
+
options[:maximum] -= 1 if range.exclude_end?
|
16
|
+
end
|
17
|
+
|
18
|
+
super
|
19
|
+
end
|
20
|
+
|
21
|
+
def check_validity!
|
22
|
+
keys = CHECKS.keys & options.keys
|
23
|
+
|
24
|
+
if keys.empty?
|
25
|
+
raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.'
|
26
|
+
end
|
27
|
+
|
28
|
+
keys.each do |key|
|
29
|
+
value = options[key]
|
30
|
+
|
31
|
+
unless value.is_a?(Integer) && value >= 0
|
32
|
+
raise ArgumentError, ":#{key} must be a nonnegative Integer"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def validate_each(record, attribute, value)
|
38
|
+
raise(ArgumentError, "A CarrierWave::Uploader::Base object was expected") unless value.kind_of? CarrierWave::Uploader::Base
|
39
|
+
|
40
|
+
value = (options[:tokenizer] || DEFAULT_TOKENIZER).call(value) if value.kind_of?(String)
|
41
|
+
|
42
|
+
CHECKS.each do |key, validity_check|
|
43
|
+
next unless check_value = options[key]
|
44
|
+
|
45
|
+
value ||= [] if key == :maximum
|
46
|
+
|
47
|
+
value_size = value.size
|
48
|
+
next if value_size.send(validity_check, check_value)
|
49
|
+
|
50
|
+
errors_options = options.except(*RESERVED_OPTIONS)
|
51
|
+
errors_options[:file_size] = help.number_to_human_size check_value
|
52
|
+
|
53
|
+
default_message = options[MESSAGES[key]]
|
54
|
+
errors_options[:message] ||= default_message if default_message
|
55
|
+
|
56
|
+
record.errors.add(attribute, MESSAGES[key], errors_options)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def help
|
61
|
+
Helper.instance
|
62
|
+
end
|
63
|
+
|
64
|
+
class Helper
|
65
|
+
include Singleton
|
66
|
+
include ActionView::Helpers::NumberHelper
|
67
|
+
end
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: i_am_valid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- jfelchner
|
9
|
+
- m5rk
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-10-02 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: ''
|
16
|
+
email: support@chirrpy.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files:
|
20
|
+
- README.md
|
21
|
+
- LICENSE
|
22
|
+
files:
|
23
|
+
- lib/i_am_valid/file_size_validator.rb
|
24
|
+
- lib/i_am_valid/version.rb
|
25
|
+
- lib/i_am_valid.rb
|
26
|
+
- Rakefile
|
27
|
+
- README.md
|
28
|
+
- LICENSE
|
29
|
+
homepage: https://github.com/chirrpy/i_am_valid
|
30
|
+
licenses: []
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options:
|
33
|
+
- --charset = UTF-8
|
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: i_am_valid
|
50
|
+
rubygems_version: 1.8.24
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Custom ActiveModel::Validations to Make Your Life Easier
|
54
|
+
test_files: []
|