platanus 0.0.21 → 0.0.22

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,33 @@
1
+ # validators/email.rb : Email validator for active record
2
+ #
3
+ # Copyright April 2012, Ignacio Baixas +mailto:ignacio@platan.us+.
4
+
5
+ require 'mail'
6
+
7
+ ## Adds the "email" validation to active record
8
+ #
9
+ # Usage:
10
+ #
11
+ # validates :email_col, email: true
12
+ #
13
+ class EmailValidator < ActiveModel::EachValidator
14
+ def validate_each(_record, _attribute, _value)
15
+ return if _value.nil?
16
+ begin
17
+ mail = Mail::Address.new(_value)
18
+ # We must check that value contains a domain and that value is an email address
19
+ res = mail.domain && mail.address == _value
20
+ tree = mail.__send__(:tree)
21
+ # We need to dig into treetop
22
+ # A valid domain must have dot_atom_text elements size > 1
23
+ # user@localhost is excluded
24
+ # treetop must respond to domain
25
+ # We exclude valid email values like <user@localhost.com>
26
+ # Hence we use mail.__send__(tree).domain
27
+ res &&= (tree.domain.dot_atom_text.elements.size > 1)
28
+ rescue Exception => e
29
+ res = false
30
+ end
31
+ _record.errors[_attribute] << (options[:message] || "is invalid") unless res
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ # validators/rut.rb : Chilean rut validator for active record
2
+ #
3
+ # Copyright April 2012, Ignacio Baixas +mailto:ignacio@platan.us+.
4
+
5
+ ## Adds the "rut" validation to active record models
6
+ #
7
+ # Usage:
8
+ #
9
+ # validates :rut_col, rut: true
10
+ #
11
+ class RutValidator < ActiveModel::EachValidator
12
+
13
+ def validate_each(_record, _attribute, _value)
14
+ return if _value.nil?
15
+ begin
16
+ t = _value.gsub(/[^0-9K]/i,'').[0...-1].to_i
17
+ m, s = 0, 1
18
+ while t > 0
19
+ s = (s + t%10 * (9 - m%6)) % 11
20
+ m += 1
21
+ t /= 10
22
+ end
23
+ v = if s > 0 then (s-1).to_s else 'K' end
24
+ r = (v == _value.last.upcase)
25
+ rescue Exception => e
26
+ r = false
27
+ end
28
+ _record.errors[_attribute] << (options[:message] || "is invalid") unless r
29
+ end
30
+
31
+ end
@@ -1,3 +1,3 @@
1
1
  module Platanus
2
- VERSION = "0.0.21" # 0.1 will come with tests!
2
+ VERSION = "0.0.22" # 0.1 will come with tests!
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: platanus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.21
4
+ version: 0.0.22
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-08 00:00:00.000000000 Z
12
+ date: 2012-10-10 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Platan.us utility gem
15
15
  email:
@@ -37,6 +37,8 @@ files:
37
37
  - lib/platanus/templates/prawn.rb
38
38
  - lib/platanus/templates/spreadsheet.rb
39
39
  - lib/platanus/traceable.rb
40
+ - lib/platanus/validators/email.rb
41
+ - lib/platanus/validators/rut.rb
40
42
  - lib/platanus/version.rb
41
43
  - platanus.gemspec
42
44
  - spec/canned_spec.rb