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.
@@ -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 = ranges.map{|i| i.to_a}.flatten;
6
- string = (0..(count-1)).map{ o[rand(o.length)] }.join;
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
- self.slice(0,count)
10
+ slice(0, count)
12
11
  end
13
12
 
14
13
  def right(count)
15
- self.slice(-count,count)
14
+ slice(-count, count)
16
15
  end
17
16
 
18
17
  def left_trim
19
18
  # remove leading whitespace
20
- self.gsub(/^[\t\s]+/, '')
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
- self.gsub(/[\t\s]+$/, '')
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
- self.left_trim.right_trim
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 = self.split("\n")
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
- self.replace(new_string)
53
+ replace(new_string)
55
54
  end
56
55
 
57
- def is_alpha_numeric?
56
+ def alpha_numeric?
58
57
  regex = /^[a-zA-Z0-9]+$/
59
- return (self =~ regex) == 0 ? true : false
58
+ (self =~ regex) == 0 ? true : false
60
59
  end
61
60
 
62
- def is_email_address?
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
- return (self =~ email_regex) == 0 ? true : false
103
+ (self =~ email_regex) == 0 ? true : false
104
104
  end
105
+ # rubocop:enable Metrics/LineLength
105
106
 
106
- def is_zipcode?
107
+ def zipcode?
107
108
  self =~ %r{^(\d{5})(-\d{4})?$}x ? true : false
108
109
  end
109
110
 
