midwire_common 0.1.12 → 0.1.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/.rubocop.yml +73 -0
- data/.ruby-version +1 -1
- data/CHANGELOG +5 -0
- data/Gemfile +1 -1
- data/Guardfile +5 -2
- data/README.md +1 -1
- data/Rakefile +3 -3
- data/lib/midwire_common.rb +1 -1
- data/lib/midwire_common/array.rb +8 -4
- data/lib/midwire_common/data_file_cache.rb +1 -1
- data/lib/midwire_common/enumerable.rb +7 -2
- data/lib/midwire_common/file.rb +0 -1
- data/lib/midwire_common/file/stat.rb +2 -2
- data/lib/midwire_common/fixnum.rb +11 -7
- data/lib/midwire_common/float.rb +3 -1
- data/lib/midwire_common/hash.rb +31 -29
- data/lib/midwire_common/rake_helper.rb +9 -5
- data/lib/midwire_common/string.rb +31 -32
- data/lib/midwire_common/time.rb +1 -3
- data/lib/midwire_common/time_tool.rb +13 -14
- data/lib/midwire_common/version.rb +1 -1
- data/lib/tasks/version.rake +97 -85
- data/midwire_common.gemspec +22 -22
- data/spec/lib/midwire_common/array_spec.rb +42 -22
- data/spec/lib/midwire_common/data_file_cache_spec.rb +12 -13
- data/spec/lib/midwire_common/enumerable_spec.rb +4 -3
- data/spec/lib/midwire_common/file/stat_spec.rb +2 -2
- data/spec/lib/midwire_common/fixnum_spec.rb +8 -8
- data/spec/lib/midwire_common/float_spec.rb +3 -3
- data/spec/lib/midwire_common/hash_spec.rb +66 -22
- data/spec/lib/midwire_common/string_spec.rb +32 -32
- data/spec/lib/midwire_common/time_spec.rb +2 -2
- data/spec/lib/midwire_common/time_tool_spec.rb +0 -2
- data/spec/spec_helper.rb +2 -2
- metadata +39 -38
data/midwire_common.gemspec
CHANGED
@@ -1,27 +1,27 @@
|
|
1
|
-
#
|
1
|
+
# coding: utf-8
|
2
2
|
require File.expand_path('../lib/midwire_common/version', __FILE__)
|
3
3
|
|
4
|
-
Gem::Specification.new do |
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = 'midwire_common'
|
6
|
+
spec.version = MidwireCommon::VERSION
|
7
|
+
spec.authors = ['Chris Blackburn']
|
8
|
+
spec.email = ['chris@midwiretech.com']
|
9
|
+
spec.description = 'A useful Ruby library for the Midwire development team'
|
10
|
+
spec.summary = 'Midwire Ruby Library'
|
11
|
+
spec.homepage = 'https://github.com/midwire/midwire_common'
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
gem.require_paths = ["lib"]
|
16
|
-
gem.version = MidwireCommon::VERSION
|
13
|
+
spec.files = `git ls-files -z`.split("\x0")
|
14
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
15
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
16
|
+
spec.require_paths = ['lib']
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
18
|
+
spec.add_development_dependency 'rspec', '~> 2.14'
|
19
|
+
spec.add_development_dependency 'simplecov', '~> 0.7'
|
20
|
+
spec.add_development_dependency 'guard', '~> 2.11'
|
21
|
+
spec.add_development_dependency 'guard-bundler', '~> 2.1'
|
22
|
+
spec.add_development_dependency 'guard-rspec', '~> 4.5'
|
23
|
+
spec.add_development_dependency 'guard-rubocop', '~> 1.2'
|
24
|
+
spec.add_development_dependency 'pry', '~> 0.10'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.1'
|
26
|
+
spec.add_development_dependency 'rubocop', '~> 0.28'
|
27
27
|
end
|
@@ -1,42 +1,62 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Array do
|
4
|
-
it
|
5
|
-
[1,2,2,3,3,3].count_occurrences.should
|
6
|
-
|
4
|
+
it 'counts occurrences of an element' do
|
5
|
+
[1, 2, 2, 3, 3, 3].count_occurrences.should eq(1 => 1, 2 => 2, 3 => 3)
|
6
|
+
%w(asdf asdf qwer asdf).count_occurrences.should == {
|
7
|
+
'asdf' => 3,
|
8
|
+
'qwer' => 1
|
9
|
+
}
|
7
10
|
end
|
8
11
|
|
9
|
-
it
|
10
|
-
myarray = [1,2,3,4,5,6,7,8,9,'0']
|
11
|
-
myarray.randomize.should_not
|
12
|
+
it 'randomizes the order of an array' do
|
13
|
+
myarray = [1, 2, 3, 4, 5, 6, 7, 8, 9, '0']
|
14
|
+
myarray.randomize.should_not eq(myarray)
|
12
15
|
myarray.randomize!
|
13
|
-
myarray.should_not == [1,2,3,4,5,6,7,8,9,'0']
|
16
|
+
myarray.should_not == [1, 2, 3, 4, 5, 6, 7, 8, 9, '0']
|
14
17
|
end
|
15
18
|
|
16
|
-
it
|
19
|
+
it 'sorts elements case insensitive' do
|
17
20
|
myarray = %w(zebra ghost Zebra cat Cat)
|
18
|
-
myarray.sort_case_insensitive.should
|
21
|
+
myarray.sort_case_insensitive.should eq(%w(Cat cat ghost Zebra zebra))
|
19
22
|
end
|
20
23
|
|
21
|
-
it
|
24
|
+
it 'can process first and last entries differently than others' do
|
22
25
|
text = ''
|
23
|
-
[
|
24
|
-
lambda
|
25
|
-
|
26
|
-
|
26
|
+
['KU', 'K-State', 'MU'].each_with_first_last(
|
27
|
+
lambda do |team|
|
28
|
+
text += "#{team} came first in the NCAA basketball tournament.\n"
|
29
|
+
end,
|
30
|
+
lambda do |team|
|
31
|
+
text += "#{team} did not come first or last in the final four.\n"
|
32
|
+
end,
|
33
|
+
lambda do |team|
|
34
|
+
text += "#{team} came last in the final four this year.\n"
|
35
|
+
end
|
27
36
|
)
|
28
|
-
text.should == "
|
37
|
+
text.should == <<-string.here_with_pipe("\n")
|
38
|
+
|KU came first in the NCAA basketball tournament.
|
39
|
+
|K-State did not come first or last in the final four.
|
40
|
+
|MU came last in the final four this year.
|
41
|
+
|
|
42
|
+
string
|
29
43
|
end
|
30
44
|
|
31
|
-
it
|
45
|
+
it 'can binary search for elements' do
|
32
46
|
a = %w(a b c)
|
33
|
-
a.bsearch('a').should
|
34
|
-
a.bsearch('b').should
|
35
|
-
a.bsearch('c').should
|
47
|
+
a.bsearch('a').should eq(0)
|
48
|
+
a.bsearch('b').should eq(1)
|
49
|
+
a.bsearch('c').should eq(2)
|
36
50
|
end
|
37
51
|
|
38
|
-
|
39
|
-
|
40
|
-
[
|
52
|
+
# rubocop:disable Metrics/LineLength
|
53
|
+
it 'can superjoin elements' do
|
54
|
+
[1, 2, 3].superjoin(['->', '+', '<-']).should eq('->1+2+3<-')
|
55
|
+
[[1, 2], [2, 3]].superjoin(
|
56
|
+
%w(<table><tr> </tr><tr> </tr></table>), %w(<td> </td><td> </td>)
|
57
|
+
).should eq(
|
58
|
+
'<table><tr><td>1</td><td>2</td></tr><tr><td>2</td><td>3</td></tr></table>'
|
59
|
+
)
|
41
60
|
end
|
61
|
+
# rubocop:enable Metrics/LineLength
|
42
62
|
end
|
@@ -1,40 +1,39 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe MidwireCommon::DataFileCache do
|
4
|
-
let(:cache_dir) {'/tmp/.cache_dir'}
|
5
|
-
let(:filename) {
|
6
|
-
let(:filepath) {"#{cache_dir}/#{filename}"}
|
7
|
-
let(:data) {'this is my data'}
|
4
|
+
let(:cache_dir) { '/tmp/.cache_dir' }
|
5
|
+
let(:filename) { 'data_file_cache' }
|
6
|
+
let(:filepath) { "#{cache_dir}/#{filename}" }
|
7
|
+
let(:data) { 'this is my data' }
|
8
8
|
|
9
9
|
before(:each) do
|
10
10
|
@cache = MidwireCommon::DataFileCache.new("#{cache_dir}/#{filename}")
|
11
11
|
end
|
12
12
|
|
13
|
-
it
|
14
|
-
File.
|
13
|
+
it 'creates a directory for the cache file' do
|
14
|
+
File.exist?(cache_dir).should eq(true)
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
17
|
+
it 'caches data' do
|
18
18
|
@cache.put(data)
|
19
|
-
File.
|
19
|
+
File.exist?(filepath).should eq(true)
|
20
20
|
x = File.read(filepath)
|
21
21
|
x.should == data
|
22
22
|
end
|
23
23
|
|
24
|
-
it
|
24
|
+
it 'determines the age of the cached data' do
|
25
25
|
tm = @cache.age
|
26
|
-
(tm > 0).should
|
26
|
+
(tm > 0).should eq(true)
|
27
27
|
end
|
28
28
|
|
29
|
-
it
|
29
|
+
it 'retrieves cached data' do
|
30
30
|
dta = @cache.get
|
31
31
|
dta.should == data
|
32
32
|
end
|
33
33
|
|
34
|
-
it
|
34
|
+
it 'prepends the filename with cache dir' do
|
35
35
|
testfile = 'testfile'
|
36
36
|
nf = @cache.send(:normalize_filename, testfile)
|
37
37
|
nf.should == "#{cache_dir}/#{testfile}"
|
38
38
|
end
|
39
|
-
|
40
39
|
end
|
@@ -1,8 +1,9 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Enumerable do
|
4
|
-
it
|
5
|
-
[1,2,3,3,3,3,2].sort_by_frequency.should
|
6
|
-
%w(a b c d e f a f f b f a).sort_by_frequency
|
4
|
+
it 'can sort by frequency of occurrences' do
|
5
|
+
[1, 2, 3, 3, 3, 3, 2].sort_by_frequency.should eq([3, 3, 3, 3, 2, 2, 1])
|
6
|
+
%w(a b c d e f a f f b f a).sort_by_frequency
|
7
|
+
.should eq(%w(f f f f a a a b b c d e))
|
7
8
|
end
|
8
9
|
end
|
@@ -1,15 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Fixnum do
|
4
|
-
it
|
5
|
-
777.
|
6
|
-
776.
|
4
|
+
it 'knows if it is odd or even' do
|
5
|
+
777.odd?.should eq(true)
|
6
|
+
776.odd?.should eq(false)
|
7
7
|
|
8
|
-
777.
|
9
|
-
776.
|
8
|
+
777.even?.should eq(false)
|
9
|
+
776.even?.should eq(true)
|
10
10
|
end
|
11
11
|
|
12
|
-
it
|
13
|
-
|
12
|
+
it 'can format itself with commas' do
|
13
|
+
8_729_928_827.commify.should == '8,729,928,827'
|
14
14
|
end
|
15
|
-
end
|
15
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Float do
|
4
|
-
it
|
5
|
-
|
6
|
-
|
4
|
+
it 'can format itself with commas' do
|
5
|
+
8_729_928_827.0.commify.should eq('8,729,928,827.0')
|
6
|
+
8_729_928_827.20332002.commify.should eq('8,729,928,827.20332')
|
7
7
|
end
|
8
8
|
end
|
@@ -1,40 +1,84 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
# rubocop:disable Lint/Void
|
3
4
|
describe Hash do
|
4
|
-
it
|
5
|
-
h = {a:
|
6
|
-
h.grep(/a test/).should == [[:a,
|
7
|
-
h.grep(/b/).should == [['b',
|
8
|
-
h.grep(/^this/).should == [
|
5
|
+
it 'greps key/value pairs using a regular expression' do
|
6
|
+
h = { a: 'this is a test', 'b' => 'this is not the answer' }
|
7
|
+
h.grep(/a test/).should == [[:a, 'this is a test']]
|
8
|
+
h.grep(/b/).should == [['b', 'this is not the answer']]
|
9
|
+
h.grep(/^this/).should == [
|
10
|
+
[:a, 'this is a test'], ['b', 'this is not the answer']
|
11
|
+
]
|
9
12
|
end
|
10
13
|
|
11
|
-
it
|
12
|
-
{a: 1, b: 2, c: 3}.except(:a).should == {b: 2, c: 3}
|
14
|
+
it 'returns elements with certain keys filtered out' do
|
15
|
+
{ a: 1, b: 2, c: 3 }.except(:a).should == { b: 2, c: 3 }
|
13
16
|
end
|
14
17
|
|
15
|
-
it
|
16
|
-
{a: 1, b: 2, c: 3}.only(:a).should == {a: 1}
|
18
|
+
it 'returns elements for discretely passed keys' do
|
19
|
+
{ a: 1, b: 2, c: 3 }.only(:a).should == { a: 1 }
|
17
20
|
end
|
18
21
|
|
19
|
-
it
|
20
|
-
h = {a: 1, b: 2, c: 3}
|
21
|
-
h.pop(:a).should == {a: 1}
|
22
|
-
h.should == {b: 2, c: 3}
|
22
|
+
it 'pops an element off of the stack' do
|
23
|
+
h = { a: 1, b: 2, c: 3 }
|
24
|
+
h.pop(:a).should == { a: 1 }
|
25
|
+
h.should == { b: 2, c: 3 }
|
23
26
|
end
|
24
27
|
|
25
|
-
it
|
26
|
-
{ a: 1, b: 2, c: 3}.to_query_string.should ==
|
28
|
+
it 'returns a query string' do
|
29
|
+
{ a: 1, b: 2, c: 3 }.to_query_string.should == 'a=1&b=2&c=3'
|
27
30
|
end
|
28
31
|
|
29
|
-
it
|
30
|
-
h = {'a'=>1, 'b'=>2, 'c'=>3}
|
31
|
-
h.symbolize_keys.should == {a: 1, b: 2, c: 3}
|
32
|
+
it 'symbolizes its keys' do
|
33
|
+
h = { 'a' => 1, 'b' => 2, 'c' => 3 }
|
34
|
+
h.symbolize_keys.should == { a: 1, b: 2, c: 3 }
|
32
35
|
h.symbolize_keys!
|
33
|
-
h.should == {a: 1, b: 2, c: 3}
|
36
|
+
h.should == { a: 1, b: 2, c: 3 }
|
34
37
|
end
|
35
38
|
|
36
|
-
it
|
37
|
-
h = {
|
38
|
-
|
39
|
+
it 'recursively symbolizes its keys' do
|
40
|
+
h = {
|
41
|
+
'a' => 1,
|
42
|
+
'b' => {
|
43
|
+
'a' => 1,
|
44
|
+
'b' => 2,
|
45
|
+
'c' => {
|
46
|
+
'a' => 1,
|
47
|
+
'b' => 2,
|
48
|
+
'c' => 3
|
49
|
+
}
|
50
|
+
},
|
51
|
+
'c' => 3
|
52
|
+
}
|
53
|
+
h.recursively_symbolize_keys!.should == {
|
54
|
+
a: 1, b: { a: 1, b: 2, c: { a: 1, b: 2, c: 3 } }, c: 3
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'diff methods' do
|
59
|
+
let(:h1_keys) { { 'a' => 1, 'b' => 2, 'c' => 3 } }
|
60
|
+
let(:h2_keys) { { 'a' => 1, 'b' => 2, 'd' => 3 } }
|
61
|
+
|
62
|
+
let(:h1_keys_nested) { { 'a' => 1, 'b' => 2, { 'x' => 99 } => 3 } }
|
63
|
+
let(:h2_keys_nested) { { 'a' => 1, 'b' => 2, { 'x' => 99 } => 4 } }
|
64
|
+
|
65
|
+
let(:h1_values) { { 'a' => 1, 'b' => 2, 'c' => 3 } }
|
66
|
+
let(:h2_values) { { 'a' => 1, 'b' => 2, 'c' => 4 } }
|
67
|
+
|
68
|
+
context '.diff' do
|
69
|
+
it 'reports different keys' do
|
70
|
+
h1_keys.diff(h2_keys).should == { 'c' => [3, nil], 'd' => [nil, 3] }
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'reports different values' do
|
74
|
+
h1_values.diff(h2_values).should == { 'c' => [3, nil], 'c' => [3, 4] }
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'reports different keys even with nested hashes' do
|
78
|
+
h1_keys_nested.diff(h2_keys_nested).should ==
|
79
|
+
{ { 'x' => 99 } => [3, 4] }
|
80
|
+
end
|
81
|
+
end
|
39
82
|
end
|
40
83
|
end
|
84
|
+
# rubocop:enable Lint/Void
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
#
|
3
|
+
# rubocop:disable Metrics/LineLength
|
4
4
|
require 'spec_helper'
|
5
5
|
|
6
6
|
describe String do
|
@@ -44,7 +44,7 @@ describe String do
|
|
44
44
|
end
|
45
45
|
|
46
46
|
it "'trim' removes whitespace from both sides of the string" do
|
47
|
-
" \t \t a test is coming \t \t ".trim.should ==
|
47
|
+
" \t \t a test is coming \t \t ".trim.should == 'a test is coming'
|
48
48
|
end
|
49
49
|
|
50
50
|
it "'trim!' removes whitespace from both sides of the string" do
|
@@ -74,7 +74,7 @@ describe String do
|
|
74
74
|
end
|
75
75
|
|
76
76
|
it 'here_with_pipe - with no space delimeter' do
|
77
|
-
html = <<-STOP.here_with_pipe(
|
77
|
+
html = <<-STOP.here_with_pipe('')
|
78
78
|
|<!-- Begin: comment -->
|
79
79
|
|<script type="text/javascript">
|
80
80
|
|</script>
|
@@ -101,7 +101,6 @@ describe String do
|
|
101
101
|
end
|
102
102
|
|
103
103
|
it 'shortens itself with elipses at the end' do
|
104
|
-
#
|
105
104
|
s = 'this is my very long string which I will eventually shorten with the enhanced String class that we are now testing.'
|
106
105
|
short = s.shorten
|
107
106
|
expect(short).to eq('this is my very long string...')
|
@@ -132,43 +131,44 @@ describe String do
|
|
132
131
|
|
133
132
|
context 'characterization' do
|
134
133
|
it 'knows if it is alpha-numeric or not' do
|
135
|
-
'abcd-9191'.
|
136
|
-
'abcd.9191'.
|
137
|
-
'abcd91910'.
|
138
|
-
'abcd_9191'.
|
139
|
-
'abcd 9191'.
|
134
|
+
'abcd-9191'.alpha_numeric?.should eq(false)
|
135
|
+
'abcd.9191'.alpha_numeric?.should eq(false)
|
136
|
+
'abcd91910'.alpha_numeric?.should eq(true)
|
137
|
+
'abcd_9191'.alpha_numeric?.should eq(false)
|
138
|
+
'abcd 9191'.alpha_numeric?.should eq(false)
|
140
139
|
end
|
141
140
|
|
142
141
|
it 'knows if it is an email address or not' do
|
143
|
-
'abcd_9191'.
|
144
|
-
'abcd@9191'.
|
145
|
-
'abcd@9191.poop'.
|
146
|
-
'abcd@9191.info'.
|
147
|
-
'abcd-asdf@9191.com'.
|
148
|
-
'abcd_asdf@9191.com'.
|
149
|
-
'abcd.asdf@9191.com'.
|
142
|
+
'abcd_9191'.email_address?.should eq(false)
|
143
|
+
'abcd@9191'.email_address?.should eq(false)
|
144
|
+
'abcd@9191.poop'.email_address?.should eq(false)
|
145
|
+
'abcd@9191.info'.email_address?.should eq(true)
|
146
|
+
'abcd-asdf@9191.com'.email_address?.should eq(true)
|
147
|
+
'abcd_asdf@9191.com'.email_address?.should eq(true)
|
148
|
+
'abcd.asdf@9191.com'.email_address?.should eq(true)
|
150
149
|
end
|
151
150
|
|
152
151
|
it 'knows if it is a zipcode or not' do
|
153
|
-
'13922-2356'.
|
154
|
-
'13922.2343'.
|
155
|
-
'13922 2342'.
|
156
|
-
'ABSSD'.
|
157
|
-
'i3323'.
|
158
|
-
'13922'.
|
152
|
+
'13922-2356'.zipcode?.should eq(true)
|
153
|
+
'13922.2343'.zipcode?.should eq(false)
|
154
|
+
'13922 2342'.zipcode?.should eq(false)
|
155
|
+
'ABSSD'.zipcode?.should eq(false)
|
156
|
+
'i3323'.zipcode?.should eq(false)
|
157
|
+
'13922'.zipcode?.should eq(true)
|
159
158
|
end
|
160
159
|
|
161
160
|
it 'knows if it is numeric or not' do
|
162
|
-
'12341'.
|
163
|
-
'12341.23'.
|
164
|
-
'12341.00000000000000023'.
|
165
|
-
'0.12341'.
|
166
|
-
'0x2E'.
|
167
|
-
' 0.12341'.
|
168
|
-
' 0.12341 '.
|
169
|
-
'.12341'.
|
170
|
-
' 12341.'.
|
171
|
-
' 12341. '.
|
161
|
+
'12341'.numeric?.should eq(true)
|
162
|
+
'12341.23'.numeric?.should eq(true)
|
163
|
+
'12341.00000000000000023'.numeric?.should eq(true)
|
164
|
+
'0.12341'.numeric?.should eq(true)
|
165
|
+
'0x2E'.numeric?.should eq(true)
|
166
|
+
' 0.12341'.numeric?.should eq(true)
|
167
|
+
' 0.12341 '.numeric?.should eq(true)
|
168
|
+
'.12341'.numeric?.should eq(true)
|
169
|
+
' 12341.'.numeric?.should eq(false)
|
170
|
+
' 12341. '.numeric?.should eq(false)
|
172
171
|
end
|
173
172
|
end
|
174
173
|
end
|
174
|
+
# rubocop:enable Metrics/LineLength
|