midwire_common 0.1.12 → 0.1.13
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 +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
@@ -1,23 +1,22 @@
|
|
1
1
|
class String
|
2
|
-
|
3
2
|
class << self
|
4
|
-
def random(count = 6, ranges = [('a'..'z'),('A'..'Z'),('0'..'9')])
|
5
|
-
o =
|
6
|
-
|
3
|
+
def random(count = 6, ranges = [('a'..'z'), ('A'..'Z'), ('0'..'9')])
|
4
|
+
o = ranges.map(&:to_a).flatten
|
5
|
+
(0..(count - 1)).map { o[rand(o.length)] }.join
|
7
6
|
end
|
8
7
|
end
|
9
8
|
|
10
9
|
def left(count)
|
11
|
-
|
10
|
+
slice(0, count)
|
12
11
|
end
|
13
12
|
|
14
13
|
def right(count)
|
15
|
-
|
14
|
+
slice(-count, count)
|
16
15
|
end
|
17
16
|
|
18
17
|
def left_trim
|
19
18
|
# remove leading whitespace
|
20
|
-
|
19
|
+
gsub(/^[\t\s]+/, '')
|
21
20
|
end
|
22
21
|
|
23
22
|
def left_trim!
|
@@ -26,7 +25,7 @@ class String
|
|
26
25
|
|
27
26
|
def right_trim
|
28
27
|
# remove trailing whitespace
|
29
|
-
|
28
|
+
gsub(/[\t\s]+$/, '')
|
30
29
|
end
|
31
30
|
|
32
31
|
def right_trim!
|
@@ -35,7 +34,7 @@ class String
|
|
35
34
|
|
36
35
|
def trim
|
37
36
|
# remove leading and trailing whitespace
|
38
|
-
|
37
|
+
left_trim.right_trim
|
39
38
|
end
|
40
39
|
|
41
40
|
def trim!
|
@@ -48,18 +47,19 @@ class String
|
|
48
47
|
# |<script type="text/javascript">
|
49
48
|
# stop
|
50
49
|
def here_with_pipe(delimeter = ' ')
|
51
|
-
lines =
|
52
|
-
lines.map! {|c| c.sub!(/\s*\|/, '')}
|
50
|
+
lines = split("\n")
|
51
|
+
lines.map! { |c| c.sub!(/\s*\|/, '') }
|
53
52
|
new_string = lines.join(delimeter)
|
54
|
-
|
53
|
+
replace(new_string)
|
55
54
|
end
|
56
55
|
|
57
|
-
def
|
56
|
+
def alpha_numeric?
|
58
57
|
regex = /^[a-zA-Z0-9]+$/
|
59
|
-
|
58
|
+
(self =~ regex) == 0 ? true : false
|
60
59
|
end
|
61
60
|
|
62
|
-
|
61
|
+
# rubocop:disable Metrics/LineLength
|
62
|
+
def email_address?
|
63
63
|
# //Email address
|
64
64
|
# //Use this version to seek out email addresses in random documents and texts.
|
65
65
|
# //Does not match email addresses using an IP address instead of a domain name.
|
@@ -100,30 +100,29 @@ class String
|
|
100
100
|
|
101
101
|
email_regex = %r{^[A-Z0-9._%-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|biz|info|name|aero|jobs|museum|edu|pro)$}xi # Case insensitive
|
102
102
|
|
103
|
-
|
103
|
+
(self =~ email_regex) == 0 ? true : false
|
104
104
|
end
|
105
|
+
# rubocop:enable Metrics/LineLength
|
105
106
|
|
106
|
-
def
|
107
|
+
def zipcode?
|
107
108
|
self =~ %r{^(\d{5})(-\d{4})?$}x ? true : false
|
108
109
|
end
|
109
110
|
|
110
|
-
def
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
true # numeric
|
117
|
-
end
|
111
|
+
def numeric?
|
112
|
+
Float(self)
|
113
|
+
rescue
|
114
|
+
false # not numeric
|
115
|
+
else
|
116
|
+
true # numeric
|
118
117
|
end
|
119
118
|
|
120
119
|
def format_phone
|
121
|
-
|
122
|
-
'(' << slice(0..2) << ')' << slice(3..5) << '-' << slice(-4,4)
|
120
|
+
gsub!(/[a-z,! \-\(\)\:\;\.\&\$]+/i, '')
|
121
|
+
'(' << slice(0..2) << ')' << slice(3..5) << '-' << slice(-4, 4)
|
123
122
|
end
|
124
123
|
|
125
124
|
def sanitize
|
126
|
-
|
125
|
+
gsub(/[^a-z0-9,! \-\(\)\:\;\.\&\$]+/i, '')
|
127
126
|
end
|
128
127
|
|
129
128
|
def sanitize!
|
@@ -131,12 +130,12 @@ class String
|
|
131
130
|
end
|
132
131
|
|
133
132
|
def shorten(maxcount = 30)
|
134
|
-
if
|
133
|
+
if length >= maxcount
|
135
134
|
shortened = self[0, maxcount]
|
136
135
|
splitted = shortened.split(/\s/)
|
137
136
|
if splitted.length > 1
|
138
137
|
words = splitted.length
|
139
|
-
splitted[0, words-1].join(
|
138
|
+
splitted[0, words - 1].join(' ') + '...'
|
140
139
|
else
|
141
140
|
shortened[0, maxcount - 3] + '...'
|
142
141
|
end
|
@@ -146,10 +145,10 @@ class String
|
|
146
145
|
end
|
147
146
|
|
148
147
|
def escape_single_quotes
|
149
|
-
|
148
|
+
gsub(/[']/, '\\\\\'')
|
150
149
|
end
|
151
150
|
|
152
151
|
def escape_double_quotes
|
153
|
-
|
152
|
+
gsub(/["]/, '\\\\\"')
|
154
153
|
end
|
155
154
|
end
|
data/lib/midwire_common/time.rb
CHANGED
@@ -1,35 +1,34 @@
|
|
1
1
|
module MidwireCommon
|
2
|
-
|
3
2
|
class TimeTool
|
4
|
-
|
5
3
|
# converts the given time (HH:MM:SS) to seconds
|
6
4
|
#
|
7
5
|
# +time+ the time-string
|
8
6
|
def self.time_to_seconds(time)
|
9
|
-
return -1 if time.nil?
|
7
|
+
return -1 if time.nil? || time.strip.empty?
|
10
8
|
times = time.split(/:/).reverse
|
11
9
|
seconds = 0
|
12
|
-
|
10
|
+
(0...times.length).each_with_index do |i|
|
13
11
|
seconds += times[i].to_i * (60**i)
|
14
12
|
end
|
15
|
-
|
13
|
+
seconds
|
16
14
|
end
|
17
15
|
|
18
16
|
# converts the given seconds into a time string (HH:MM:SS)
|
19
17
|
#
|
20
18
|
# +seconds+ the seconds to convert
|
21
19
|
def self.seconds_to_time(seconds)
|
22
|
-
return
|
20
|
+
return 'unknown' if seconds.nil?
|
23
21
|
t = seconds
|
24
|
-
time =
|
25
|
-
2.downto(0)
|
22
|
+
time = ''
|
23
|
+
2.downto(0) do |i|
|
26
24
|
tmp = t / (60**i)
|
27
|
-
t
|
28
|
-
time
|
29
|
-
|
30
|
-
|
31
|
-
|
25
|
+
t -= tmp * 60**i
|
26
|
+
time += ':' unless time.empty?
|
27
|
+
# rubocop:disable Style/FormatString
|
28
|
+
time += ('%02d' % tmp)
|
29
|
+
# rubocop:enable Style/FormatString
|
30
|
+
end
|
31
|
+
time
|
32
32
|
end
|
33
33
|
end
|
34
|
-
|
35
34
|
end
|
data/lib/tasks/version.rake
CHANGED
@@ -6,9 +6,6 @@ require 'pry'
|
|
6
6
|
|
7
7
|
module Bundler
|
8
8
|
class GemHelper
|
9
|
-
|
10
|
-
protected
|
11
|
-
|
12
9
|
# Push the gem to your own internal gem inabox server
|
13
10
|
# def rubygem_push(path)
|
14
11
|
# sh("gem inabox '#{path}'")
|
@@ -21,16 +18,19 @@ namespace :version do
|
|
21
18
|
PROJECT_ROOT = File.expand_path(FileUtils.pwd)
|
22
19
|
PROJECT_NAME = ENV['PROJECT_NAME'] || File.basename(PROJECT_ROOT)
|
23
20
|
|
24
|
-
desc
|
21
|
+
desc 'Write changes to the CHANGELOG'
|
25
22
|
task :changes do
|
26
|
-
text = ask(
|
27
|
-
text.insert(
|
23
|
+
text = ask('CHANGELOG Entry:')
|
24
|
+
text.insert(
|
25
|
+
0,
|
26
|
+
"*#{read_version.join('.')}* (#{Time.now.strftime('%B %d, %Y')})\n\n"
|
27
|
+
)
|
28
28
|
text << "\n"
|
29
29
|
prepend_changelog(text)
|
30
30
|
system("#{ENV['EDITOR']} CHANGELOG")
|
31
31
|
end
|
32
32
|
|
33
|
-
desc
|
33
|
+
desc 'Increment the patch version and write changes to the changelog'
|
34
34
|
task :bump_patch do
|
35
35
|
exit unless check_branch
|
36
36
|
major, minor, patch = read_version
|
@@ -38,116 +38,128 @@ namespace :version do
|
|
38
38
|
write_version([major, minor, patch])
|
39
39
|
version_string = read_version.join('.')
|
40
40
|
readme = open('README.md').read
|
41
|
-
File.open('README.md', 'w')
|
42
|
-
|
41
|
+
File.open('README.md', 'w') do |f|
|
42
|
+
# rubocop:disable Metrics/LineLength
|
43
|
+
f.write(readme.gsub(/^\*\*Version: [0-9\.]+\*\*$/, "**Version: #{version_string}**"))
|
44
|
+
# rubocop:enable Metrics/LineLength
|
45
|
+
end
|
46
|
+
Rake::Task['version:changes'].invoke
|
43
47
|
end
|
44
|
-
desc "Alias for :bump_patch"
|
45
|
-
task :bump => :bump_patch do; end
|
46
48
|
|
47
|
-
|
49
|
+
# rubocop:disable Style/Blocks
|
50
|
+
desc 'Alias for :bump_patch'
|
51
|
+
task bump: :bump_patch do; end
|
52
|
+
# rubocop:enable Style/Blocks
|
53
|
+
|
54
|
+
desc 'Increment the minor version and write changes to the changelog'
|
48
55
|
task :bump_minor do
|
49
56
|
exit unless check_branch
|
50
|
-
major, minor,
|
57
|
+
major, minor, _patch = read_version
|
51
58
|
minor = minor.to_i + 1
|
52
59
|
patch = 0
|
53
60
|
write_version([major, minor, patch])
|
54
|
-
Rake::Task[
|
61
|
+
Rake::Task['version:changes'].invoke
|
55
62
|
end
|
56
63
|
|
57
|
-
desc
|
64
|
+
desc 'Increment the major version and write changes to the changelog'
|
58
65
|
task :bump_major do
|
59
66
|
exit unless check_branch
|
60
|
-
major,
|
67
|
+
major, _minor, _patch = read_version
|
61
68
|
major = major.to_i + 1
|
62
69
|
minor = 0
|
63
70
|
patch = 0
|
64
71
|
write_version([major, minor, patch])
|
65
|
-
Rake::Task[
|
72
|
+
Rake::Task['version:changes'].invoke
|
66
73
|
end
|
67
74
|
|
68
|
-
##################################################
|
69
75
|
private
|
70
76
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
77
|
+
def version_file_path
|
78
|
+
split = PROJECT_NAME.split('-')
|
79
|
+
"#{PROJECT_ROOT}/lib/#{split.join('/')}/version.rb"
|
80
|
+
end
|
75
81
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
end
|
82
|
+
def module_name
|
83
|
+
if PROJECT_NAME.match(/-/)
|
84
|
+
PROJECT_NAME.split('-').map(&:capitalize).join('::')
|
85
|
+
elsif PROJECT_NAME.match(/_/)
|
86
|
+
PROJECT_NAME.split('_').map(&:capitalize).join
|
87
|
+
else
|
88
|
+
PROJECT_NAME.capitalize
|
84
89
|
end
|
90
|
+
end
|
85
91
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
92
|
+
def read_version
|
93
|
+
load version_file_path
|
94
|
+
text = eval("#{module_name}::VERSION")
|
95
|
+
text.split('.')
|
96
|
+
end
|
91
97
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
end
|
98
|
+
def write_version(version_array)
|
99
|
+
version = version_array.join('.')
|
100
|
+
new_version = %(VERSION = "#{version}")
|
101
|
+
lines = File.readlines(version_file_path)
|
102
|
+
File.open(version_file_path, 'w') do |f|
|
103
|
+
lines.each do |line|
|
104
|
+
if line =~ /VERSION/
|
105
|
+
f.write("#{new_version}\n")
|
106
|
+
else
|
107
|
+
f.write(line)
|
103
108
|
end
|
104
109
|
end
|
105
110
|
end
|
111
|
+
end
|
106
112
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
end
|
113
|
+
def prepend_changelog(text_array)
|
114
|
+
# read current changelog
|
115
|
+
old = File.read("#{PROJECT_ROOT}/CHANGELOG").to_s.chomp
|
116
|
+
text_array.push(old)
|
117
|
+
File.open("#{PROJECT_ROOT}/CHANGELOG", 'w') do |f|
|
118
|
+
text_array.flatten.each do |line|
|
119
|
+
f.puts(line)
|
115
120
|
end
|
116
121
|
end
|
122
|
+
end
|
117
123
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
response
|
124
|
+
# rubocop:disable Lint/AssignmentInCondition
|
125
|
+
def ask(message)
|
126
|
+
response = []
|
127
|
+
puts message
|
128
|
+
puts 'Hit <Control>-D when finished:'
|
129
|
+
while line = Readline.readline('* ', false)
|
130
|
+
response << "* #{line.chomp}" unless line.nil?
|
126
131
|
end
|
132
|
+
response
|
133
|
+
end
|
134
|
+
# rubocop:enable Lint/AssignmentInCondition
|
127
135
|
|
128
|
-
|
129
|
-
|
130
|
-
|
136
|
+
def current_branch
|
137
|
+
`git rev-parse --abbrev-ref HEAD`.chomp
|
138
|
+
end
|
131
139
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
140
|
+
def branch_warning_message
|
141
|
+
<<-string.here_with_pipe("\n")
|
142
|
+
|You typically do not want to bump versions on the 'master' branch
|
143
|
+
|unless you plan to rebase or back-merge into the 'develop'.
|
144
|
+
|
|
145
|
+
|If you don't care or don't know what I'm talking about just enter 'y'
|
146
|
+
|and continue.
|
147
|
+
|
|
148
|
+
|Optionally, you can hit 'n' to abort and switch your branch to 'develop'
|
149
|
+
|or whatever branch you use for development, bump the version, merge to
|
150
|
+
|'master' then 'rake release'.
|
151
|
+
|
|
152
|
+
|Do you really want to bump the version on your 'master' branch? (y/n)
|
153
|
+
string
|
154
|
+
end
|
155
|
+
|
156
|
+
def check_branch
|
157
|
+
return true unless current_branch == 'master'
|
158
|
+
puts(branch_warning_message)
|
159
|
+
while (line = $stdin.gets.chomp)
|
160
|
+
return true if line.match(/[yY]/)
|
161
|
+
puts 'Aborting version bump.'
|
162
|
+
return false
|
152
163
|
end
|
164
|
+
end
|
153
165
|
end
|