110
- def is_numeric?
111
- begin
112
- Float(self)
113
- rescue
114
- false # not numeric
115
- else
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
- self.gsub!(/[a-z,! \-\(\)\:\;\.\&\$]+/i, '')
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
- self.gsub(/[^a-z0-9,! \-\(\)\:\;\.\&\$]+/i, '')
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 self.length >= maxcount
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
- self.gsub(/[']/, '\\\\\'')
148
+ gsub(/[']/, '\\\\\'')
150
149
  end
151
150
 
152
151
  def escape_double_quotes
153
- self.gsub(/["]/, '\\\\\"')
152
+ gsub(/["]/, '\\\\\"')
154
153
  end
155
154
  end
@@ -1,9 +1,7 @@
1
1
  class Time
2
-
3
2
  class << self
4
3
  def timestamp
5
- "#{Time.now.strftime("%Y%m%d%H%M%S")}"
4
+ "#{Time.now.strftime('%Y%m%d%H%M%S')}"
6
5
  end
7
6
  end
8
-
9
7
  end
@@ -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? or time.strip.empty?
7
+ return -1 if time.nil? || time.strip.empty?
10
8
  times = time.split(/:/).reverse
11
9
  seconds = 0
12
- for i in (0...times.length)
10
+ (0...times.length).each_with_index do |i|
13
11
  seconds += times[i].to_i * (60**i)
14
12
  end
15
- return seconds
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 "unknown" if seconds.nil?
20
+ return 'unknown' if seconds.nil?
23
21
  t = seconds
24
- time = ""
25
- 2.downto(0) { |i|
22
+ time = ''
23
+ 2.downto(0) do |i|
26
24
  tmp = t / (60**i)
27
- t = t - tmp * 60**i
28
- time = time + ":" if not time.empty?
29
- time = time + ("%02d" % tmp)
30
- }
31
- return time
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
@@ -1,6 +1,6 @@
1
1
  original_verbosity = $VERBOSE
2
2
  $VERBOSE = nil
3
3
  module MidwireCommon
4
- VERSION = "0.1.12"
4
+ VERSION = "0.1.13"
5
5
  end
6
6
  $VERBOSE = original_verbosity
@@ -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 "Write changes to the CHANGELOG"
21
+ desc 'Write changes to the CHANGELOG'
25
22
  task :changes do
26
- text = ask("CHANGELOG Entry:")
27
- text.insert(0, "*#{read_version.join('.')}* (#{Time.now.strftime("%B %d, %Y")})\n\n")
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 "Increment the patch version and write changes to the changelog"
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') {|f| f.write(readme.gsub(/^\*\*Version: [0-9\.]+\*\*$/, "**Version: #{version_string}**")) }
42
- Rake::Task["version:changes"].invoke
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
- desc "Increment the minor version and write changes to the changelog"
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, patch = read_version
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["version:changes"].invoke
61
+ Rake::Task['version:changes'].invoke
55
62
  end
56
63
 
57
- desc "Increment the major version and write changes to the changelog"
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, minor, patch = read_version
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["version:changes"].invoke
72
+ Rake::Task['version:changes'].invoke
66
73
  end
67
74
 
68
- ##################################################
69
75
  private
70
76
 
71
- def version_file_path
72
- split = PROJECT_NAME.split('-')
73
- "#{PROJECT_ROOT}/lib/#{split.join('/')}/version.rb"
74
- end
77
+ def version_file_path
78
+ split = PROJECT_NAME.split('-')
79
+ "#{PROJECT_ROOT}/lib/#{split.join('/')}/version.rb"
80
+ end
75
81
 
76
- def module_name
77
- if PROJECT_NAME.match(/-/)
78
- PROJECT_NAME.split('-').map {|e| e.capitalize}.join('::')
79
- elsif PROJECT_NAME.match(/_/)
80
- PROJECT_NAME.split('_').map {|e| e.capitalize}.join
81
- else
82
- PROJECT_NAME.capitalize
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
- def read_version
87
- load version_file_path
88
- text = eval("#{module_name}::VERSION")
89
- major, minor, patch = text.split('.')
90
- end
92
+ def read_version
93
+ load version_file_path
94
+ text = eval("#{module_name}::VERSION")
95
+ text.split('.')
96
+ end
91
97
 
92
- def write_version(version_array)
93
- version = version_array.join('.')
94
- new_version = %Q( VERSION = "#{version}")
95
- lines = File.readlines(version_file_path)
96
- File.open(version_file_path, 'w') do |f|
97
- lines.each do |line|
98
- if line =~ /VERSION/
99
- f.write("#{new_version.to_s}\n")
100
- else
101
- f.write(line)
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
- def prepend_changelog(text_array)
108
- # read current changelog
109
- old = File.read("#{PROJECT_ROOT}/CHANGELOG").to_s.chomp
110
- text_array.push(old)
111
- File.open("#{PROJECT_ROOT}/CHANGELOG", 'w') do |f|
112
- text_array.flatten.each do |line|
113
- f.puts(line)
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
- def ask(message)
119
- response = []
120
- puts message
121
- puts "Hit <Control>-D when finished:"
122
- while line = Readline.readline('* ', false)
123
- response << "* #{line.chomp}" unless line.nil?
124
- end
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
- def current_branch
129
- `git rev-parse --abbrev-ref HEAD`.chomp
130
- end
136
+ def current_branch
137
+ `git rev-parse --abbrev-ref HEAD`.chomp
138
+ end
131
139
 
132
- def check_branch
133
- if current_branch == 'master'
134
- puts "You typically do not want to bump versions on the 'master' branch"
135
- puts "unless you plan to rebase or back-merge into the 'develop'."
136
- puts ""
137
- puts "If you don't care or don't know what I'm talking about just enter 'y'"
138
- puts "and continue."
139
- puts ""
140
- puts "Optionally, you can hit 'n' to abort and switch your branch to 'develop'"
141
- puts "or whatever branch you use for development, bump the version, merge to"
142
- puts "'master' then 'rake release'."
143
- puts ""
144
- puts "Do you really want to bump the version on your 'master' branch? (y/n)"
145
- while (line = $stdin.gets.chomp)
146
- return true if line.match /[yY]/
147
- puts "Aborting version bump."
148
- return false
149
- end
150
- end
151
- return true
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