ciuchcia 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/ciuchcia/date.rb +1 -1
- data/lib/ciuchcia/number_in_words.rb +4 -3
- data/lib/ciuchcia/validations.rb +31 -16
- data/lib/ciuchcia.rb +1 -1
- data/spec/ciuchcia_spec.rb +25 -9
- metadata +2 -2
data/Rakefile
CHANGED
data/lib/ciuchcia/date.rb
CHANGED
@@ -7,14 +7,15 @@
|
|
7
7
|
def number_in_words(n,ending=nil)
|
8
8
|
# It's probably the worst code in ruby I've ever written
|
9
9
|
# It seems to work, but it definitely should not ;)
|
10
|
-
|
10
|
+
n = n.to_i
|
11
|
+
return '' if n.nil? or n == 0
|
11
12
|
sc = [''] + %w{jeden dwa trzy cztery pięć sześć siedem osiem dziewięć}
|
12
13
|
sn = %w{dziesięć jedenaście dwanaście trzynaście czternaście piętnaście szesnaście siedemnaście osiemnaście dziewiętnaście}
|
13
14
|
sd = ['',''] + %w{dwadzieścia trzydzieści czterdzieści pięćdziesiąt sześćdziesiąt siedemdziesiąt osiemdziesiąt dziewiędziesiąt sto}
|
14
15
|
ss = [''] + %w{sto dwieście trzysta czterysta pięćset sześćset siedemset osiemset dziewięćset}
|
15
|
-
b = (ending || ['','','']),%w{tysiąc tysiące tysięcy},%w{milion miliony milionów},%w{miliard miliardy
|
16
|
+
b = (ending || ['','','']),%w{tysiąc tysiące tysięcy},%w{milion miliony milionów},%w{miliard miliardy miliardów},%w{bilion biliony bilionów}
|
16
17
|
p = n.to_s.size
|
17
|
-
return
|
18
|
+
return n if p > 15
|
18
19
|
d,dn = n.to_s[0,(p%3 == 0 ? 3 : p%3)], n.to_s[(p%3 == 0 ? 3 : p%3)..-1]
|
19
20
|
return "#{d.to_i==0 ? '' : b[((p-1)/3.0).floor][0]} #{number_in_words(dn,ending)}".strip if (d.to_i == 1 or d.to_i == 0 ) and n != 1
|
20
21
|
r = ''
|
data/lib/ciuchcia/validations.rb
CHANGED
@@ -37,6 +37,20 @@ module Ciuchcia
|
|
37
37
|
true
|
38
38
|
end
|
39
39
|
|
40
|
+
def self.valid_pesel?(pesel)
|
41
|
+
# validates presence
|
42
|
+
return false if pesel.nil? or (pesel.empty? rescue true)
|
43
|
+
|
44
|
+
# clean up and check format (should be 11 digits)
|
45
|
+
pesel.gsub!(/\ |\-|\../,'')
|
46
|
+
return false unless pesel.match(/^\d{11}$/)
|
47
|
+
|
48
|
+
weights = [1,3,7,9,1,3,7,9,1,3]
|
49
|
+
digits = pesel.split('').map {|i| i.to_i }
|
50
|
+
return false unless digits[0..-2].inject(digits[-1]) { |sum,x| sum + x*weights.shift } % 10 == 0
|
51
|
+
true
|
52
|
+
end
|
53
|
+
|
40
54
|
end
|
41
55
|
end
|
42
56
|
|
@@ -46,31 +60,32 @@ module ActiveRecord
|
|
46
60
|
|
47
61
|
def validates_nip(*attr_names)
|
48
62
|
configuration = { :message => 'Niepoprawny numer NIP', :on => :save }
|
49
|
-
configuration.update(attr_names.extract_options!)
|
50
|
-
|
51
|
-
attr_accessor(*(attr_names.map { |n| "#{n}_confirmation" }))
|
63
|
+
configuration.update(attr_names.extract_options!)
|
52
64
|
|
53
65
|
validates_each(attr_names, configuration) do |record, attr_name, value|
|
54
|
-
|
55
|
-
record.errors.add(attr_name, configuration[:message]) unless Ciuchcia::Validations.valid_nip? value
|
66
|
+
record.errors.add(attr_name, configuration[:message]) unless Ciuchcia::Validations.valid_nip? record.send("#{attr_name}")
|
56
67
|
end
|
57
68
|
end
|
58
69
|
|
59
70
|
def validates_regon(*attr_names)
|
60
71
|
configuration = { :message => 'Niepoprawny numer REGON', :on => :save }
|
61
72
|
configuration.update(attr_names.extract_options!)
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
validates_each(attr_names, configuration) do |record, attr_name, value|
|
66
|
-
value = record.send("#{attr_name}")
|
67
|
-
record.errors.add(attr_name, configuration[:message]) unless Ciuchcia::Validationsvalid_regon? value
|
73
|
+
|
74
|
+
validates_each(attr_names, configuration) do |record, attr_name, value|
|
75
|
+
record.errors.add(attr_name, configuration[:message]) unless Ciuchcia::Validations.valid_regon? record.send("#{attr_name}")
|
68
76
|
end
|
69
|
-
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def validates_pesel(*attr_names)
|
80
|
+
configuration = { :message => 'Niepoprawny numer PESEL', :on => :save }
|
81
|
+
configuration.update(attr_names.extract_options!)
|
82
|
+
|
83
|
+
validates_each(attr_names, configuration) do |record, attr_name, value|
|
84
|
+
record.errors.add(attr_name, configuration[:message]) unless Ciuchcia::Validations.valid_pesel? record.send("#{attr_name}")
|
85
|
+
end
|
86
|
+
end
|
70
87
|
|
71
|
-
|
72
|
-
end
|
73
|
-
|
74
|
-
|
88
|
+
|
89
|
+
end
|
75
90
|
end
|
76
91
|
end
|
data/lib/ciuchcia.rb
CHANGED
data/spec/ciuchcia_spec.rb
CHANGED
@@ -4,10 +4,13 @@ require File.join(File.dirname(__FILE__), %w[spec_helper])
|
|
4
4
|
|
5
5
|
describe Ciuchcia do
|
6
6
|
it "should correctly tralnlate numbers to words" do
|
7
|
+
number_in_words(1).should eql("jeden")
|
8
|
+
number_in_words(1000).should eql("tysiąc")
|
7
9
|
number_in_words(123).should eql("sto dwadzieścia trzy")
|
8
10
|
number_in_words(1047).should eql("tysiąc czterdzieści siedem")
|
9
11
|
number_in_words(12).should eql("dwanaście")
|
10
12
|
number_in_words(666).should eql("sześćset sześćdziesiąt sześć")
|
13
|
+
number_in_words(123456789).should eql("sto dwadzieścia trzy miliony czterysta pięćdziesiąt sześć tysięcy siedemset osiemdziesiąt dziewięć")
|
11
14
|
end
|
12
15
|
|
13
16
|
it "should know how to count mouney" do
|
@@ -17,20 +20,33 @@ describe Ciuchcia do
|
|
17
20
|
end
|
18
21
|
|
19
22
|
it "should correctly validates NIP number" do
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
ActiveRecord::Base.valid_nip?('525-21-85-036').should == true
|
23
|
+
Ciuchcia::Validations.valid_nip?('779-21-25-257').should == true
|
24
|
+
Ciuchcia::Validations.valid_nip?('779-21-25-254').should == false
|
25
|
+
Ciuchcia::Validations.valid_nip?('blablabla').should == false
|
26
|
+
Ciuchcia::Validations.valid_nip?('779-21-25-25423').should == false
|
27
|
+
Ciuchcia::Validations.valid_nip?('7792125223').should == false
|
28
|
+
Ciuchcia::Validations.valid_nip?('525-21-85-036').should == true
|
27
29
|
end
|
28
30
|
|
29
31
|
it "should correctly validates REGON number" do
|
30
|
-
|
31
|
-
|
32
|
+
Ciuchcia::Validations.valid_regon?('016385358').should == true
|
33
|
+
Ciuchcia::Validations.valid_regon?('016385354').should == false
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should correctly validates PESEL number" do
|
37
|
+
Ciuchcia::Validations.valid_pesel?('85120701576').should == true
|
38
|
+
Ciuchcia::Validations.valid_pesel?('85120701575').should == false
|
39
|
+
Ciuchcia::Validations.valid_pesel?('82904310630').should == false
|
32
40
|
end
|
33
41
|
|
34
42
|
|
35
43
|
end
|
36
44
|
|
45
|
+
describe ActiveRecord::Base do
|
46
|
+
it "should respond to validations" do
|
47
|
+
ActiveRecord::Base.should respond_to :validates_regon
|
48
|
+
ActiveRecord::Base.should respond_to :validates_nip
|
49
|
+
ActiveRecord::Base.should respond_to :validates_pesel
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ciuchcia
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Kacper Cie\xC5\x9Bla"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-07-
|
12
|
+
date: 2008-07-27 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|