valerii 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ pkg
2
+ .DS_Store
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ariejan de Vroom
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,39 @@
1
+ = valerii
2
+
3
+ Convert integers to short strings and back again.
4
+
5
+ == Installation
6
+
7
+ You'll need Gemcutter, then just install valerii.
8
+
9
+ $ sudo gem install gemcutter && gem tumble
10
+ $ sudo gem install valerii
11
+
12
+ or to use in your Rails project, add to your config/environment.rb
13
+
14
+ config.gem 'valerii', :lib => 'valerii'
15
+
16
+
17
+ == Example
18
+
19
+ #!/usr/bin/env ruby
20
+
21
+ require 'valerii'
22
+
23
+ Valerii.encode(1234) # => "16J"
24
+ Valerii.decode("16J") # => 1234
25
+
26
+ == Note on Patches/Pull Requests
27
+
28
+ * Fork the project.
29
+ * Make your feature addition or bug fix.
30
+ * Add tests for it. This is important so I don't break it in a
31
+ future version unintentionally.
32
+ * Commit, do not mess with rakefile, version, or history.
33
+ (if you want to have your own version, that is fine but
34
+ bump version in a commit by itself I can ignore when I pull)
35
+ * Send me a pull request. Bonus points for topic branches.
36
+
37
+ == Copyright
38
+
39
+ Copyright (c) 2009 Ariejan de Vroom. See LICENSE for details.
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "valerii"
8
+ gem.summary = %Q{Convert integers to short strings and vice versa.}
9
+ gem.description = %Q{Convert integers to short strings and vice versa.}
10
+ gem.email = "ariejan@ariejan.net"
11
+ gem.homepage = "http://github.com/ariejan/valerii"
12
+ gem.authors = ["Ariejan de Vroom"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "valerii #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.1.0
@@ -0,0 +1,36 @@
1
+ require 'enumerator'
2
+
3
+ class Valerii
4
+
5
+ ENCODE_CHARS =
6
+ %w( B C D F G H J K
7
+ M N P Q R S T V
8
+ W Z b c d f h j
9
+ k m n p r x t v )
10
+
11
+ DECODE_MAP = ENCODE_CHARS.to_enum(:each_with_index).inject({}) do |h,(c,i)|
12
+ h[c] = i; h
13
+ end
14
+
15
+ # Encodes an integer into a string
16
+ #
17
+ # Valerii.encode(1234) # => "KJ"
18
+ #
19
+ def self.encode(number, opts = {})
20
+ str = number.to_s(2).reverse.scan(/.{1,5}/).map do |bits|
21
+ ENCODE_CHARS[bits.reverse.to_i(2)]
22
+ end.reverse.join
23
+
24
+ return str
25
+ end
26
+
27
+ # Decode a string to an integer
28
+ #
29
+ # Valerii.decode("KJ") # => 1234
30
+ #
31
+ def self.decode(string)
32
+ string.split(//).map { |char|
33
+ DECODE_MAP[char] or return nil
34
+ }.inject(0) { |result,val| (result << 5) + val }
35
+ end
36
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'valerii'
7
+
8
+ class Test::Unit::TestCase
9
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class ValeriiTest < Test::Unit::TestCase
4
+
5
+ def test_encoding_and_decoding_single_chars
6
+ from = (0..31).to_a
7
+ to = %w( B C D F G H J K M N P Q R S T V W Z b c d f h j k m n p r x t v )
8
+
9
+ from.zip(to) do |symbol_value, encode_symbol|
10
+ assert_equal encode_symbol, Valerii.encode(symbol_value)
11
+ assert_equal symbol_value, Valerii.decode(encode_symbol)
12
+ end
13
+ end
14
+
15
+ def test_encoding_larger_numbers
16
+ assert_equal("CB", Valerii.encode(32))
17
+ assert_equal("CJb", Valerii.encode(1234))
18
+ assert_equal("GbNfdbmNWprxRHbVCJGmkHvKDhTMDKDDJcrfPmGJDHMHnKZBBBBBBBBBBBBBBBBBBBB", Valerii.encode(10**100))
19
+ end
20
+
21
+ def test_decoding_strings
22
+ assert_equal(32, Valerii.decode("CB"))
23
+ assert_equal(1234, Valerii.decode("CJb"))
24
+ assert_equal(10**100, Valerii.decode("GbNfdbmNWprxRHbVCJGmkHvKDhTMDKDDJcrfPmGJDHMHnKZBBBBBBBBBBBBBBBBBBBB"))
25
+ end
26
+
27
+ end
@@ -0,0 +1,49 @@
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{valerii}
8
+ s.version = "1.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ariejan de Vroom"]
12
+ s.date = %q{2009-10-13}
13
+ s.description = %q{Convert integers to short strings and vice versa.}
14
+ s.email = %q{ariejan@ariejan.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/valerii.rb",
26
+ "test/test_helper.rb",
27
+ "test/valerii_test.rb",
28
+ "valerii.gemspec"
29
+ ]
30
+ s.homepage = %q{http://github.com/ariejan/valerii}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.5}
34
+ s.summary = %q{Convert integers to short strings and vice versa.}
35
+ s.test_files = [
36
+ "test/test_helper.rb",
37
+ "test/valerii_test.rb"
38
+ ]
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
45
+ else
46
+ end
47
+ else
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: valerii
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ariejan de Vroom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-13 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Convert integers to short strings and vice versa.
17
+ email: ariejan@ariejan.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .gitignore
27
+ - LICENSE
28
+ - README.rdoc
29
+ - Rakefile
30
+ - VERSION
31
+ - lib/valerii.rb
32
+ - test/test_helper.rb
33
+ - test/valerii_test.rb
34
+ - valerii.gemspec
35
+ has_rdoc: true
36
+ homepage: http://github.com/ariejan/valerii
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Convert integers to short strings and vice versa.
63
+ test_files:
64
+ - test/test_helper.rb
65
+ - test/valerii_test.rb