sexy_validations 0.2.2 → 0.3.0

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.0
@@ -1,6 +1,11 @@
1
1
  require 'sexy_validations/errors'
2
2
 
3
3
  module SexyValidations
4
+ def self.load_validator(name)
5
+ require "sexy_validations/validators/#{name}"
6
+ "SexyValidations::Validators::#{name.to_s.capitalize}".constantize
7
+ end
8
+
4
9
  def self.included(klass)
5
10
  klass.instance_eval do
6
11
  class_inheritable_array :validations
@@ -30,10 +35,9 @@ module SexyValidations
30
35
 
31
36
  module ClassMethods
32
37
  def load_validator(name)
33
- require "sexy_validations/validators/#{name}"
34
- "SexyValidations::Validators::#{name.to_s.capitalize}".constantize
38
+ SexyValidations.load_validator(name)
35
39
  end
36
-
40
+
37
41
  def validates(attribute = nil, validations = nil, &block)
38
42
  if validations
39
43
  condition = nil
@@ -74,7 +78,7 @@ module SexyValidations
74
78
  def validate!
75
79
  errors.clear
76
80
  validations.each do |validation|
77
- valid = case
81
+ case
78
82
  when validation[:validator]
79
83
  if validation[:condition]
80
84
  next unless validation[:condition].call(self)
@@ -9,6 +9,17 @@ module SexyValidations
9
9
  end
10
10
  end
11
11
 
12
+ def self.file_from_value(value)
13
+ case value.class.name
14
+ when "File", "Tempfile"
15
+ value
16
+ when "Sequel::Plugins::Paperclip::Attachment"
17
+ value.queued_file
18
+ else
19
+ raise ArgumentError, "#{value} is not a valid input"
20
+ end
21
+ end
22
+
12
23
  def self.validate(model, attribute, value, options)
13
24
  return unless value
14
25
 
@@ -18,18 +29,17 @@ module SexyValidations
18
29
  }
19
30
  end
20
31
 
21
- unless value.is_a?(File) || value.is_a?(Tempfile)
22
- raise ArgumentError, "#{value} is not a File"
23
- end
32
+ file = file_from_value(value)
33
+ return unless file
24
34
 
25
35
  if options[:size]
26
36
  min = options[:size].min
27
- if value.size < min
37
+ if file.size < min
28
38
  model.errors.add(attribute, "zu klein (mindestes #{humanized_size(min)})")
29
39
  end
30
40
 
31
41
  max = options[:size].max
32
- if value.size > max
42
+ if file.size > max
33
43
  model.errors.add(attribute, "zu groß (maximal #{humanized_size(max)})")
34
44
  end
35
45
  end
@@ -11,13 +11,12 @@ module SexyValidations
11
11
  }
12
12
  end
13
13
 
14
- unless value.is_a?(File) || value.is_a?(Tempfile)
15
- raise ArgumentError, "#{value} is not a File"
16
- end
14
+ file = SexyValidations.load_validator(:file).file_from_value(value)
15
+ return unless file
17
16
 
18
17
  if options[:geometry]
19
18
  geo1 = Sequel::Plugins::Paperclip::Processors::Image::Geometry.from_s(options[:geometry])
20
- geo2 = Sequel::Plugins::Paperclip::Processors::Image::Geometry.from_file(value)
19
+ geo2 = Sequel::Plugins::Paperclip::Processors::Image::Geometry.from_file(file)
21
20
  if geo2
22
21
  if geo2.width < geo1.width
23
22
  model.errors.add(attribute, "zu klein (weniger als %d Pixel breit)"%[geo1.width])
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sexy_validations}
8
- s.version = "0.2.2"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Corin Langosch"]
12
- s.date = %q{2010-09-17}
12
+ s.date = %q{2010-09-21}
13
13
  s.description = %q{Module which provides sexy validations for models.}
14
14
  s.email = %q{info@netskin.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 2
9
- version: 0.2.2
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Corin Langosch
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-17 00:00:00 +01:00
17
+ date: 2010-09-21 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency