JackDanger-alphadecimal 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ ### 1.0.0 / 2008-03-11
2
+
3
+ * added rails/init.rb to allow loading as a gem into a Rails app
4
+
5
+ ### 1.0.0 / 2008-03-11
6
+
7
+ * 1 major enhancement
8
+
9
+ * Birthday!
10
+
@@ -0,0 +1,10 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ alphadecimal.gemspec
6
+ bin/alphadecimal
7
+ init.rb
8
+ lib/alphadecimal.rb
9
+ rails/init.rb
10
+ test/test_alphadecimal.rb
@@ -0,0 +1,49 @@
1
+ = alphadecimal
2
+
3
+ * http://github.com/JackDanger/alphadecimal
4
+
5
+ == DESCRIPTION:
6
+
7
+ Convert integers to base62 strings (A-Za-z0-9) and back. A handy
8
+ way to shorten long numbers.
9
+
10
+ == SYNOPSIS:
11
+
12
+ # 45.alphadecimal
13
+ # => 'j'
14
+ # 5234233.alphadecimal
15
+ # 'Lxf7'
16
+ # 'yH783Xx3Ajz99'.alphadecimal
17
+ # => 194466611442340388130115
18
+ # 194466611442340388130115.alphadecimal
19
+ # => 'yH783Xx3Ajz99'
20
+
21
+
22
+ == INSTALL:
23
+
24
+ * sudo gem install alphadecimal
25
+
26
+ == LICENSE:
27
+
28
+ (The MIT License)
29
+
30
+ Copyright (c) 2008 Jack Danger Canty, Mike Mondragon
31
+
32
+ Permission is hereby granted, free of charge, to any person obtaining
33
+ a copy of this software and associated documentation files (the
34
+ 'Software'), to deal in the Software without restriction, including
35
+ without limitation the rights to use, copy, modify, merge, publish,
36
+ distribute, sublicense, and/or sell copies of the Software, and to
37
+ permit persons to whom the Software is furnished to do so, subject to
38
+ the following conditions:
39
+
40
+ The above copyright notice and this permission notice shall be
41
+ included in all copies or substantial portions of the Software.
42
+
43
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
44
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
45
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
46
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
47
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
48
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
49
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/alphadecimal.rb'
6
+
7
+ Hoe.new('alphadecimal', Alphadecimal::VERSION) do |p|
8
+ # p.rubyforge_name = 'alphadecimal' # if different than lowercase project name
9
+ p.developer('Mike Mondragon', 'mikemondragon@gmail.com')
10
+ p.developer('Jack Danger Canty', 'rubygems@6brand.com')
11
+ end
12
+
13
+ # vim: syntax=Ruby
@@ -0,0 +1,35 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{alphadecimal}
3
+ s.version = "1.0.1"
4
+
5
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
+ s.authors = ["Mike Mondragon", "Jack Danger Canty"]
7
+ s.date = %q{2009-03-19}
8
+ s.default_executable = %q{alphadecimal}
9
+ s.description = %q{Convert integers to base62 strings (A-Za-z0-9) and back. A handy way to shorten long numbers.}
10
+ s.email = ["mikemondragon@gmail.com", "rubygems@6brand.com"]
11
+ s.executables = ["alphadecimal"]
12
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
13
+ s.files = ["History.txt", "Manifest.txt", "README.txt", "Rakefile", "alphadecimal.gemspec", "bin/alphadecimal", "init.rb", "lib/alphadecimal.rb", "rails/init.rb", "test/test_alphadecimal.rb"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/JackDanger/alphadecimal}
16
+ s.rdoc_options = ["--main", "README.txt"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{alphadecimal}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Convert integers to base62 strings (A-Za-z0-9) and back}
21
+ s.test_files = ["test/test_alphadecimal.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_development_dependency(%q<hoe>, [">= 1.11.0"])
29
+ else
30
+ s.add_dependency(%q<hoe>, [">= 1.11.0"])
31
+ end
32
+ else
33
+ s.add_dependency(%q<hoe>, [">= 1.11.0"])
34
+ end
35
+ end
File without changes
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'alphadecimal'
@@ -0,0 +1,64 @@
1
+ module Alphadecimal
2
+ VERSION = '1.0.1'
3
+
4
+ B62_0, B62_9 = '0'[0], '9'[0]
5
+ B62_A, B62_Z = 'A'[0], 'Z'[0]
6
+ B62_a, B62_z = 'a'[0], 'z'[0]
7
+ B62_CHRS = [(B62_0..B62_9).map, (B62_A..B62_Z).map, (B62_a..B62_z).map].flatten
8
+
9
+ module Number
10
+ def alphadecimal
11
+ return self if nil? || self < 0
12
+ return to_s if self == 0
13
+ string = ""
14
+ value = self
15
+ until value == 0
16
+ case mod = value % 62
17
+ when 0..9 then string << (mod + B62_0)
18
+ when 10..35 then string << (mod - 10 + B62_A)
19
+ when 36...62 then string << (mod - 36 + B62_a)
20
+ end
21
+ value = value / 62
22
+ end
23
+ string.reverse
24
+ end
25
+ end
26
+
27
+ module String
28
+ def alphadecimal
29
+ return self unless is_alphadecimal?
30
+ val = 0
31
+ key = reverse
32
+ (0...key.size).each do |i|
33
+ char = key[i]
34
+ case
35
+ when (B62_0..B62_9).include?(char) then norm = char - B62_0
36
+ when (B62_A..B62_Z).include?(char) then norm = char - B62_A + 10
37
+ when (B62_a..B62_z).include?(char) then norm = char - B62_a + 36
38
+ end
39
+ val = val + (norm * (62 ** i))
40
+ end
41
+ val
42
+ end
43
+
44
+ def is_alphadecimal?
45
+ return false if nil?
46
+ string = self.dup
47
+ return false if string.length <= 0
48
+ (0...string.size).each do |i|
49
+ return false unless B62_CHRS.include?(string[i])
50
+ end
51
+ true
52
+ end
53
+ end
54
+ end
55
+
56
+ class String
57
+ include Alphadecimal::String
58
+ end
59
+ class Fixnum
60
+ include Alphadecimal::Number
61
+ end
62
+ class Bignum
63
+ include Alphadecimal::Number
64
+ end
@@ -0,0 +1 @@
1
+ require 'alphadecimal'
@@ -0,0 +1,69 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + "/../lib/alphadecimal"
3
+
4
+ class TestAlphadecimal < Test::Unit::TestCase
5
+
6
+ include Alphadecimal
7
+
8
+ def test_fixnum_becomes_string
9
+ assert_kind_of String, 45.alphadecimal
10
+ end
11
+
12
+ def test_bignum_becomes_string
13
+ assert_kind_of String, 231087934571049032.alphadecimal
14
+ end
15
+
16
+ def test_string_becomes_bignum
17
+ assert_kind_of Bignum, 'aHealk43JZy'.alphadecimal
18
+ end
19
+
20
+ def test_string_becomes_fixnum
21
+ assert_kind_of Fixnum, 'j'.alphadecimal
22
+ end
23
+
24
+ def test_invalid_string_returns_self
25
+ string = 'Whoah there!'
26
+ assert_equal string, string.alphadecimal
27
+ end
28
+
29
+ def test_identities
30
+ [123, 90000008, 733, '12331', 'abYzj', '0', 'A', 'z',
31
+ 79823898923232, 'ZZZZZZZZZZZZZZZZZZZ', 'Yz82J3Ng5Nj9M1',
32
+ '!!!', '@$%!^', 'abc ', 'HP', 4, 'ǟ'].each do |object|
33
+ assert_equal object, object.alphadecimal.alphadecimal
34
+ end
35
+ end
36
+
37
+ def test_alphabet_characters_should_only_validate
38
+ alphabet = (('0'[0]..'9'[0]).collect <<
39
+ ('A'[0]..'Z'[0]).collect <<
40
+ ('a'[0]..'z'[0]).collect).flatten
41
+ (0..255).each do |i|
42
+ case
43
+ when alphabet.include?(i)
44
+ assert i.chr.is_alphadecimal?, "char #{i} is not valid as string #{i.chr}"
45
+ else
46
+ assert !i.chr.is_alphadecimal?, "char #{i} is valid as string #{i.chr}"
47
+ end
48
+ end
49
+ end
50
+
51
+ def test_edge_62_to_the_0_convertions_should_be_valid
52
+ (0...62).each do |i|
53
+ encode = i.alphadecimal
54
+ decode = encode.alphadecimal
55
+ assert_equal i, decode, "integer #{i} was encoded as #{encode} and was decoded to #{decode}"
56
+ end
57
+ end
58
+
59
+ def test_edge_62_to_the_n_convertions_should_be_valid
60
+ (0...3).each do |p|
61
+ n = 62 ** p
62
+ (0...62).each do |i|
63
+ encode = (i+n).alphadecimal
64
+ decode = encode.alphadecimal
65
+ assert_equal i+n, decode, "interger #{i+n} was encoded as #{encode} and was decoded to #{decode}"
66
+ end
67
+ end
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: JackDanger-alphadecimal
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Mondragon
8
+ - Jack Danger Canty
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-03-19 00:00:00 -07:00
14
+ default_executable: alphadecimal
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: hoe
18
+ type: :development
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 1.11.0
25
+ version:
26
+ description: Convert integers to base62 strings (A-Za-z0-9) and back. A handy way to shorten long numbers.
27
+ email:
28
+ - mikemondragon@gmail.com
29
+ - rubygems@6brand.com
30
+ executables:
31
+ - alphadecimal
32
+ extensions: []
33
+
34
+ extra_rdoc_files:
35
+ - History.txt
36
+ - Manifest.txt
37
+ - README.txt
38
+ files:
39
+ - History.txt
40
+ - Manifest.txt
41
+ - README.txt
42
+ - Rakefile
43
+ - alphadecimal.gemspec
44
+ - bin/alphadecimal
45
+ - init.rb
46
+ - lib/alphadecimal.rb
47
+ - rails/init.rb
48
+ - test/test_alphadecimal.rb
49
+ has_rdoc: true
50
+ homepage: http://github.com/JackDanger/alphadecimal
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --main
54
+ - README.txt
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project: alphadecimal
72
+ rubygems_version: 1.2.0
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: Convert integers to base62 strings (A-Za-z0-9) and back
76
+ test_files:
77
+ - test/test_alphadecimal.rb