Skip to content

Commit 1866f53

Browse files
committed
Merge pull request #14 from microservices/update_all_the_things
Update all the things
2 parents ec07db7 + bd24237 commit 1866f53

File tree

11 files changed

+202
-220
lines changed

11 files changed

+202
-220
lines changed

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--color

.travis.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: ruby
2+
sudo: false
3+
cache: bundler
24
rvm:
3-
- 1.9.3
4-
- 2.0.0
5-
- jruby-19mode
5+
- 2.1
6+
- 2.2
67
script: bundle exec rake

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
source "https://rubygems.org"
1+
source 'https://rubygems.org'
22

33
# Specify your gem's dependencies in test.gemspec
44
gemspec

Rakefile

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ require 'rspec'
1515
require 'rspec/core/rake_task'
1616

1717
desc 'Default: run specs.'
18-
task :default => :spec
18+
task default: :spec
1919

2020
RSpec::Core::RakeTask.new do |t|
21-
if ENV['COVERAGE'] and RUBY_VERSION =~ /^1.8/
21+
if ENV['COVERAGE'] && RUBY_VERSION =~ /^1.8/
2222
t.rcov = true
2323
t.rcov_opts = ['--exclude', 'spec', '--exclude', 'gems']
2424
end
@@ -32,14 +32,13 @@ begin
3232
doc_destination = File.join(project_root, 'doc')
3333

3434
YARD::Rake::YardocTask.new(:doc) do |yt|
35-
yt.files = Dir.glob(File.join(project_root, 'lib', '**', '*.rb')) +
36-
[ File.join(project_root, 'README.md') ]
35+
yt.files = Dir.glob(File.join(project_root, 'lib', '**', '*.rb')) +
36+
[File.join(project_root, 'README.md')]
3737
yt.options = ['--output-dir', doc_destination, '--readme', 'README.md']
3838
end
3939
rescue LoadError
40-
desc "Generate YARD Documentation"
40+
desc 'Generate YARD Documentation'
4141
task :doc do
42-
abort "Please install the YARD gem to generate rdoc."
42+
abort 'Please install the YARD gem to generate rdoc.'
4343
end
4444
end
45-

lib/noid.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
require "noid/version"
2-
require "noid/minter"
3-
require "noid/template"
1+
require 'noid/version'
2+
require 'noid/minter'
3+
require 'noid/template'
44

55
module Noid
6-
XDIGIT = ['0','1','2','3','4','5','6','7','8','9','b','c','d','f','g','h','j','k','m','n','p','q','r','s','t','v','w','x','z']
7-
MAX_COUNTERS = 293
6+
XDIGIT = %w(0 1 2 3 4 5 6 7 8 9 b c d f g h j k m n p q r s t v w x z)
7+
MAX_COUNTERS = 293
88
end

lib/noid/minter.rb

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
module Noid
22
class Minter
3-
attr_reader :seed, :seq
3+
attr_reader :seed, :seq, :template
44
attr_writer :counters
55

6-
def initialize options = {}
7-
6+
def initialize(options = {})
87
if options[:state]
98
seed(options[:seed])
109
@seq = options[:seq]
@@ -19,44 +18,34 @@ def initialize options = {}
1918
@after_mint = options[:after_mint]
2019
end
2120
@template ||= Noid::Template.new(@template_string)
22-
end
21+
end
2322

2423
##
2524
# Mint a new identifier
2625
def mint
2726
n = next_in_sequence
2827
id = template.mint(n)
29-
if @after_mint
30-
@after_mint.call(self, id)
31-
end
28+
@after_mint.call(self, id) if @after_mint
3229
id
3330
end
3431

35-
##
36-
# Noid identifier template
37-
#
38-
# @return Noid::Template
39-
def template
40-
@template
41-
end
42-
4332
##
4433
# Is the identifier valid under the template string and checksum?
4534
# @param [String] id
4635
# @return bool
47-
def valid? id
48-
prefix = @template.prefix.empty? ? '' : id[0..@template.prefix.length-1]
36+
def valid?(id)
37+
prefix = @template.prefix.empty? ? '' : id[0..@template.prefix.length - 1]
4938
ch = @template.prefix.empty? ? id.split('') : id[@template.prefix.length..-1].split('')
5039
check = ch.pop if @template.checkdigit?
5140
return false unless prefix == @template.prefix
5241

