oscillator 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/oscillator.rb +6 -0
- data/lib/oscillator/canonical_uploader_path.rb +9 -0
- data/lib/oscillator/file_size_validator.rb +68 -0
- data/lib/oscillator/version.rb +3 -0
- metadata +55 -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/oscillator.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,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: oscillator
|
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/oscillator/canonical_uploader_path.rb
|
24
|
+
- lib/oscillator/file_size_validator.rb
|
25
|
+
- lib/oscillator/version.rb
|
26
|
+
- lib/oscillator.rb
|
27
|
+
- Rakefile
|
28
|
+
- README.md
|
29
|
+
- LICENSE
|
30
|
+
homepage: https://github.com/chirrpy/oscillator
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options:
|
34
|
+
- --charset = UTF-8
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project: oscillator
|
51
|
+
rubygems_version: 1.8.24
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Shared CarrierWave and Validations and Configurations
|
55
|
+
test_files: []
|