typable_map 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in typable_map.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'minitest' do
5
+ # with Minitest::Unit
6
+ watch(%r|^test/(.*)\/?test_(.*)\.rb|)
7
+ watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
8
+ watch(%r|^test/test_helper\.rb|) { "test" }
9
+
10
+ # with Minitest::Spec
11
+ watch(%r|^spec/(.*)_spec\.rb|)
12
+ watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
13
+ watch(%r|^spec/spec_helper\.rb|) { "spec" }
14
+
15
+ # Rails 3.2
16
+ # watch(%r|^app/controllers/(.*)\.rb|) { |m| "test/controllers/#{m[1]}_test.rb" }
17
+ # watch(%r|^app/helpers/(.*)\.rb|) { |m| "test/helpers/#{m[1]}_test.rb" }
18
+ # watch(%r|^app/models/(.*)\.rb|) { |m| "test/unit/#{m[1]}_test.rb" }
19
+
20
+ # Rails
21
+ # watch(%r|^app/controllers/(.*)\.rb|) { |m| "test/functional/#{m[1]}_test.rb" }
22
+ # watch(%r|^app/helpers/(.*)\.rb|) { |m| "test/helpers/#{m[1]}_test.rb" }
23
+ # watch(%r|^app/models/(.*)\.rb|) { |m| "test/unit/#{m[1]}_test.rb" }
24
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Yusaku ONO
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.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # TypableMap
2
+
3
+ TypableMap assigns a uniq string to an object and fetches the object by the string.
4
+
5
+ It's inspired by TweetIrcGateway and FacebookIrcGateway.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'typable_map'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install typable_map
20
+
21
+ ## Usage
22
+
23
+ require 'typable_map'
24
+
25
+ typable_map = TypableMap::TypableMap.new(max_size: 1000, shuffle: true)
26
+
27
+ # assign a uniq string to an object
28
+
29
+ obj1 = Object.new
30
+ uniq_string1 = typable_map.push(obj1)
31
+ puts uniq_string1 # => 'vowi'
32
+
33
+ obj2 = Object.new
34
+ uniq_string2 = typable_map.push(obj2)
35
+ puts uniq_string2 # => 'lizo'
36
+
37
+ # fetch an object
38
+
39
+ obj1 = typable_map[uniq_string1]
40
+
41
+ obj2 = typable_map[uniq_string2]
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake"
3
+ require "rake/testtask"
4
+
5
+ task :default => [:test]
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.pattern = 'spec/*_spec.rb'
9
+ t.libs.push 'spec'
10
+ t.libs.push 'lib'
11
+ end
@@ -0,0 +1,46 @@
1
+
2
+ # https://github.com/shunirr/FacebookIrcGateway/blob/master/lib/facebook_irc_gateway/typable_map.rb
3
+
4
+ require "typable_map/version"
5
+ require "typable_map/counter"
6
+
7
+ module TypableMap
8
+ class TypableMap < Hash
9
+ Roman = %w[
10
+ a i u e o ka ki ku ke ko sa si su se so
11
+ ta ti tu te to na ni nu ne no ha hi fu he ho
12
+ ma mi mu me mo ya yu yo ra ri ru re ro
13
+ wa wo
14
+ ga gi gu ge go za ji zu ze zo da de do
15
+ ba bi bu be bo pa pi pu pe po
16
+ ].freeze
17
+
18
+ def initialize(opts = {})
19
+ @seq = Roman.dup
20
+ @seq.shuffle! if opts.fetch(:shuffle, nil)
21
+ @seq.freeze
22
+ @counter = Counter.new(opts.fetch(:max_size, @seq.size))
23
+ end
24
+
25
+ def push(obj)
26
+ id = generate(@counter.next)
27
+ self[id] = obj
28
+ id
29
+ end
30
+ alias :<< :push
31
+
32
+ private
33
+
34
+ def generate(n)
35
+ ret = []
36
+ begin
37
+ n, r = n.divmod(@seq.size)
38
+ ret << @seq[r]
39
+ end while n > 0
40
+ ret.reverse.join
41
+ end
42
+
43
+ :[]=
44
+
45
+ end
46
+ end
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding:utf-8 -*-
3
+
4
+ module TypableMap
5
+ class Counter
6
+ def initialize(max_count = 100)
7
+ @n = 0
8
+ @max_count = max_count
9
+ end
10
+
11
+ def next
12
+ retval = @n
13
+ @n += 1
14
+ @n = @n % @max_count
15
+ retval
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module TypableMap
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding:utf-8 -*-
3
+
4
+ require 'spec_helper'
5
+
6
+ describe :Counter do
7
+ describe 'when use default args' do
8
+ it 'should return zero first' do
9
+ counter = TypableMap::Counter.new
10
+ counter.next.must_equal 0
11
+ end
12
+
13
+ it 'should return zero after count one hundred' do
14
+ counter = TypableMap::Counter.new
15
+ 100.times do
16
+ counter.next
17
+ end
18
+ counter.next.must_equal 0
19
+ end
20
+ end
21
+
22
+ describe 'when use args' do
23
+ it 'should retern zero first' do
24
+ counter = TypableMap::Counter.new(3000)
25
+ counter.next.must_equal 0
26
+ end
27
+
28
+ it 'should retern zero after count args val' do
29
+ max_count = 3000
30
+ counter = TypableMap::Counter.new(max_count)
31
+ max_count.times do
32
+ counter.next
33
+ end
34
+ counter.next.must_equal 0
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding:utf-8 -*-
3
+
4
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
5
+
6
+ require 'rubygems'
7
+ require 'bundler/setup'
8
+
9
+ require 'minitest/unit'
10
+ require 'minitest/autorun'
11
+
12
+ require 'typable_map'
13
+ require 'typable_map/counter'
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding:utf-8 -*-
3
+
4
+ require 'spec_helper'
5
+
6
+ describe TypableMap do
7
+ before do
8
+ @typable_map = TypableMap::TypableMap.new(max_size: 1000, shuffle: true)
9
+ end
10
+
11
+ describe 'when use default args' do
12
+ it 'should initialize' do
13
+ typable_map = TypableMap::TypableMap.new
14
+ uniq_str = typable_map.push(Object.new)
15
+ uniq_str.wont_be_nil
16
+ end
17
+
18
+ it 'should generate string "a" first' do
19
+ typable_map = TypableMap::TypableMap.new
20
+ uniq_str = typable_map.push(Object.new)
21
+ uniq_str.must_equal "a"
22
+ end
23
+ end
24
+
25
+ describe 'when use max_size args' do
26
+ it 'should initialize' do
27
+ typable_map = TypableMap::TypableMap.new(max_size: 1000)
28
+ uniq_str = typable_map.push(Object.new)
29
+ uniq_str.wont_be_nil
30
+ end
31
+
32
+ it 'should generate string "a" first' do
33
+ typable_map = TypableMap::TypableMap.new
34
+ uniq_str = typable_map.push(Object.new)
35
+ uniq_str.must_equal "a"
36
+ end
37
+ end
38
+
39
+ describe 'when use shuffle args' do
40
+ it 'should initialize' do
41
+ typable_map = TypableMap::TypableMap.new(shuffle: true)
42
+ uniq_str = typable_map.push(Object.new)
43
+ uniq_str.wont_be_nil
44
+ end
45
+ end
46
+
47
+ describe 'when push object' do
48
+ it 'should generate uniq string' do
49
+ inserted_obj = Object.new
50
+ uniq_str = @typable_map.push(inserted_obj)
51
+ uniq_str.wont_equal "aaaa"
52
+ end
53
+ end
54
+
55
+ describe 'when pull object' do
56
+ it 'should retern the object' do
57
+ inserted_obj = Object.new
58
+ uniq_str = @typable_map.push(inserted_obj)
59
+
60
+ fetched_obj = @typable_map[uniq_str]
61
+ fetched_obj.must_equal inserted_obj
62
+ end
63
+ end
64
+
65
+ describe 'when push a object many times' do
66
+ it 'should generate uniq strings' do
67
+ before_str = ''
68
+ 1000.times do
69
+ inserted_obj = Object.new
70
+ uniq_str = @typable_map.push(inserted_obj)
71
+ uniq_str.wont_equal before_str
72
+ before_str = uniq_str
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'typable_map/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "typable_map"
8
+ gem.version = TypableMap::VERSION
9
+ gem.authors = ["Yusaku ONO"]
10
+ gem.email = ["yono.05@gmail.com"]
11
+ gem.description = %q{TypableMap library inspired by TweetIrcGateway and FacebookIrcGateway}
12
+ gem.summary = %q{TypableMap library inspired by TweetIrcGateway and FacebookIrcGateway}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'guard-minitest'
21
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: typable_map
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Yusaku ONO
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: guard-minitest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: TypableMap library inspired by TweetIrcGateway and FacebookIrcGateway
31
+ email:
32
+ - yono.05@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - Guardfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/typable_map.rb
44
+ - lib/typable_map/counter.rb
45
+ - lib/typable_map/version.rb
46
+ - spec/counter_spec.rb
47
+ - spec/spec_helper.rb
48
+ - spec/typable_map_spec.rb
49
+ - typable_map.gemspec
50
+ homepage: ''
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.23
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: TypableMap library inspired by TweetIrcGateway and FacebookIrcGateway
74
+ test_files:
75
+ - spec/counter_spec.rb
76
+ - spec/spec_helper.rb
77
+ - spec/typable_map_spec.rb