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 CHANGED
@@ -10,7 +10,7 @@ require 'ciuchcia'
10
10
  task :default => 'spec:run'
11
11
 
12
12
  PROJ.name = 'ciuchcia'
13
- PROJ.version = '0.0.1'
13
+ PROJ.version = Ciuchcia::VERSION
14
14
  PROJ.authors = 'Kacper Cieśla'
15
15
  PROJ.email = 'kacper.ciesla@gmail.com'
16
16
  PROJ.url = 'FIXME (project homepage)'
data/lib/ciuchcia/date.rb CHANGED
@@ -1,4 +1,4 @@
1
- if Object.const_defined? 'Date'
1
+ if Object.const_defined? 'Date' and Ciuchcia.const_defined? 'PATCH_DATE'
2
2
 
3
3
  class CiuchciaDate < Date
4
4
  MONTHNAMES = [nil] + %w{Styczeń Luty Marzec Kwiecień Maj Czerwiec Lipiec Sierpień Wrzesień Październik Listopad Grudzień}
@@ -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
- return '' if n == 0
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 miliarðów}
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 'bardzo dużo' if p > 11
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 = ''
@@ -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
- value = record.send("#{attr_name}")
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
- attr_accessor(*(attr_names.map { |n| "#{n}_confirmation" }))
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
@@ -7,7 +7,7 @@ unless defined? Ciuchcia
7
7
  module Ciuchcia
8
8
 
9
9
  # :stopdoc:
10
- VERSION = '0.0.1'
10
+ VERSION = '0.0.2'
11
11
  LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
12
12
  PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
13
13
  # :startdoc:
@@ -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
- ActiveRecord::Base.valid_nip?('779-21-25-257').should == true
21
- ActiveRecord::Base.valid_nip?('779-21-25-254').should == false
22
- ActiveRecord::Base.valid_nip?('blablabla').should == false
23
- ActiveRecord::Base.valid_nip?('blablabla').should == false
24
- ActiveRecord::Base.valid_nip?('779-21-25-25423').should == false
25
- ActiveRecord::Base.valid_nip?('7792125223').should == false
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
- ActiveRecord::Base.valid_regon?('016385358').should == true
31
- ActiveRecord::Base.valid_nip?('016385354').should == false
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.1
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-03 00:00:00 +02:00
12
+ date: 2008-07-27 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency