midwire_common 0.1.9 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/CHANGELOG +9 -0
- data/Guardfile +7 -1
- data/README.md +1 -1
- data/lib/midwire_common/all.rb +1 -0
- data/lib/midwire_common/array.rb +6 -7
- data/lib/midwire_common/data_file_cache.rb +10 -15
- data/lib/midwire_common/rake_helper.rb +34 -21
- data/lib/midwire_common/version.rb +1 -1
- data/midwire_common.gemspec +2 -2
- data/spec/lib/midwire_common/string_spec.rb +24 -22
- data/spec/lib/midwire_common/time_spec.rb +4 -4
- data/spec/lib/midwire_common/time_tool_spec.rb +5 -5
- data/spec/spec_helper.rb +7 -5
- metadata +18 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b2d9f52a8ea1e5c417dedc8b47d671080df6aacd
|
4
|
+
data.tar.gz: 74b18476d79fd954b1318402323fe788bbee45da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 918873e38253dbfcc7693b30af5af984d88c5042ae4c6f0c27334826512d9865d0c1f080adc39e95a11ca388f60a6a5b3350a4a84abae5c27556bf409e52115b
|
7
|
+
data.tar.gz: a584a6861730bd4620fb8c431ad127513a05ae40e7b25c74a82f8968bf06a255a38169575f35672d9e9b032b0da40275c4f95d87eb963295126b69b6e88e3bf4
|
data/.gitignore
CHANGED
data/CHANGELOG
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
*0.1.11* (November 21, 2014)
|
2
|
+
|
3
|
+
* Fixed a bad release in 0.1.10
|
4
|
+
|
5
|
+
*0.1.10* (November 21, 2014)
|
6
|
+
|
7
|
+
* Fixed some rubocop issues
|
8
|
+
* Removed the ruby_prof dependency
|
9
|
+
|
1
10
|
*0.1.9* (September 19, 2014)
|
2
11
|
|
3
12
|
* Change space-padding for the hour to zero-padding, for the Time.timestamp method.
|
data/Guardfile
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
guard 'rspec', :cli => '--color --format doc', all_on_start: false, all_after_pass: false do
|
2
2
|
watch(%r{^spec/.+_spec\.rb$})
|
3
|
+
watch('spec/spec_helper.rb') { 'spec' }
|
3
4
|
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
4
|
-
watch(
|
5
|
+
watch(%r{^lib/midwire_common/all.rb$}) { 'spec' }
|
6
|
+
end
|
7
|
+
|
8
|
+
guard :rubocop do
|
9
|
+
watch(%r{.+\.rb$})
|
10
|
+
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
|
5
11
|
end
|
data/README.md
CHANGED
data/lib/midwire_common/all.rb
CHANGED
data/lib/midwire_common/array.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
class Array
|
2
2
|
def count_occurrences
|
3
3
|
k = Hash.new(0)
|
4
|
-
|
4
|
+
each { |x| k[x] += 1 }
|
5
5
|
k
|
6
6
|
end
|
7
7
|
|
8
8
|
def randomize
|
9
|
-
|
9
|
+
sort_by { rand }
|
10
10
|
end
|
11
11
|
|
12
12
|
def randomize!
|
13
|
-
|
13
|
+
replace(randomize)
|
14
14
|
end
|
15
15
|
|
16
16
|
def sort_case_insensitive
|
17
|
-
self.sort_by {|x| x.downcase}
|
17
|
+
self.sort_by { |x| x.downcase }
|
18
18
|
end
|
19
19
|
|
20
20
|
def each_with_first_last(first_code, main_code, last_code)
|
@@ -41,9 +41,8 @@ class Array
|
|
41
41
|
# => <table><tr><td>1</td><td>2</td></tr><tr><td>2</td><td>3</td></tr></table>
|
42
42
|
def superjoin(*ldescr)
|
43
43
|
d, rest = ldescr[0], ldescr[1..-1]
|
44
|
-
d[0] +
|
44
|
+
d[0] + map do |a|
|
45
45
|
(a.respond_to?(:superjoin) && rest.length > 0) ? a.superjoin(*rest) : a.to_s
|
46
|
-
|
46
|
+
end.join(d[1]) + d[2]
|
47
47
|
end
|
48
|
-
|
49
48
|
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
module MidwireCommon
|
2
|
-
|
3
2
|
# A simple class to cache data in a file
|
4
3
|
class DataFileCache
|
5
|
-
|
6
4
|
def initialize(filename)
|
7
5
|
@cache_dir = File.dirname(filename)
|
8
6
|
@cache_file = normalize_filename(filename)
|
@@ -12,7 +10,7 @@ module MidwireCommon
|
|
12
10
|
def put(data)
|
13
11
|
x = data.dup
|
14
12
|
x = x.join("\n") if x.is_a? Array
|
15
|
-
File.open(@cache_file, 'w') {|file| file.write(x)}
|
13
|
+
File.open(@cache_file, 'w') { |file| file.write(x) }
|
16
14
|
end
|
17
15
|
|
18
16
|
def get
|
@@ -20,24 +18,21 @@ module MidwireCommon
|
|
20
18
|
end
|
21
19
|
|
22
20
|
def age
|
23
|
-
return
|
21
|
+
return 999_999_99.0 unless File.exist?(@cache_file)
|
24
22
|
(Time.now - File.ctime(@cache_file)).to_f
|
25
23
|
end
|
26
24
|
|
27
|
-
########################################
|
28
25
|
private
|
29
26
|
|
30
|
-
|
31
|
-
|
32
|
-
|
27
|
+
def ensure_cache_dir
|
28
|
+
FileUtils::mkdir_p(@cache_dir) unless File.exist?(@cache_dir)
|
29
|
+
end
|
33
30
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
end
|
38
|
-
return filename
|
31
|
+
def normalize_filename(filename)
|
32
|
+
unless filename.match(Regexp.new(@cache_dir))
|
33
|
+
filename = "#{@cache_dir}/#{filename}"
|
39
34
|
end
|
40
|
-
|
35
|
+
filename
|
36
|
+
end
|
41
37
|
end
|
42
|
-
|
43
38
|
end
|
@@ -2,14 +2,13 @@ require 'thor'
|
|
2
2
|
require 'midwire_common'
|
3
3
|
|
4
4
|
module MidwireCommon
|
5
|
-
|
6
5
|
# RakeHelper helps to automate gem release and versioning tasks
|
7
6
|
class RakeHelper
|
8
7
|
include Rake::DSL if defined? Rake::DSL
|
9
8
|
|
10
9
|
def self.install_tasks(opts = {})
|
11
10
|
dir = opts[:dir] || Dir.pwd
|
12
|
-
|
11
|
+
new(dir).install
|
13
12
|
end
|
14
13
|
|
15
14
|
attr_reader :base
|
@@ -19,7 +18,7 @@ module MidwireCommon
|
|
19
18
|
end
|
20
19
|
|
21
20
|
def install
|
22
|
-
task_dir = File.expand_path(
|
21
|
+
task_dir = File.expand_path('../tasks', File.dirname(__FILE__))
|
23
22
|
Dir["#{task_dir}/*.rake"].sort.each { |ext| load ext }
|
24
23
|
end
|
25
24
|
|
@@ -37,7 +36,11 @@ module MidwireCommon
|
|
37
36
|
def install_gem
|
38
37
|
built_gem_path = build_gem
|
39
38
|
out, _ = sh_with_code("gem install '#{built_gem_path}'")
|
40
|
-
|
39
|
+
msg = <<-string.here_with_pipe
|
40
|
+
|Couldn't install gem, run `gem install #{built_gem_path}'
|
41
|
+
| for more detailed output
|
42
|
+
string
|
43
|
+
fail msg unless out[/Successfully installed/]
|
41
44
|
Bundler.ui.confirm "#{name} (#{version}) installed"
|
42
45
|
end
|
43
46
|
|
@@ -45,52 +48,58 @@ module MidwireCommon
|
|
45
48
|
guard_clean
|
46
49
|
guard_already_tagged
|
47
50
|
built_gem_path = build_gem
|
48
|
-
tag_version
|
51
|
+
tag_version do
|
49
52
|
git_push
|
50
53
|
rubygem_push(built_gem_path)
|
51
|
-
|
54
|
+
end
|
52
55
|
end
|
53
56
|
|
54
|
-
##################################################
|
55
57
|
protected
|
56
58
|
|
57
59
|
def rubygem_push(path)
|
58
|
-
if Pathname.new(
|
60
|
+
if Pathname.new('~/.gem/credentials').expand_path.exist?
|
59
61
|
sh("gem push '#{path}'")
|
60
62
|
Bundler.ui.confirm "Pushed #{name} #{version} to rubygems.org"
|
61
63
|
else
|
62
|
-
|
64
|
+
fail <<-string.here_with_pipe
|
65
|
+
|Your rubygems.org credentials aren't set.
|
66
|
+
| Run `gem push` to set them.
|
67
|
+
string
|
63
68
|
end
|
64
69
|
end
|
65
70
|
|
66
71
|
def built_gem_path
|
67
|
-
Dir[File.join(base, "#{name}-*.gem")].sort_by{|f| File.mtime(f)}.last
|
72
|
+
Dir[File.join(base, "#{name}-*.gem")].sort_by { |f| File.mtime(f) }.last
|
68
73
|
end
|
69
74
|
|
70
75
|
def git_push
|
71
76
|
perform_git_push
|
72
77
|
perform_git_push ' --tags'
|
73
|
-
Bundler.ui.confirm
|
78
|
+
Bundler.ui.confirm 'Pushed git commits and tags'
|
74
79
|
end
|
75
80
|
|
76
81
|
def perform_git_push(options = '')
|
77
82
|
cmd = "git push #{options}"
|
78
83
|
out, code = sh_with_code(cmd)
|
79
|
-
|
84
|
+
msg = <<-string.here_with_pipe
|
85
|
+
|Couldn't git push. `#{cmd}' failed with the following output:\n\n
|
86
|
+
|#{out}\n
|
87
|
+
string
|
88
|
+
fail msg unless code == 0
|
80
89
|
end
|
81
90
|
|
82
91
|
def guard_already_tagged
|
83
|
-
|
84
|
-
|
85
|
-
|
92
|
+
already_included = sh('git tag').split(/\n/).include?(version_tag)
|
93
|
+
msg = 'This tag has already been committed to the repo.'
|
94
|
+
fail(msg) if already_included
|
86
95
|
end
|
87
96
|
|
88
97
|
def guard_clean
|
89
|
-
clean?
|
98
|
+
clean? || fail('There are files that need to be committed first.')
|
90
99
|
end
|
91
100
|
|
92
101
|
def clean?
|
93
|
-
sh_with_code(
|
102
|
+
sh_with_code('git diff --exit-code')[1] == 0
|
94
103
|
end
|
95
104
|
|
96
105
|
def tag_version
|
@@ -105,19 +114,23 @@ module MidwireCommon
|
|
105
114
|
|
106
115
|
def sh(cmd, &block)
|
107
116
|
out, code = sh_with_code(cmd, &block)
|
108
|
-
|
117
|
+
msg = <<-string.here_with_pipe
|
118
|
+
|Running `#{cmd}' failed.
|
119
|
+
| Run this command directly for more detailed output.
|
120
|
+
string
|
121
|
+
code == 0 ? out : raise(out.empty? ? msg : out)
|
109
122
|
end
|
110
123
|
|
111
124
|
def sh_with_code(cmd, &block)
|
112
|
-
cmd <<
|
125
|
+
cmd << ' 2>&1'
|
113
126
|
outbuf = ''
|
114
127
|
Bundler.ui.debug(cmd)
|
115
|
-
Dir.chdir(base)
|
128
|
+
Dir.chdir(base) do
|
116
129
|
outbuf = `#{cmd}`
|
117
130
|
if $? == 0
|
118
131
|
block.call(outbuf) if block
|
119
132
|
end
|
120
|
-
|
133
|
+
end
|
121
134
|
[outbuf, $?]
|
122
135
|
end
|
123
136
|
end
|
data/midwire_common.gemspec
CHANGED
@@ -20,8 +20,8 @@ Gem::Specification.new do |gem|
|
|
20
20
|
gem.add_development_dependency "guard"
|
21
21
|
gem.add_development_dependency "guard-bundler"
|
22
22
|
gem.add_development_dependency "guard-rspec"
|
23
|
+
gem.add_development_dependency "guard-rubocop"
|
23
24
|
gem.add_development_dependency "pry"
|
24
25
|
gem.add_development_dependency "rake"
|
25
|
-
|
26
|
-
gem.add_runtime_dependency "ruby-prof"
|
26
|
+
gem.add_development_dependency "rubocop"
|
27
27
|
end
|
@@ -4,25 +4,25 @@
|
|
4
4
|
require 'spec_helper'
|
5
5
|
|
6
6
|
describe String do
|
7
|
-
it
|
7
|
+
it 'is a String' do
|
8
8
|
String.new.should be_a String
|
9
9
|
end
|
10
10
|
|
11
|
-
it
|
11
|
+
it 'generates a random string' do
|
12
12
|
String.random.length.should == 6
|
13
13
|
end
|
14
14
|
|
15
|
-
context
|
15
|
+
context 'slicing methods' do
|
16
16
|
it "'left' returns the leftmost 'n' characters" do
|
17
|
-
|
17
|
+
'My Bogus String'.left(2).should == 'My'
|
18
18
|
end
|
19
19
|
|
20
20
|
it "'right' returns the rightmost 'n' characters " do
|
21
|
-
|
21
|
+
'My Bogus String'.right(2).should == 'ng'
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
context
|
25
|
+
context 'trim method' do
|
26
26
|
it "'left_trim' removes all whitespace from the left of the string" do
|
27
27
|
" \t = this is a string".left_trim.should == '= this is a string'
|
28
28
|
end
|
@@ -83,38 +83,39 @@ describe String do
|
|
83
83
|
end
|
84
84
|
|
85
85
|
it 'format_phone returns a formatted phone number string' do
|
86
|
-
'9132329999'.format_phone.
|
87
|
-
'913.232.9999'.format_phone.
|
88
|
-
'913 232 9999'.format_phone.
|
89
|
-
'913-232-9999'.format_phone.
|
86
|
+
expect('9132329999'.format_phone).to eq('(913)232-9999')
|
87
|
+
expect('913.232.9999'.format_phone).to eq('(913)232-9999')
|
88
|
+
expect('913 232 9999'.format_phone).to eq('(913)232-9999')
|
89
|
+
expect('913-232-9999'.format_phone).to eq('(913)232-9999')
|
90
90
|
end
|
91
91
|
|
92
92
|
it 'sanitizes itself' do
|
93
|
-
'|bogus|'.sanitize.
|
94
|
-
'|∫|ß'.sanitize.
|
95
|
-
'ßogus'.sanitize.
|
96
|
-
'<tag>bogus</tag>'.sanitize.
|
97
|
-
'<tag>.bogus.</tag>'.sanitize.
|
93
|
+
expect('|bogus|'.sanitize).to eq('bogus')
|
94
|
+
expect('|∫|ß'.sanitize).to eq('')
|
95
|
+
expect('ßogus'.sanitize).to eq('ogus')
|
96
|
+
expect('<tag>bogus</tag>'.sanitize).to eq('tagbogustag')
|
97
|
+
expect('<tag>.bogus.</tag>'.sanitize).to eq('tag.bogus.tag')
|
98
98
|
s = '|∫|ß'
|
99
99
|
s.sanitize!
|
100
100
|
s.should == ''
|
101
101
|
end
|
102
102
|
|
103
103
|
it 'shortens itself with elipses at the end' do
|
104
|
+
#
|
104
105
|
s = 'this is my very long string which I will eventually shorten with the enhanced String class that we are now testing.'
|
105
106
|
short = s.shorten
|
106
|
-
short.
|
107
|
-
short.length.
|
107
|
+
expect(short).to eq('this is my very long string...')
|
108
|
+
expect(short.length).to eq(30)
|
108
109
|
|
109
110
|
s = '1234567890123456789012345678901234567890'
|
110
111
|
short = s.shorten
|
111
|
-
short.
|
112
|
-
short.length.
|
112
|
+
expect(short).to eq('123456789012345678901234567...')
|
113
|
+
expect(short.length).to eq(30)
|
113
114
|
|
114
115
|
s = '12345678901234567890'
|
115
116
|
short = s.shorten
|
116
|
-
short.
|
117
|
-
short.length.
|
117
|
+
expect(short).to eq('12345678901234567890')
|
118
|
+
expect(short.length).to eq(20)
|
118
119
|
end
|
119
120
|
|
120
121
|
context 'quotes' do
|
@@ -123,7 +124,8 @@ describe String do
|
|
123
124
|
end
|
124
125
|
|
125
126
|
it 'escapes double quotes' do
|
126
|
-
'this is a "test"'.escape_double_quotes
|
127
|
+
expect('this is a "test"'.escape_double_quotes)
|
128
|
+
.to eq("this is a \\\\\"test\\\\\"")
|
127
129
|
end
|
128
130
|
end
|
129
131
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Time do
|
4
|
-
it
|
4
|
+
it 'generates a timestamp, appropriate for a filename' do
|
5
5
|
ts = Time.timestamp
|
6
|
-
ts.length.
|
7
|
-
ts.is_alpha_numeric
|
8
|
-
ts.is_numeric
|
6
|
+
expect(ts.length).to eq(14)
|
7
|
+
expect(ts.is_alpha_numeric?).to be_true
|
8
|
+
expect(ts.is_numeric?).to be_true
|
9
9
|
end
|
10
10
|
end
|
@@ -1,13 +1,13 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe MidwireCommon::TimeTool do
|
4
4
|
|
5
|
-
it
|
6
|
-
MidwireCommon::TimeTool.seconds_to_time(
|
5
|
+
it 'converts seconds to timestamp' do
|
6
|
+
MidwireCommon::TimeTool.seconds_to_time(92_353).should == '25:39:13'
|
7
7
|
end
|
8
8
|
|
9
|
-
it
|
10
|
-
MidwireCommon::TimeTool.time_to_seconds(
|
9
|
+
it 'converts timestamp to seconds' do
|
10
|
+
MidwireCommon::TimeTool.time_to_seconds('25:39:13').should == 92_353
|
11
11
|
end
|
12
12
|
|
13
13
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,21 +1,23 @@
|
|
1
1
|
if ENV['COVERAGE']
|
2
2
|
require 'simplecov'
|
3
3
|
SimpleCov.start do
|
4
|
-
add_filter
|
5
|
-
add_filter
|
4
|
+
add_filter 'spec/'
|
5
|
+
add_filter 'vendor/'
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
|
-
require
|
9
|
+
require 'pry'
|
10
10
|
require File.join(File.dirname(__FILE__), '..', 'lib', 'midwire_common')
|
11
|
-
require
|
11
|
+
require 'midwire_common/all'
|
12
12
|
|
13
13
|
PROJECT_ROOT = File.expand_path('..', File.dirname(__FILE__))
|
14
14
|
|
15
15
|
RSpec.configure do |config|
|
16
|
+
include MidwireCommon
|
17
|
+
|
16
18
|
config.mock_with :rspec
|
17
19
|
config.color_enabled = true
|
18
|
-
config.order =
|
20
|
+
config.order = 'random'
|
19
21
|
|
20
22
|
def capture(stream)
|
21
23
|
begin
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: midwire_common
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Blackburn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: guard-rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: pry
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -109,13 +123,13 @@ dependencies:
|
|
109
123
|
- !ruby/object:Gem::Version
|
110
124
|
version: '0'
|
111
125
|
- !ruby/object:Gem::Dependency
|
112
|
-
name:
|
126
|
+
name: rubocop
|
113
127
|
requirement: !ruby/object:Gem::Requirement
|
114
128
|
requirements:
|
115
129
|
- - ">="
|
116
130
|
- !ruby/object:Gem::Version
|
117
131
|
version: '0'
|
118
|
-
type: :
|
132
|
+
type: :development
|
119
133
|
prerelease: false
|
120
134
|
version_requirements: !ruby/object:Gem::Requirement
|
121
135
|
requirements:
|