-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from sealink/refactoring
Refactoring
- Loading branch information
Showing
14 changed files
with
125 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
source 'https://rubygems.org' | ||
|
||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
require 'bundler/gem_tasks' | ||
require 'rspec/core/rake_task' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require 'bundler/setup' | ||
require 'humanized_id' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,37 @@ | ||
# frozen_string_literal: true | ||
module HumanizedId | ||
class Humanizer | ||
SOURCE_CHARSET = %w(0123456789abcdefghijklmnopqrstuvwxyz).freeze | ||
SOURCE_CHARSET = (('0'..'9').to_a + ('a'..'z').to_a).freeze | ||
|
||
def initialize(id:, min_length: nil, prefix: '') | ||
@id = id | ||
@min_length = min_length | ||
@prefix = prefix.nil? ? '' : prefix | ||
@target_charset = HumanizedId::CHARACTERSET.join('') | ||
@source_charset = SOURCE_CHARSET.join('') | ||
@prefix = prefix.to_s | ||
@target_charset = CHARSET.join | ||
@source_charset = SOURCE_CHARSET.join | ||
end | ||
|
||
def generate_humanized_id | ||
new_id = convert_to_target_base id: @id | ||
new_id = convert_to_target_charset id: new_id | ||
new_id = resize id: new_id if @min_length | ||
new_id = resize convert_to_target_charset convert_to_target_base(@id) | ||
"#{@prefix}#{new_id}" | ||
end | ||
|
||
private | ||
|
||
def convert_to_target_base(id: @id, target_base: @target_charset.length) | ||
id.to_s(target_base) | ||
rescue ArgumentError | ||
raise Error, 'id is not an integer' | ||
def convert_to_target_base(id) | ||
fail Error, 'id is not an integer' unless id.is_a? Integer | ||
id.to_s(@target_charset.length) | ||
end | ||
|
||
def convert_to_target_charset( | ||
id: @id, target_charset: @target_charset, source_charset: @source_charset) | ||
source_charset_subset = source_charset.slice(0, target_charset.length) | ||
id.tr(source_charset_subset, target_charset) | ||
def convert_to_target_charset(id) | ||
source_charset_subset = @source_charset.slice(0, @target_charset.length) | ||
id.tr(source_charset_subset, @target_charset) | ||
end | ||
|
||
def resize(id: @id, min_length: @min_length, target_charset: @target_charset) | ||
if min_length > id.length | ||
padding = target_charset[0] * (min_length - id.length) | ||
id = "#{padding}#{id}" | ||
end | ||
id | ||
def resize(id) | ||
return id if @min_length.nil? || @min_length <= id.length | ||
padding = @target_charset[0] * (@min_length - id.length) | ||
"#{padding}#{id}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
require 'spec_helper' | ||
|
||
describe HumanizedId::Humanizer do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,83 @@ | ||
# frozen_string_literal: true | ||
require 'spec_helper' | ||
|
||
describe HumanizedId do | ||
let(:expected_params) { params } | ||
|
||
context 'when calling humanize' do | ||
let(:params) { { id: 1_234_567 } } | ||
let(:expected_id) { '6RDFD' } | ||
let(:humanized_id) { HumanizedId.humanize params } | ||
let(:humanize) { HumanizedId.humanize params } | ||
|
||
context 'with minimum required values' do | ||
it 'should return expected id' do | ||
expect(humanized_id).to eq expected_id | ||
let(:humanizer_instance) { double } | ||
let(:humanizer) { double } | ||
|
||
before do | ||
allow(humanizer).to receive(:new).and_return(humanizer_instance) | ||
allow(humanizer_instance).to receive(:generate_humanized_id) | ||
stub_const('HumanizedId::Humanizer', humanizer) | ||
end | ||
|
||
shared_examples 'a delegated call to humanizer' do | ||
before do | ||
humanize | ||
end | ||
|
||
specify do | ||
expect(humanizer).to have_received(:new).with(expected_params) | ||
end | ||
specify do | ||
expect(humanizer_instance).to have_received(:generate_humanized_id).with(no_args) | ||
end | ||
end | ||
|
||
context 'with minimum required values' do | ||
let(:expected_params) { super().merge(min_length: nil, prefix: '') } | ||
it_behaves_like 'a delegated call to humanizer' | ||
end | ||
|
||
context 'with all params passed in' do | ||
let(:params) { | ||
{ | ||
id: 1_234_567, | ||
min_length: 20, | ||
prefix: 'test' | ||
} | ||
} | ||
it 'should humanize id with appropriate min_length and prefix' do | ||
expect(humanized_id).to eq(params[:prefix] + ('2' * 15) + expected_id) | ||
end | ||
let(:params) { super().merge(min_length: 20, prefix: 'test') } | ||
it_behaves_like 'a delegated call to humanizer' | ||
end | ||
end | ||
|
||
context 'when calling generate_random' do | ||
let(:params) { {} } | ||
let(:random_id) { HumanizedId.generate_random params } | ||
let(:generate_random) { HumanizedId.generate_random params } | ||
|
||
context 'with minimum required values' do | ||
it 'should produce random id of default length' do | ||
expect(random_id.length).to eq HumanizedId::DEFAULT_GENERATION_LENGTH | ||
let(:rand_generator_instance) { double } | ||
let(:rand_generator) { double } | ||
|
||
before do | ||
allow(rand_generator).to receive(:new).and_return(rand_generator_instance) | ||
allow(rand_generator_instance).to receive(:generate_random_humanized_id) | ||
stub_const('HumanizedId::RandGenerator', rand_generator) | ||
end | ||
|
||
shared_examples 'a delegated call to rand generator' do | ||
before do | ||
generate_random | ||
end | ||
|
||
specify do | ||
expect(rand_generator).to have_received(:new).with(expected_params) | ||
end | ||
specify do | ||
expect(rand_generator_instance) | ||
.to have_received(:generate_random_humanized_id).with(no_args) | ||
end | ||
end | ||
|
||
context 'with all values passed in' do | ||
let(:params) { | ||
{ | ||
prefix: 'test', | ||
length: 3 | ||
} | ||
context 'with minimum required values' do | ||
let(:expected_params) { | ||
super().merge(prefix: '', length: HumanizedId::DEFAULT_GENERATION_LENGTH) | ||
} | ||
it 'should produce random id with requested length and prefix' do | ||
expect(random_id.length).to eq(params[:prefix].length + params[:length]) | ||
expect(random_id[0..(params[:prefix].length - 1)]).to eq params[:prefix] | ||
expect(random_id).to match(/#{params[:prefix]}#{HumanizedId::CHARACTERSET}{3}/) | ||
end | ||
it_behaves_like 'a delegated call to rand generator' | ||
end | ||
|
||
context 'with all values passed in' do | ||
let(:params) { super().merge(prefix: 'test', length: 3) } | ||
it_behaves_like 'a delegated call to rand generator' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__) | ||
require 'support/coverage_loader' | ||
require 'humanized_id' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
MINIMUM_COVERAGE = 80 | ||
|
||
unless ENV['COVERAGE'] == 'off' | ||
|