5342
return false unless @template.characters.length == ch.length
5443
@template.characters.split('').each_with_index do |c, i|
55-
return false unless Noid::XDIGIT.include? ch[i]
56-
return false if c == 'd' and ch[i] =~ /[^\d]/
44+
return false unless Noid::XDIGIT.include? ch[i]
45+
return false if c == 'd' && ch[i] =~ /[^\d]/
5746
end
5847

59-
return false unless check.nil? or check == @template.checkdigit(id[0..-2])
48+
return false unless check.nil? || check == @template.checkdigit(id[0..-2])
6049

6150
true
6251
end
@@ -66,9 +55,9 @@ def valid? id
6655
# @param [Integer] seed
6756
# @param [Integer] seq
6857
# @return [Random]
69-
def seed seed = nil, seq = 0
58+
def seed(seed = nil, seq = 0)
7059
@rand = ::Random.new(seed) if seed
71-
@rand ||= ::Random.new
60+
@rand ||= ::Random.new
7261
@seed = @rand.seed
7362
@seq = seq || 0
7463

@@ -81,14 +70,14 @@ def next_in_sequence
8170
n = @seq
8271
@seq += 1
8372
case template.generator
84-
when 'r'
85-
n = next_random
73+
when 'r'
74+
n = next_random
8675
end
8776
n
8877
end
8978

9079
def next_random
91-
raise Exception("Exhausted noid sequence pool") if counters.size == 0
80+
raise 'Exhausted noid sequence pool' if counters.size == 0
9281
i = @rand.rand(counters.size)
9382
n = counters[i][:value]
9483
counters[i][:value] += 1
@@ -100,7 +89,7 @@ def next_random
10089
# Counters to use for quasi-random NOID sequences
10190
def counters
10291
return @counters if @counters
103-
return [] unless template.generator == "r"
92+
return [] unless template.generator == 'r'
10493

10594
percounter = template.max / (@max_counters || Noid::MAX_COUNTERS) + 1
10695
t = 0
@@ -120,7 +109,7 @@ def counters
120109
end
121110

122111
def dump
123-
{ :state => true, :seq => @seq, :seed => @seed, :template => template.template, :counters => Marshal.load(Marshal.dump(counters)) }
112+
{ state: true, seq: @seq, seed: @seed, template: template.template, counters: Marshal.load(Marshal.dump(counters)) }
124113
end
125114
end
126115
end

lib/noid/template.rb

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ module Noid
22
class Template
33
attr_reader :template
44

5-
# @param [String] template A Template is a coded string of the form Prefix.Mask that governs how identifiers will be minted.
6-
def initialize template
7-
@template = template
5+
# @param [String] template A Template is a coded string of the form Prefix.Mask that governs how identifiers will be minted.
6+
def initialize(template)
7+
@template = template
88
end
99

10-
def mint n
10+
def mint(n)
1111
str = prefix
1212
str += n2xdig(n)
1313
str += checkdigit(str) if checkdigit?
1414

1515
str
1616
end
1717

18-
def valid? str
18+
def valid?(str)
1919
return false unless str[0..prefix.length] == prefix
2020

2121
if generator == 'z'
@@ -26,12 +26,12 @@ def valid? str
2626

2727
characters.split('').each_with_index do |c, i|
2828
case c
29-
when 'e'
30-
return false unless Noid::XDIGIT.include? str[prefix.length + i]
31-
when 'd'
32-
return false unless str[prefix.length + i] =~ /\d/
29+
when 'e'
30+
return false unless Noid::XDIGIT.include? str[prefix.length + i]
31+
when 'd'
32+
return false unless str[prefix.length + i] =~ /\d/
3333
end
34-
end
34+
end
3535

3636
return false unless checkdigit(str[0..-2]) == str.split('').last if checkdigit?
3737

@@ -53,18 +53,18 @@ def mask
5353
##
5454
# generator type to use: r, s, z
5555
def generator
56-
@generator ||= mask[0..0]
56+
@generator ||= mask[0..0]
5757
end
5858

5959
##
6060
# sequence pattern: e (extended), d (digit)
6161
def characters
6262
@characters ||= begin
63-
if checkdigit?
64-
mask[1..-2]
65-
else
66-
mask[1..-1]
67-
end
63+
if checkdigit?
64+
mask[1..-2]
65+
else
66+
mask[1..-1]
67+
end
6868
end
6969
end
7070

