abaco 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 509e576544c792f2395a95275062ca29c146522a
4
+ data.tar.gz: cd45a7fd6974e6b23f991589dc72c1a1eecd9804
5
+ SHA512:
6
+ metadata.gz: 04e2ff016bceb5cdacd4e42647886eb859e70ad05b1382a2bfa0d7e534664fe5236df95b081474d4f9ba4fc3a4d10c7a9bbfb01740d9ff7925048c3bee269f6d
7
+ data.tar.gz: 1be84dee50c6e17533e8f051482cb5bcd3deb1243feea130173882421abb4063063469aebc70806544643bf5639c28ae04b50dca7a16152d5a5360774df27898
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ - 2.0.0
5
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in abaco.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 InterConn
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,75 @@
1
+ # Abaco
2
+
3
+ [![Build Status](https://travis-ci.org/interconn-isp/abaco.svg?branch=master)](https://travis-ci.org/interconn-isp/abaco)
4
+ [![Dependency Status](https://gemnasium.com/interconn-isp/abaco.svg)](https://gemnasium.com/interconn-isp/abaco)
5
+ [![Code Climate](https://codeclimate.com/github/interconn-isp/abaco/badges/gpa.svg)](https://codeclimate.com/github/interconn-isp/abaco)
6
+
7
+ Abaco is a Ruby library for converting numbers into Italian words.
8
+
9
+ It was specifically developed for the generation of postal payment slips via our
10
+ [Bollettino](https://github.com/interconn-isp/bollettino) gem. Initially, Abaco
11
+ was part of the gem, then we decided to extract the functionality.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'abaco'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install abaco
28
+
29
+ ## Usage
30
+
31
+ Abaco extends the `Numeric` class, so you can use with `Fixnum` and `Float`:
32
+
33
+ ```ruby
34
+ 15.to_italian # <= "quindici/00"
35
+ 15.5.to_italian # <= "quindici/50"
36
+ ```
37
+
38
+ You can also call the converter directly:
39
+
40
+ ```ruby
41
+ Abaco::Converter.convert(15) # <= "quindici/00"
42
+ Abaco::Converter.convert(15.5) # <= "quindici/50"
43
+ ```
44
+
45
+ ## Limitations
46
+
47
+ Presently, the biggest number Abaco can translate is **999.999.999.999**. Since
48
+ this library is mainly meant to be used for billing purposes, we didn't find it
49
+ necessary to go beyond that limit.
50
+
51
+ If you're billing someone for one trillion, contact us right away: we'll be glad
52
+ to help you! :)
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it (https://github.com/interconn-isp/abaco/fork)
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create a new Pull Request
61
+
62
+ ## Related projects
63
+
64
+ The [Bollettino](https://github.com/interconn-isp/bollettino) gem can be used
65
+ with Abaco to generate Italian postal payment slips.
66
+
67
+ ## Maintainers
68
+
69
+ Abaco is developed and maintained by [Alessandro Desantis](https://github.com/alessandro1997).
70
+
71
+ ## License
72
+
73
+ Abaco is released under the MIT license.
74
+
75
+ [![InterConn](http://www.gravatar.com/avatar/b3f5893b97323096977545477e0066c5.jpg?s=100)](http://www.interconn.it)
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'abaco/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'abaco'
8
+ spec.version = Abaco::VERSION
9
+ spec.authors = ['Alessandro Desantis']
10
+ spec.email = ['desa.alessandro@gmail.com']
11
+ spec.summary = %q{Convert numbers into Italian words.}
12
+ spec.description = %q{Abaco is a gem for converting numbers into Italian words.}
13
+ spec.homepage = 'https://github.com/interconn-isp/abaco'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.3'
23
+ spec.add_development_dependency 'rspec', '~> 3.1'
24
+ spec.add_development_dependency 'mocha', '~> 1.1'
25
+ end
@@ -0,0 +1,6 @@
1
+ require 'abaco/version'
2
+ require 'abaco/converter'
3
+ require 'abaco/core_ext/numeric'
4
+
5
+ module Abaco
6
+ end
@@ -0,0 +1,104 @@
1
+ module Abaco
2
+ # Number converter
3
+ #
4
+ # The convert converts numbers into Italian.
5
+ #
6
+ # @author Alessandro Desantis <desa.alessandro@gmail.com>
7
+ class Converter
8
+ EXCEPTIONS = {
9
+ 'unocento' => 'cento',
10
+ 'unomila' => 'mille',
11
+ 'uno m' => 'un m',
12
+ /t\wmille/ => 'tunomila',
13
+ 'ntaotto' => 'ntotto',
14
+ 'ntauno' => 'ntuno',
15
+ 'un miliardi' => 'un miliardo',
16
+ 'un milioni' => 'un milione'
17
+ }
18
+
19
+ NUMBERS = {
20
+ units: ['', 'uno', 'due', 'tre', 'quattro', 'cinque', 'sei', 'sette', 'otto', 'nove'],
21
+ teens: ['dieci', 'undici', 'dodici', 'tredici', 'quattordici', 'quindici', 'sedici', 'diciassette', 'diciotto', 'diciannove'],
22
+ tens: ['', 'dieci', 'venti', 'trenta', 'quaranta', 'cinquanta', 'sessanta', 'settanta', 'ottanta', 'novanta']
23
+ }
24
+
25
+ SUFFIXES = {
26
+ 3 => 'mila',
27
+ 6 => 'milioni',
28
+ 9 => 'miliardi'
29
+ }
30
+
31
+ MAX_NUMBER = 999999999999
32
+
33
+ # Converts the given number into Italian words.
34
+ #
35
+ # @param number [Numeric] the number to convert (<= 999.999.999.999)
36
+ #
37
+ # @return [String]
38
+ # A string in the 'NUMBER/DD' form, 'DD' being two decimal digits (these
39
+ # will be added even if the number is a Fixnum).
40
+ #
41
+ # @raise BigNumberError if the number is too big to be converted
42
+ def self.convert(number)
43
+ if number > MAX_NUMBER
44
+ raise BigNumberError, "Abaco can't convert numbers bigger than #{MAX_NUMBER}"
45
+ end
46
+
47
+ number = number.to_f
48
+
49
+ tmp = number.to_i
50
+ result = ''
51
+
52
+ 9.step(3, -3) do |n|
53
+ # Is the number in the 10^n form?
54
+ if Math.log10(tmp).to_i >= n
55
+ result += describe_part(tmp / 10 ** n)
56
+
57
+ result += ' ' if n >= 6
58
+ result += SUFFIXES[n]
59
+ result += ' ' if n >= 6
60
+ end
61
+
62
+ tmp %= 10 ** n
63
+ end
64
+
65
+ decimal_part = '/'
66
+ decimal_part += ('%.2f' % number).split('.').last
67
+
68
+ result += describe_part(tmp) + decimal_part
69
+
70
+ EXCEPTIONS.each_pair do |search, replace|
71
+ result.gsub! search, replace
72
+ end
73
+
74
+ result
75
+ end
76
+
77
+ private
78
+
79
+ def self.describe_part(number)
80
+ tmp = number
81
+ result = ''
82
+
83
+ result += NUMBERS[:units][tmp / 100] + 'cento' if tmp >= 100
84
+
85
+ if (tmp %= 100) >= 20
86
+ result += NUMBERS[:tens][tmp / 10]
87
+ elsif tmp >= 10 && tmp < 20
88
+ result += NUMBERS[:teens][tmp % 10]
89
+ tmp = 0
90
+ end
91
+
92
+ result + NUMBERS[:units][tmp % 10]
93
+ end
94
+
95
+ # Big number error
96
+ #
97
+ # This exception is raised when Abaco tries to convert a number that is too
98
+ # big to be translated (> #{MAX_NUMBER}).
99
+ #
100
+ # @author Alessandro Desantis <desa.alessandro@gmail.com>
101
+ class BigNumberError < StandardError
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,8 @@
1
+ class Numeric
2
+ # Converts the number into Italian using #{Abaco::Converter}.
3
+ #
4
+ # @return [String] the converted number
5
+ def to_italian
6
+ Abaco::Converter.convert(self)
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module Abaco
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Abaco::Converter do
4
+ subject { described_class }
5
+
6
+ describe '.convert' do
7
+ {
8
+ 34.5 => 'trentaquattro/50',
9
+ 34.05 => 'trentaquattro/05',
10
+ 7 => 'sette/00',
11
+ 17 => 'diciassette/00',
12
+ 27 => 'ventisette/00',
13
+ 100 => 'cento/00',
14
+ 107 => 'centosette/00',
15
+ 110 => 'centodieci/00',
16
+ 127 => 'centoventisette/00',
17
+ 1199 => 'millecentonovantanove/00',
18
+ 17634 => 'diciassettemilaseicentotrentaquattro/00',
19
+ 1973431 => 'un milione novecentosettantatremilaquattrocentotrentuno/00',
20
+ 1999197431 => 'un miliardo novecentonovantanove milioni centonovantasettemilaquattrocentotrentuno/00',
21
+ 999999999999 => 'novecentonovantanove miliardi novecentonovantanove milioni novecentonovantanovemilanovecentonovantanove/00'
22
+ }.each_pair do |number, result|
23
+ it "converts #{number} as '#{result}'" do
24
+ expect(subject.convert(number)).to eq(result)
25
+ end
26
+ end
27
+
28
+ it 'raises an error for numbers > 999.999.999.999' do
29
+ expect {
30
+ subject.convert(10**12)
31
+ }.to raise_error(Abaco::Converter::BigNumberError)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Numeric do
4
+ describe '#to_italian' do
5
+ it 'converts the number into Italian' do
6
+ Abaco::Converter
7
+ .expects(:convert)
8
+ .with(10)
9
+ .once
10
+ .returns('test')
11
+
12
+ expect(10.to_italian).to eq('test')
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,87 @@
1
+ require 'abaco'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
6
+ # file to always be loaded, without a need to explicitly require it in any files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # will add to the boot time of your test suite on EVERY test run, even for an
11
+ # individual file that may not need all of that loaded. Instead, consider making
12
+ # a separate helper file that requires the additional dependencies and performs
13
+ # the additional setup, and require it from the spec files that actually need it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # These two settings work together to allow you to limit a spec run
44
+ # to individual examples or groups you care about by tagging them with
45
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
46
+ # get run.
47
+ config.filter_run :focus
48
+ config.run_all_when_everything_filtered = true
49
+
50
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
51
+ # For more details, see:
52
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
53
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
54
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
55
+ config.disable_monkey_patching!
56
+
57
+ # This setting enables warnings. It's recommended, but in some cases may
58
+ # be too noisy due to issues in dependencies.
59
+ config.warnings = false
60
+
61
+ # Many RSpec users commonly either run the entire suite or an individual
62
+ # file, and it's useful to allow more verbose output when running an
63
+ # individual spec file.
64
+ if config.files_to_run.one?
65
+ # Use the documentation formatter for detailed output,
66
+ # unless a formatter has already been configured
67
+ # (e.g. via a command-line flag).
68
+ config.default_formatter = 'doc'
69
+ end
70
+
71
+ # Print the 10 slowest examples and example groups at the
72
+ # end of the spec run, to help surface which specs are running
73
+ # particularly slow.
74
+ config.profile_examples = false
75
+
76
+ # Run specs in random order to surface order dependencies. If you find an
77
+ # order dependency and want to debug it, you can fix the order by providing
78
+ # the seed, which is printed after each run.
79
+ # --seed 1234
80
+ config.order = :random
81
+
82
+ # Seed global randomization in this process using the `--seed` CLI option.
83
+ # Setting this allows you to use `--seed` to deterministically reproduce
84
+ # test failures related to randomization by passing the same `--seed` value
85
+ # as the one that triggered the failure.
86
+ Kernel.srand config.seed
87
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: abaco
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alessandro Desantis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ description: Abaco is a gem for converting numbers into Italian words.
70
+ email:
71
+ - desa.alessandro@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - abaco.gemspec
84
+ - lib/abaco.rb
85
+ - lib/abaco/converter.rb
86
+ - lib/abaco/core_ext/numeric.rb
87
+ - lib/abaco/version.rb
88
+ - spec/abaco/converter_spec.rb
89
+ - spec/core_ext/numeric.rb
90
+ - spec/spec_helper.rb
91
+ homepage: https://github.com/interconn-isp/abaco
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.3
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Convert numbers into Italian words.
115
+ test_files:
116
+ - spec/abaco/converter_spec.rb
117
+ - spec/core_ext/numeric.rb
118
+ - spec/spec_helper.rb