basecustom 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2012 Daniel P. Clark & 6ft Dan(TM)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
+
data/README ADDED
@@ -0,0 +1,26 @@
1
+ BaseCustom
2
+ by Daniel P. Clark
3
+
4
+ Inspired by:
5
+ * base62 by "JT Zemp" and contributors "Saadiq Rodgers-King", "Derrick Camerino"
6
+
7
+ Description
8
+ * Define any base conversion with any identifier for each value.
9
+
10
+ require 'basecustom'
11
+
12
+ base2 = BaseCustom.new('01')
13
+ base2.base('00001')
14
+ => 1
15
+ base2.base('100110101')
16
+ => 309
17
+ base2.base(340)
18
+ => "101010100"
19
+ base2.base(0xF45)
20
+ => "111101000101"
21
+
22
+ baseABC = BaseCustom.new('ABC')
23
+ baseABC.base('ABC')
24
+ => 5
25
+ baseABC.base(123)
26
+ => "BBBCA"
@@ -0,0 +1,3 @@
1
+ class BaseCustom
2
+ VERSION = "0.1.0"
3
+ end
data/lib/basecustom.rb ADDED
@@ -0,0 +1,47 @@
1
+ # BaseCustom
2
+ # by Daniel P. Clark
3
+ # MIT License
4
+ # Define any base conversion with any identifier for each value.
5
+ $: << File.join(File.dirname(__FILE__), "/basecustom")
6
+ require 'basecustom/version'
7
+
8
+ class BaseCustom
9
+ def initialize(array_in)
10
+ if array_in.is_a?(String)
11
+ array_in = array_in.split('')
12
+ end
13
+ if not array_in.is_a?(Array)
14
+ raise "Invalid type. Please provide a String or an Array."
15
+ end
16
+ @BASE_PRIMITIVES_ARRAY = array_in
17
+ @BASE_PRIMITIVES_HASH = Hash[@BASE_PRIMITIVES_ARRAY.each_with_index.map {|x,idx| [x, idx]}]
18
+ end # initialize
19
+
20
+ def base(input_val)
21
+ if input_val.is_a?(String)
22
+ if input_val.split('').any? { |i| not @BASE_PRIMITIVES_ARRAY.include?(i) }
23
+ raise "Characters used are not in predefined base!"
24
+ end
25
+ i, i_out = 0, 0
26
+ input_val.split(//).reverse.each do |c|
27
+ place = @BASE_PRIMITIVES_HASH.size ** i
28
+ i_out += @BASE_PRIMITIVES_HASH[c] * place
29
+ i += 1
30
+ end
31
+ i_out
32
+ # end input_val.is_a?(String)
33
+ elsif input_val.is_a?(Integer)
34
+ return "0" if input_val == 0
35
+ number = input_val
36
+ result = ''
37
+ while(number != 0)
38
+ result = @BASE_PRIMITIVES_ARRAY[number % @BASE_PRIMITIVES_ARRAY.size ].to_s + result
39
+ number /= @BASE_PRIMITIVES_ARRAY.size
40
+ end
41
+ result
42
+ # end input_val.is_a?(Integer)
43
+ else
44
+ raise "#{input_val.class} is not a supported type!"
45
+ end
46
+ end # base
47
+ end # class BaseCustom
data/test/test_bc.rb ADDED
@@ -0,0 +1,34 @@
1
+ $: << File.join(File.dirname(__FILE__), "/../lib")
2
+ $: << File.join(File.dirname(__FILE__), "/../lib/basecustom")
3
+ require 'test/unit'
4
+ require 'basecustom'
5
+
6
+ class TestBaseCustom < Test::Unit::TestCase
7
+ def test_base2
8
+ base2 = BaseCustom.new('01')
9
+ assert base2.base('00001') == 1
10
+ assert base2.base('100110101') == 309
11
+ assert base2.base(340) == "101010100"
12
+ assert base2.base(0xF45) == "111101000101"
13
+ assert base2.base(0b111) == "111"
14
+ end
15
+
16
+ def test_baseABC
17
+ baseABC = BaseCustom.new('ABC')
18
+ assert baseABC.base('ABC') == 5
19
+ assert baseABC.base(123) == "BBBCA"
20
+ end
21
+
22
+ def test_base10
23
+ base10 = BaseCustom.new('0123456789')
24
+ assert base10.base(123) == "123"
25
+ assert base10.base("123") == 123
26
+ end
27
+
28
+ def test_errors
29
+ base2 = BaseCustom.new('01')
30
+ assert_raise RuntimeError, LoadError do base2.base(:nonexistant); end
31
+ assert_raise RuntimeError, LoadError do base2.base('abc'); end
32
+ assert_raise RuntimeError, LoadError do base2.base(4.5); end
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: basecustom
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Daniel P. Clark / 6ftDan(TM)
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-10-30 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Define any base conversion with any identifier for each value. Let the fun begin!
22
+ email: webmaster@6ftdan.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ files:
30
+ - lib/basecustom.rb
31
+ - lib/basecustom/version.rb
32
+ - test/test_bc.rb
33
+ - README
34
+ - LICENSE
35
+ homepage: http://www.github.com/danielpclark/BaseCustom
36
+ licenses:
37
+ - The MIT License (MIT)
38
+ post_install_message: Enjoy BaseCustom! You may now define your own numbering systems.
39
+ rdoc_options:
40
+ - --main
41
+ - README
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.24
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Define any base conversion with any identifier for each value.
69
+ test_files:
70
+ - test/test_bc.rb