@@ -78,8 +78,8 @@ def checkdigit?
7878
# calculate a checkdigit for the str
7979
# @param [String] str
8080
# @return [String] checkdigit
81-
def checkdigit str
82-
Noid::XDIGIT[str.split('').map { |x| Noid::XDIGIT.index(x).to_i }.each_with_index.map { |n, idx| n*(idx+1) }.inject { |sum, n| sum += n } % Noid::XDIGIT.length ]
81+
def checkdigit(str)
82+
Noid::XDIGIT[str.split('').map { |x| Noid::XDIGIT.index(x).to_i }.each_with_index.map { |n, idx| n * (idx + 1) }.inject { |sum, n| sum + n } % Noid::XDIGIT.length]
8383
end
8484

8585
##
@@ -93,52 +93,51 @@ def min
9393
def max
9494
@max ||= begin
9595
case generator
96-
when 'z'
97-
nil
98-
else
99-
characters.split('').map { |x| character_space(x) }.compact.inject(1) { |total, x| total *= x }
96+
when 'z'
97+
nil
98+
else
99+
characters.split('').map { |x| character_space(x) }.compact.inject(1) { |total, x| total * x }
100100
end
101101
end
102102
end
103103

104-
105104
protected
105+
106106
##
107107
# total size of a given template character value
108108
# @param [String] c
109-
def character_space c
109+
def character_space(c)
110110
case c
111-
when 'e'
112-
Noid::XDIGIT.length
113-
when 'd'
114-
10
111+
when 'e'
112+
Noid::XDIGIT.length
113+
when 'd'
114+
10
115115
end
116116
end
117117

118118
##
119119
# convert a minter position to a noid string under this template
120120
# @param [Integer] n
121121
# @return [String]
122-
def n2xdig n
122+
def n2xdig(n)
123123
xdig = characters.reverse.split('').map do |c|
124124
value = n % character_space(c)
125-
n = n / character_space(c)
125+
n /= character_space(c)
126126
Noid::XDIGIT[value]
127127
end.compact.join('')
128128

129129
if generator == 'z'
130130
c = characters.split('').last
131131
while n > 0
132132
value = n % character_space(c)
133-
n = n / character_space(c)
133+
n /= character_space(c)
134134
xdig += Noid::XDIGIT[value]
135135
end
136136
end
137137

138-
raise Exception if n > 0
138+
raise 'Exhausted noid sequence pool' if n > 0
139139

140140
xdig.reverse
141141
end
142-
143142
end
144143
end

lib/noid/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ def self.version
44
@version ||= File.read(File.join(File.dirname(__FILE__), '..', '..', 'VERSION')).chomp
55
end
66

7-
VERSION = self.version
7+
VERSION = version
88
end
99
end

noid.gemspec

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
# -*- encoding: utf-8 -*-
2-
$:.push File.expand_path("../lib", __FILE__)
3-
require "noid/version"
2+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
3+
require 'noid/version'
44

55
Gem::Specification.new do |s|
6-
s.name = "noid"
6+
s.name = 'noid'
77
s.version = Noid::VERSION
8-
s.authors = ["Chris Beer"]
9-
s.email = ["[email protected]"]
10-
s.homepage = "http://github.com/microservices/noid"
11-
s.summary = %q{Nice Opaque Identifier}
12-
s.description = %q{Nice Opaque Identifier}
13-
s.licenses = ["MIT"]
8+
s.authors = ['Chris Beer']
9+
s.email = ['[email protected]']
10+
s.homepage = 'http://github.com/microservices/noid'
11+
s.summary = 'Nice Opaque Identifier'
12+
s.description = 'Nice Opaque Identifier'
13+
s.licenses = ['MIT']
1414

15-
s.rubyforge_project = "noid"
15+
s.rubyforge_project = 'noid'
1616

1717
s.files = `git ls-files`.split("\n")
1818
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20-
s.require_paths = ["lib"]
19+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
20+
s.require_paths = ['lib']
2121

2222
s.required_ruby_version = '>= 1.9.3'
2323

24-
s.add_development_dependency "bundler"
25-
s.add_development_dependency "rake"
26-
s.add_development_dependency "rspec", ">= 2.0"
24+
s.add_development_dependency 'bundler'
25+
s.add_development_dependency 'rake'
26+
s.add_development_dependency 'rspec', '>= 3.0'
2727
end

0 commit comments

Comments
 (0)