tuwaga 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "rspec", "~> 2.3.0"
5
+ gem "bundler", "~> 1.0.0"
6
+ gem "jeweler", "~> 1.5.2"
7
+ gem "rcov", ">= 0"
8
+ end
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ rspec (2.3.0)
13
+ rspec-core (~> 2.3.0)
14
+ rspec-expectations (~> 2.3.0)
15
+ rspec-mocks (~> 2.3.0)
16
+ rspec-core (2.3.1)
17
+ rspec-expectations (2.3.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.3.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.0.0)
26
+ jeweler (~> 1.5.2)
27
+ rcov
28
+ rspec (~> 2.3.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Yehezkiel Syamsuhadi
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.
@@ -0,0 +1,18 @@
1
+ = tuwaga
2
+
3
+ A library to convert number from one number base to another
4
+
5
+ == Contributing to tuwaga
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Yehezkiel Syamsuhadi. See LICENSE.txt for
18
+ further details.
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "tuwaga"
16
+ gem.homepage = "http://github.com/yehezkielbs/tuwaga"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{A library to convert number from one number base to another}
19
+ gem.description = %Q{A library to convert number from one number base to another}
20
+ gem.email = "yehezkielbs@gmail.com"
21
+ gem.authors = ["Yehezkiel Syamsuhadi"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "tuwaga #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.3
@@ -0,0 +1,79 @@
1
+ class Tuwaga
2
+ attr_reader :base, :symbols, :symbol_value_map
3
+
4
+ def initialize base = 10, symbols = nil
5
+ @base = base
6
+ validate_base
7
+
8
+ @symbols = blank?(symbols) ? guess_symbols : symbols.split('')
9
+ validate_symbols
10
+
11
+ @symbol_value_map = {}
12
+ @symbols.each_with_index { |item, index| @symbol_value_map[item] = index }
13
+ end
14
+
15
+ def to_decimal input
16
+ raise 'Input must be a string' if (!input.is_a?(String))
17
+
18
+ result = 0
19
+ power = 0
20
+ input.split('').reverse.each do |char|
21
+ result += @symbol_value_map[char] * (@base ** power)
22
+ power += 1
23
+ end
24
+
25
+ result
26
+ end
27
+
28
+ def from_decimal input
29
+ raise 'Input must be an integer' if (!input.is_a?(Integer))
30
+
31
+ return @symbols[0] if input == 0
32
+
33
+ result = ''
34
+ in_decimal = input
35
+ while (in_decimal > 0) do
36
+ result = @symbols[in_decimal % @base] + result
37
+ in_decimal = in_decimal / @base
38
+ end
39
+
40
+ result
41
+ end
42
+
43
+ def convert_to value, number_base
44
+ raise 'Value must be a String' if (!value.is_a?(String))
45
+ raise 'Number_base must be an instance of Tuwaga' if (!number_base.is_a?(Tuwaga))
46
+
47
+ number_base.from_decimal(to_decimal(value))
48
+ end
49
+
50
+ def convert_from value, number_base
51
+ raise 'Value must be a String' if (!value.is_a?(String))
52
+ raise 'Number_base must be an instance of Tuwaga' if (!number_base.is_a?(Tuwaga))
53
+
54
+ from_decimal(number_base.to_decimal(value))
55
+ end
56
+
57
+ private
58
+
59
+ def blank? value
60
+ !value || (value.respond_to?('empty?') && value.empty?)
61
+ end
62
+
63
+ def guess_symbols
64
+ raise 'Can not guess what should be the symbols when base > 36 and symbols are not defined' if (@base > 36)
65
+
66
+ num_alpha = ('0'..'9').to_a + ('a'..'z').to_a
67
+ num_alpha[0, @base]
68
+ end
69
+
70
+ def validate_base
71
+ raise 'Base cannot be less than 2' if (@base < 2)
72
+ raise 'Base must be an integer' if (!@base.is_a?(Integer))
73
+ end
74
+
75
+ def validate_symbols
76
+ raise 'Symbols contains duplicate(s)' if (@symbols != @symbols.uniq)
77
+ raise 'Symbols length is not equal to base' if (@symbols.length != @base)
78
+ end
79
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'tuwaga'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
@@ -0,0 +1,93 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe 'Tuwaga' do
4
+ describe 'constructor' do
5
+ it 'should be able to construct Tuwaga with default settings' do
6
+ dec_system = Tuwaga.new
7
+ dec_system.should be_an_instance_of(Tuwaga)
8
+ dec_system.base.should == 10
9
+ dec_system.symbols.should == ('0'..'9').to_a
10
+
11
+ expected_symbol_value_map = {}
12
+ ('0'..'9').to_a.each_with_index { |item, index| expected_symbol_value_map[item] = index }
13
+ dec_system.symbol_value_map.should == expected_symbol_value_map
14
+ end
15
+
16
+ it 'should be able to construct Tuwaga with given settings' do
17
+ new_system = Tuwaga.new(4, 'abcd')
18
+ new_system.should be_an_instance_of(Tuwaga)
19
+ new_system.base.should == 4
20
+ new_system.symbols.should == %w( a b c d )
21
+
22
+ expected_result = {
23
+ 'a' => 0,
24
+ 'b' => 1,
25
+ 'c' => 2,
26
+ 'd' => 3
27
+ }
28
+ new_system.symbol_value_map.should == expected_result
29
+ end
30
+
31
+ it 'should die when base is less than 2' do
32
+ lambda { Tuwaga.new(-2) }.should raise_error('Base cannot be less than 2')
33
+ end
34
+
35
+ it 'should die when base is not an integer' do
36
+ lambda { Tuwaga.new(3.2) }.should raise_error('Base must be an integer')
37
+ end
38
+
39
+ it 'should die when base > 36 but symbols are not defined' do
40
+ lambda { Tuwaga.new(37) }.should raise_error('Can not guess what should be the symbols when base > 36 and symbols are not defined')
41
+ end
42
+
43
+ it 'should die when symbols length is not equal to base' do
44
+ lambda { Tuwaga.new(2, '012') }.should raise_error('Symbols length is not equal to base')
45
+ end
46
+
47
+ it 'should die when symbols contains duplicate(s)' do
48
+ lambda { Tuwaga.new(4, '0112') }.should raise_error('Symbols contains duplicate(s)')
49
+ end
50
+ end
51
+
52
+ describe 'converter' do
53
+ [
54
+ #base x, symbols, number in decimal, number in base x
55
+ [16, nil, 16, '10'],
56
+ [16, '0123456789QWERTY', 773037414154, 'W3YE9TR70Q'],
57
+ [10, '9876543210', 1234567890, '8765432109'],
58
+ [4, 'BLAH', 4, 'LB'],
59
+ [2, '01', 0, '0'],
60
+ [16, nil, 10, 'a']
61
+ ].each do |test|
62
+ (base, symbols, number_in_decimal, number_in_base_x) = test
63
+
64
+ it "should be able to convert '#{number_in_base_x}' in base #{base} to decimal" do
65
+ number_base = Tuwaga.new(base, symbols)
66
+ number_base.to_decimal(number_in_base_x).should == number_in_decimal
67
+ end
68
+
69
+ it "should be able to convert '#{number_in_decimal}' in decimal to base #{base}" do
70
+ number_base = Tuwaga.new(base, symbols)
71
+ number_base.from_decimal(number_in_decimal).should == number_in_base_x
72
+ end
73
+
74
+ it "should be able to convert '#{number_in_base_x}' in base #{base} to base 7" do
75
+ number_base = Tuwaga.new(base, symbols)
76
+
77
+ base_7 = Tuwaga.new(7, 'qwertyu')
78
+ number_in_base_7 = number_base.convert_to(number_in_base_x, base_7);
79
+
80
+ base_7.to_decimal(number_in_base_7).should == number_in_decimal
81
+ end
82
+
83
+ it "should be able to convert '#{number_in_decimal}' in base #{base} from base 7" do
84
+ number_base = Tuwaga.new(base, symbols)
85
+
86
+ base_7 = Tuwaga.new(7, 'qwertyu')
87
+ number_in_base_7 = number_base.convert_to(number_in_base_x, base_7);
88
+
89
+ number_base.convert_from(number_in_base_7, base_7).should == number_in_base_x
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
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{tuwaga}
8
+ s.version = "0.0.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Yehezkiel Syamsuhadi"]
12
+ s.date = %q{2011-02-22}
13
+ s.description = %q{A library to convert number from one number base to another}
14
+ s.email = %q{yehezkielbs@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/tuwaga.rb",
29
+ "spec/spec_helper.rb",
30
+ "spec/tuwaga_spec.rb",
31
+ "tuwaga.gemspec"
32
+ ]
33
+ s.homepage = %q{http://github.com/yehezkielbs/tuwaga}
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.4.2}
37
+ s.summary = %q{A library to convert number from one number base to another}
38
+ s.test_files = [
39
+ "spec/spec_helper.rb",
40
+ "spec/tuwaga_spec.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
48
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
50
+ s.add_development_dependency(%q<rcov>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
53
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
55
+ s.add_dependency(%q<rcov>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
59
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
60
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
61
+ s.add_dependency(%q<rcov>, [">= 0"])
62
+ end
63
+ end
64
+
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tuwaga
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 3
10
+ version: 0.0.3
11
+ platform: ruby
12
+ authors:
13
+ - Yehezkiel Syamsuhadi
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-22 00:00:00 +11:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ name: rspec
24
+ type: :development
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 2
33
+ - 3
34
+ - 0
35
+ version: 2.3.0
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ prerelease: false
39
+ name: bundler
40
+ type: :development
41
+ version_requirements: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ hash: 23
47
+ segments:
48
+ - 1
49
+ - 0
50
+ - 0
51
+ version: 1.0.0
52
+ requirement: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ prerelease: false
55
+ name: jeweler
56
+ type: :development
57
+ version_requirements: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ hash: 7
63
+ segments:
64
+ - 1
65
+ - 5
66
+ - 2
67
+ version: 1.5.2
68
+ requirement: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ prerelease: false
71
+ name: rcov
72
+ type: :development
73
+ version_requirements: &id004 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirement: *id004
83
+ description: A library to convert number from one number base to another
84
+ email: yehezkielbs@gmail.com
85
+ executables: []
86
+
87
+ extensions: []
88
+
89
+ extra_rdoc_files:
90
+ - LICENSE.txt
91
+ - README.rdoc
92
+ files:
93
+ - .document
94
+ - .rspec
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - LICENSE.txt
98
+ - README.rdoc
99
+ - Rakefile
100
+ - VERSION
101
+ - lib/tuwaga.rb
102
+ - spec/spec_helper.rb
103
+ - spec/tuwaga_spec.rb
104
+ - tuwaga.gemspec
105
+ has_rdoc: true
106
+ homepage: http://github.com/yehezkielbs/tuwaga
107
+ licenses:
108
+ - MIT
109
+ post_install_message:
110
+ rdoc_options: []
111
+
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ hash: 3
120
+ segments:
121
+ - 0
122
+ version: "0"
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ hash: 3
129
+ segments:
130
+ - 0
131
+ version: "0"
132
+ requirements: []
133
+
134
+ rubyforge_project:
135
+ rubygems_version: 1.4.2
136
+ signing_key:
137
+ specification_version: 3
138
+ summary: A library to convert number from one number base to another
139
+ test_files:
140
+ - spec/spec_helper.rb
141
+ - spec/tuwaga_spec.rb