bulgarianize 0.1.2

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Stefan Kanev
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,37 @@
1
+ bulgarianize
2
+ ============
3
+
4
+ A small collection of niceness for localizing your applications in Bulgarian.
5
+ If the following text doesn't make any sense, you probably won't need the gem.
6
+
7
+ На български
8
+ ------------
9
+
10
+ `bulgarianize` представлява малка колекция от решения на всякакви проблеми,
11
+ които изникват при писане на софтуер на български. Или поне тези, с които
12
+ авторът му се е сблъскал.
13
+
14
+ Функционалността включва (и вероятно е лимитирана до):
15
+
16
+ (Цели) числа словом
17
+ -------------------
18
+
19
+ Проста хватка:
20
+
21
+ 1.bg.as_words # едно
22
+ 42.bg.as_words # четиридесет и две
23
+ 1_024.bg.as_words # хиляда двадесет и четири
24
+
25
+ Поради граматическа особеност на българския език се поддържа следното:
26
+
27
+ 1.bg.as_words(:male) # един
28
+ 1.bg.as_words(:female) # една
29
+ 1.bg.as_words(:neuter) # едно
30
+ 42.bg.as_words(:male) # четиридесет и два
31
+
32
+ По подразбиране родът е среден.
33
+
34
+ Copyright
35
+ ---------
36
+
37
+ Copyright (c) 2009 Stefan Kanev. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "bulgarianize"
8
+ gem.summary = %Q{Adds pizzazz to your Bulgarian-speaking ruby code}
9
+ gem.description = %Q{A humble gem adding various usefulness for dealing with Bulgarian localization.}
10
+ gem.email = "stefan.kanev@gmail.com"
11
+ gem.homepage = "http://github.com/skanev/bulgarianize"
12
+ gem.authors = ["Stefan Kanev"]
13
+ gem.add_development_dependency "rspec"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ spec.spec_opts = ['--colour', '--format progress']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ if File.exist?('VERSION')
40
+ version = File.read('VERSION')
41
+ else
42
+ version = ""
43
+ end
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "bulgarianize #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{bulgarianize}
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Stefan Kanev"]
12
+ s.date = %q{2009-09-27}
13
+ s.description = %q{A humble gem adding various usefulness for dealing with Bulgarian localization.}
14
+ s.email = %q{stefan.kanev@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.markdown",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "bulgarianize.gemspec",
27
+ "lib/bulgarianize.rb",
28
+ "lib/bulgarianize/number_to_words.rb",
29
+ "lib/bulgarianize/proxies.rb",
30
+ "spec/bulgarianize_spec.rb",
31
+ "spec/number_to_words_spec.rb",
32
+ "spec/proxies_spec.rb",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/skanev/bulgarianize}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.4}
39
+ s.summary = %q{Adds pizzazz to your Bulgarian-speaking ruby code}
40
+ s.test_files = [
41
+ "spec/bulgarianize_spec.rb",
42
+ "spec/number_to_words_spec.rb",
43
+ "spec/proxies_spec.rb",
44
+ "spec/spec_helper.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<rspec>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<rspec>, [">= 0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<rspec>, [">= 0"])
58
+ end
59
+ end
@@ -0,0 +1,2 @@
1
+ require 'bulgarianize/proxies'
2
+ require 'bulgarianize/number_to_words'
@@ -0,0 +1,67 @@
1
+ module Bulgarianize
2
+ class Condition
3
+ def initialize(&block)
4
+ @block = block
5
+ end
6
+
7
+ def ===(n)
8
+ @block[n]
9
+ end
10
+ end
11
+
12
+ module NumberToWords
13
+ MALE_DIGITS = %w[нула един два три четири пет шест седем осем девет]
14
+ FEMALE_DIGITS = %w[нула една две три четири пет шест седем осем девет]
15
+ NEUTER_DIGITS = %w[нула едно две три четири пет шест седем осем девет]
16
+ NAMES_11_TO_19 = %w[единадесет дванадесет тринадесет четиринадесет петданесет шестнадесет седемнадесет осемнадесет деветнадесет]
17
+ MULTIPLES_OF_10 = %w[десет двадесет тридесет четиридесет петдесет шестдесет седемдесет осемдесет деветдесет]
18
+ HUNDREDS = %w[сто двеста триста четиристотин петстотин шестстотин седемстотин осемстотин деветстотин]
19
+
20
+ def as_words(gender = :neuter)
21
+ words_for(self.numeric.to_i, gender)
22
+ end
23
+
24
+ def as_leva
25
+ levas, cents = self.numeric.floor.to_i, self.numeric.%(1).*(100).to_i
26
+ "#{words_for(levas, :male)} лева и #{cents} стотинки"
27
+ end
28
+
29
+ protected
30
+
31
+ def words_for(n, gender)
32
+ case n
33
+ when 0..9 then {:male => MALE_DIGITS, :female => FEMALE_DIGITS, :neuter => NEUTER_DIGITS}.fetch(gender).fetch(n)
34
+ when 11..19 then NAMES_11_TO_19.fetch(n - 11)
35
+ when 10, 20, 30, 40, 50, 60, 70, 80, 90 then MULTIPLES_OF_10.fetch(n / 10 - 1)
36
+ when 21..99 then join_words words_for(whole_part(n, 10), gender), words_for(n % 10, gender)
37
+ when 100, 200, 300, 400, 500, 600, 700, 800, 900 then HUNDREDS.fetch(n / 100 - 1)
38
+ when 101..999 then join_words words_for(whole_part(n, 100), gender), words_for(n % 100, gender)
39
+ when 1000 then 'хиляда'
40
+ when cond { |n| n % 1_000 == 0 and n < 1_000_000 } then "#{words_for(n / 1000, :female)} хиляди"
41
+ when 1_001..999_999 then join_words words_for(whole_part(n, 1000), gender), words_for(n % 1000, gender)
42
+ when 1_000_000 then 'един милион'
43
+ when cond { |n| n % 1_000_000 == 0 and n < 1_000_000_000_000 } then "#{words_for(n / 1_000_000, :male)} милиона"
44
+ when 1_000_001..999_999_999_999 then join_words words_for(whole_part(n, 1_000_000), gender), words_for(n % 1_000_000, gender)
45
+ else n.to_s
46
+ end
47
+ end
48
+
49
+ def join_words(a, b)
50
+ if b.include? ' и '
51
+ "#{a} #{b}"
52
+ else
53
+ "#{a} и #{b}"
54
+ end
55
+ end
56
+
57
+ def cond(&block)
58
+ Condition.new(&block)
59
+ end
60
+
61
+ def whole_part(number, base)
62
+ (number / base) * base
63
+ end
64
+ end
65
+
66
+ NumericProxy.send :include, NumberToWords
67
+ end
@@ -0,0 +1,29 @@
1
+ module Bulgarianize
2
+ class StringProxy
3
+ attr_accessor :string
4
+
5
+ def initialize(string)
6
+ @string = string
7
+ end
8
+ end
9
+
10
+ class NumericProxy
11
+ attr_accessor :numeric
12
+
13
+ def initialize(numeric)
14
+ @numeric = numeric
15
+ end
16
+ end
17
+ end
18
+
19
+ class Numeric
20
+ def bg
21
+ Bulgarianize::NumericProxy.new(self)
22
+ end
23
+ end
24
+
25
+ class String
26
+ def bg
27
+ Bulgarianize::StringProxy.new(self)
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Bulgarianize" do
4
+ end
@@ -0,0 +1,105 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require 'bigdecimal'
4
+
5
+ describe "NumberToWords" do
6
+ describe "#as_leva" do
7
+ it "should work with simple numbers less than 10" do
8
+ %w{нула едно две три четири пет шест седем осем девет}.each_with_index do |words, number|
9
+ number.bg.as_words.should == words
10
+ end
11
+ end
12
+
13
+ it "should enable genders with numbers less than 10" do
14
+ numbers = [
15
+ %w{нула нула нула},
16
+ %w{един една едно},
17
+ %w{два две две},
18
+ %w{три три три},
19
+ ]
20
+
21
+ numbers.each_with_index do |names, number|
22
+ [:male, :female, :neuter].zip(names) do |gender, name|
23
+ number.bg.as_words(gender).should == name
24
+ end
25
+ end
26
+ end
27
+
28
+ it "should work with numbers 11-19" do
29
+ %w[единадесет дванадесет тринадесет четиринадесет петданесет шестнадесет седемнадесет осемнадесет деветнадесет].zip((11..19).to_a).each do |name, number|
30
+ number.bg.as_words.should == name
31
+ end
32
+ end
33
+
34
+ it "should work with numbers dividable by 10" do
35
+ %w[десет двадесет тридесет четиридесет петдесет шестдесет седемдесет осемдесет деветдесет].zip([10, 20, 30, 40, 50, 60, 70, 80, 90]).each do |name, number|
36
+ number.bg.as_words.should == name
37
+ end
38
+ end
39
+
40
+ it "should work with numbers less than 100, not dividable by 10" do
41
+ 42.bg.as_words.should == 'четиридесет и две'
42
+ 97.bg.as_words.should == 'деветдесет и седем'
43
+ 89.bg.as_words.should == 'осемдесет и девет'
44
+ end
45
+
46
+ it "should work with numbers less than 1000, divisable by 100" do
47
+ %w[сто двеста триста четиристотин петстотин шестстотин седемстотин осемстотин деветстотин].zip([100, 200, 300, 400, 500, 600, 700, 800, 900]).each do |name, number|
48
+ number.bg.as_words.should == name
49
+ end
50
+ end
51
+
52
+ it "should work with numbers 100-1000, not dividable by 100" do
53
+ 110.bg.as_words.should == 'сто и десет'
54
+ 217.bg.as_words.should == 'двеста и седемнадесет'
55
+ 420.bg.as_words.should == 'четиристотин и двадесет'
56
+
57
+ 123.bg.as_words.should == 'сто двадесет и три'
58
+ end
59
+
60
+ it "should work with numbers divisable with 1000" do
61
+ 1_000.bg.as_words.should == 'хиляда'
62
+ 2_000.bg.as_words.should == 'две хиляди'
63
+ 5_000.bg.as_words.should == 'пет хиляди'
64
+ 10_000.bg.as_words.should == 'десет хиляди'
65
+ 13_000.bg.as_words.should == 'тринадесет хиляди'
66
+ 28_000.bg.as_words.should == 'двадесет и осем хиляди'
67
+ 100_000.bg.as_words.should == 'сто хиляди'
68
+ 673_000.bg.as_words.should == 'шестстотин седемдесет и три хиляди'
69
+ end
70
+
71
+ it "should work with numbers less than 1 000 000, not divisable with 1000" do
72
+ 1_001.bg.as_words.should == 'хиляда и едно'
73
+ 1_024.bg.as_words.should == 'хиляда двадесет и четири'
74
+ 17_001.bg.as_words.should == 'седемнадесет хиляди и едно'
75
+ 24_200.bg.as_words.should == 'двадесет и четири хиляди и двеста'
76
+ 65_536.bg.as_words.should == 'шестдесет и пет хиляди петстотин тридесет и шест'
77
+
78
+ 123_456.bg.as_words.should == 'сто двадесет и три хиляди четиристотин петдесет и шест'
79
+ end
80
+
81
+ it "should work with millions" do
82
+ 1_000_000.bg.as_words.should == 'един милион'
83
+ 1_000_001.bg.as_words.should == 'един милион и едно'
84
+ 2_000_200.bg.as_words.should == 'два милиона и двеста'
85
+ 35_000_035.bg.as_words.should == 'тридесет и пет милиона тридесет и пет'
86
+ 123_456_789.bg.as_words.should == 'сто двадесет и три милиона четиристотин петдесет и шест хиляди седемстотин осемдесет и девет'
87
+ end
88
+
89
+ it "should work with big numbers in male gender" do
90
+ 121_121_121.bg.as_words(:male).should == 'сто двадесет и един милиона сто двадесет и една хиляди сто двадесет и един'
91
+ end
92
+
93
+ it "should work with the integer part only" do
94
+ BigDecimal("42").bg.as_words.should == 'четиридесет и две'
95
+ BigDecimal("42.10").bg.as_words.should == 'четиридесет и две'
96
+ end
97
+ end
98
+
99
+ it "should give numbers as levas" do
100
+ 42.bg.as_leva.should == 'четиридесет и два лева и 0 стотинки'
101
+ 3.14.bg.as_leva.should == 'три лева и 14 стотинки'
102
+ BigDecimal('3.05').bg.as_leva.should == 'три лева и 5 стотинки'
103
+ end
104
+
105
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Proxies" do
4
+ it "should make itself available as String#bg" do
5
+ "Larodi".bg.string.should == "Larodi"
6
+ end
7
+
8
+ it "should make itself available as Numeric#bg" do
9
+ 1.bg.numeric.should == 1
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'bulgarianize'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bulgarianize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Stefan Kanev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-27 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: A humble gem adding various usefulness for dealing with Bulgarian localization.
26
+ email: stefan.kanev@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.markdown
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.markdown
39
+ - Rakefile
40
+ - VERSION
41
+ - bulgarianize.gemspec
42
+ - lib/bulgarianize.rb
43
+ - lib/bulgarianize/number_to_words.rb
44
+ - lib/bulgarianize/proxies.rb
45
+ - spec/bulgarianize_spec.rb
46
+ - spec/number_to_words_spec.rb
47
+ - spec/proxies_spec.rb
48
+ - spec/spec_helper.rb
49
+ has_rdoc: true
50
+ homepage: http://github.com/skanev/bulgarianize
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --charset=UTF-8
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.5
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Adds pizzazz to your Bulgarian-speaking ruby code
77
+ test_files:
78
+ - spec/bulgarianize_spec.rb
79
+ - spec/number_to_words_spec.rb
80
+ - spec/proxies_spec.rb
81
+ - spec/spec_helper.rb