midwire_common 0.3.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d307d5ad84f9e843f0b140b76cc4d1caa9c199ed
4
- data.tar.gz: 9caa3bff2e935878df2542260cb6215b19c03aaf
3
+ metadata.gz: 3e7d213c8109f31ef945f647152122b7f8520363
4
+ data.tar.gz: 71c130a18c5ca6ab637572a5c48bbb9f0598b54b
5
5
  SHA512:
6
- metadata.gz: f1d18c918e5f6a8911883b96ee018925abfa5a0c2796ba14c1f3b0af896a173b792370b4d1170d3554b922a0afedbfa711039ee66d49b2da6432ce3b04d2a432
7
- data.tar.gz: 1d094b78ea0d0d8e525e1490422b1a8540ac912f9cfd498988559b2aef721b8702d8e156ea2d4b37ea4c76a8a3ce2a2409280c28ac565b9d5d9dbad153608800
6
+ metadata.gz: ed228a57311853ac1af82e122cd25b5d77f95280d9108355ab8164f6a6a1a8e6e42241578d58799ce1d1282f7269ab0112a2eddac2b7a4093b187760428f1c94
7
+ data.tar.gz: 5650ead282aa147f27e5830ef02022b25d297395a0df6e20346def21b8fc8474e483329a180da5a411e62d100eb4aeceee2039363d1a247bf89aca3d3485ea32
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.2.2
1
+ 2.2.4
data/CHANGELOG CHANGED
@@ -1,3 +1,14 @@
1
+ *1.1.0* (March 10, 2016)
2
+
3
+ * Fix many code smells
4
+ * Remove some duplicate methods that were provided by Ruby 2.x and stdlib
5
+
6
+ *1.0.0* (March 10, 2016)
7
+
8
+ * Remove thor dependency
9
+ * Add Ruby 2.x as the required_ruby_version
10
+ * Add version badge to README.md
11
+
1
12
  *0.3.0* (March 10, 2016)
2
13
 
3
14
  * Cleanup the code
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Midwire Common Gem
2
2
 
3
- **Version: 0.3.0**
3
+ [![Gem Version](https://badge.fury.io/rb/midwire_common.png)](https://badge.fury.io/rb/midwire_common)
4
+
5
+ **Version: 1.1.0**
4
6
 
5
7
  A handy, light-weight Ruby library for Midwire development
6
8
 
@@ -1,8 +1,9 @@
1
+ # A light-weight super Array class
1
2
  class Array
2
3
  def count_occurrences
3
- k = Hash.new(0)
4
- each { |x| k[x] += 1 }
5
- k
4
+ hash = Hash.new(0)
5
+ each { |elem| hash[elem] += 1 }
6
+ hash
6
7
  end
7
8
 
8
9
  def randomize
@@ -18,8 +19,8 @@ class Array
18
19
  end
19
20
 
20
21
  def each_with_first_last(first_code, main_code, last_code)
21
- each_with_index do |item, i|
22
- case i
22
+ each_with_index do |item, ndx|
23
+ case ndx
23
24
  when 0 then first_code.call(item)
24
25
  when size - 1 then last_code.call(item)
25
26
  else main_code.call(item)
@@ -30,8 +31,8 @@ class Array
30
31
  # Binary search returns index if found or nil
31
32
  # rubocop:disable Metrics/LineLength
32
33
  # rubocop:disable Style/SpaceAfterComma, Style/SpaceAroundOperators, Style/SpaceAfterColon, Style/SpaceAfterSemicolon, Style/Semicolon
33
- def bsearch(e, l = 0, u = length - 1)
34
- return if l>u;m=(l+u)/2;e<self[m]?u=m-1:l=m+1;e==self[m]?m:bsearch(e,l,u)
34
+ def binsearch(e, l = 0, u = length - 1)
35
+ return if l>u;m=(l+u)/2;e<self[m]?u=m-1:l=m+1;e==self[m]?m:binsearch(e,l,u)
35
36
  end
36
37
  # rubocop:enable Style/SpaceAfterComma, Style/SpaceAroundOperators, Style/SpaceAfterColon, Style/SpaceAfterSemicolon, Style/Semicolon
37
38
 
@@ -43,11 +44,11 @@ class Array
43
44
  # [[1,2],[2,3]].superjoin( %w{<table><tr> </tr><tr> </tr></table>}, %w{<td> </td><td> </td>} )
44
45
  # => <table><tr><td>1</td><td>2</td></tr><tr><td>2</td><td>3</td></tr></table>
45
46
  def superjoin(*ldescr)
46
- d = ldescr[0]
47
+ dim = ldescr[0]
47
48
  rest = ldescr[1..-1]
48
- d[0] + map do |a|
49
- (a.respond_to?(:superjoin) && rest.length > 0) ? a.superjoin(*rest) : a.to_s
50
- end.join(d[1]) + d[2]
49
+ dim[0] + map do |arr|
50
+ (arr.respond_to?(:superjoin) && !rest.empty?) ? arr.superjoin(*rest) : arr.to_s
51
+ end.join(dim[1]) + dim[2]
51
52
  end
52
53
  # rubocop:enable Metrics/LineLength
53
54
  end
@@ -8,9 +8,9 @@ module MidwireCommon
8
8
  end
9
9
 
10
10
  def put(data)
11
- x = data.dup
12
- x = x.join("\n") if x.is_a? Array
13
- File.open(@cache_file, 'w') { |file| file.write(x) }
11
+ newdata = data.dup
12
+ newdata = newdata.join("\n") if newdata.is_a? Array
13
+ File.open(@cache_file, 'w') { |file| file.write(newdata) }
14
14
  end
15
15
 
16
16
  def get
@@ -1,10 +1,11 @@
1
+ # A more useful Enumerable module
1
2
  module Enumerable
2
3
  # Sort by frequency of occurrence
3
4
  def sort_by_frequency
4
- histogram = each_with_object(Hash.new(0)) do |x, hash|
5
- hash[x] += 1
5
+ histogram = each_with_object(Hash.new(0)) do |elem, hash|
6
+ hash[elem] += 1
6
7
  hash
7
8
  end
8
- sort_by { |x| [histogram[x] * -1, x] }
9
+ sort_by { |elem| [histogram[elem] * -1, elem] }
9
10
  end
10
11
  end
@@ -1,8 +1,10 @@
1
1
  class File
2
+ # A more useful File::Stat class
2
3
  class Stat
4
+ # Return device name for a given file
3
5
  def self.device_name(file)
4
- Dir['/dev/*'].inject({}) do |h, v|
5
- h.update(File.stat(v).rdev => v)
6
+ Dir['/dev/*'].inject({}) do |hash, node|
7
+ hash.update(File.stat(node).rdev => node)
6
8
  end.values_at(File.stat(file).dev).first || nil
7
9
  end
8
10
  end
@@ -1,22 +1,4 @@
1
+ # A more useful Fixnum class
1
2
  class Fixnum
2
- # Format a number with commas and a decimal point
3
- # rubocop:disable Style/PerlBackrefs
4
- def commify
5
- to_s =~ /([^\.]*)(\..*)?/
6
- int, dec = $1.reverse, $2 ? $2 : ''
7
- while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3')
8
- end
9
- int.reverse + dec
10
- end
11
- # rubocop:enable Style/PerlBackrefs
12
-
13
- # Ruby 2 has its own methods for these
14
-
15
- # def is_even?
16
- # self % 2 == 0
17
- # end
18
-
19
- # def is_odd?
20
- # !is_even?
21
- # end
3
+ include MidwireCommon::NumberBehavior
22
4
  end
@@ -1,12 +1,4 @@
1
+ # A more useful Float class
1
2
  class Float
2
- # Format a number with commas and a decimal point
3
- # rubocop:disable Style/PerlBackrefs
4
- def commify
5
- to_s =~ /([^\.]*)(\..*)?/
6
- int, dec = $1.reverse, $2 ? $2 : ''
7
- while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3')
8
- end
9
- int.reverse + dec
10
- end
11
- # rubocop:enable Style/PerlBackrefs
3
+ include MidwireCommon::NumberBehavior
12
4
  end
@@ -1,6 +1,8 @@
1
+ # By Nick Ostrovsky
2
+ # http://firedev.com/posts/2015/bottomless-ruby-hash/
1
3
  class BottomlessHash < Hash
2
4
  def initialize
3
- super(&-> (h, k) { h[k] = self.class.new })
5
+ super(&-> (hash, key) { hash[key] = self.class.new })
4
6
  end
5
7
 
6
8
  def self.from_hash(hash)
@@ -8,11 +10,13 @@ class BottomlessHash < Hash
8
10
  end
9
11
  end
10
12
 
13
+ # A more useful Hash class
11
14
  class Hash
12
15
  def bottomless
13
16
  BottomlessHash.from_hash(self)
14
17
  end
15
18
 
19
+ # A better grep
16
20
  def grep(pattern)
17
21
  each_with_object([]) do |kv, res|
18
22
  res << kv if kv[0] =~ pattern || kv[1] =~ pattern
@@ -75,27 +79,27 @@ class Hash
75
79
 
76
80
  # Usage { :a => 1, :b => 2, :c => 3}.except(:a) -> { :b => 2, :c => 3}
77
81
  def except(*keys)
78
- reject { |k, _v| keys.flatten.include? k.to_sym }
82
+ reject { |key, _v| keys.flatten.include?(key.to_sym) }
79
83
  end
80
84
 
81
85
  # Usage { :a => 1, :b => 2, :c => 3}.only(:a) -> {:a => 1}
82
86
  def only(*keys)
83
- dup.reject { |k, _v| !keys.flatten.include? k.to_sym }
87
+ dup.reject { |key, _v| !keys.flatten.include?(key.to_sym) }
84
88
  end
85
89
 
86
90
  # Usage h = { :a => 1, :b => 2, :c => 3}.pop(:a) -> {:a => 1}
87
91
  # ... and now h == { :b => 2, :c => 3}
88
92
  def pop(*keys)
89
- r = reject { |k, _v| !keys.flatten.include? k.to_sym }
90
- reject! { |k, _v| keys.flatten.include? k.to_sym }
91
- r
93
+ ret = reject { |key, _v| !keys.flatten.include?(key.to_sym) }
94
+ reject! { |key, _v| keys.flatten.include?(key.to_sym) }
95
+ ret
92
96
  end
93
97
 
94
98
  # Usage { :a => 1, :b => 2, :c => 3}.to_query_string #= a=1&b=2&c=3
95
99
  def to_query_string
96
100
  require 'uri'
97
- map do |k, v|
98
- "#{URI.encode(k.to_s)}=#{URI.encode(v.to_s)}"
101
+ map do |key, value|
102
+ "#{URI.encode(key.to_s)}=#{URI.encode(value.to_s)}"
99
103
  end.join('&')
100
104
  end
101
105
 
@@ -116,8 +120,8 @@ class Hash
116
120
  # Modifies the hash keys in place.
117
121
  def recursively_symbolize_keys!
118
122
  symbolize_keys!
119
- values.each do |v|
120
- v.recursively_symbolize_keys! if v.is_a? Hash
123
+ values.each do |value|
124
+ value.recursively_symbolize_keys! if value.is_a?(Hash)
121
125
  end
122
126
  self
123
127
  end
@@ -0,0 +1,16 @@
1
+ module MidwireCommon
2
+ # Common number behavior
3
+ module NumberBehavior
4
+ # Format a number with commas and a decimal point
5
+ # rubocop:disable Style/PerlBackrefs
6
+ def commify
7
+ to_s =~ /([^\.]*)(\..*)?/
8
+ int = $1.reverse
9
+ dec = $2 ? $2 : ''
10
+ while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3')
11
+ end
12
+ int.reverse + dec
13
+ end
14
+ # rubocop:enable Style/PerlBackrefs
15
+ end
16
+ end
@@ -1,4 +1,3 @@
1
- require 'thor'
2
1
  require 'midwire_common'
3
2
 
4
3
  module MidwireCommon
@@ -2,8 +2,8 @@
2
2
  class String
3
3
  class << self
4
4
  def random(count = 6, ranges = [('a'..'z'), ('A'..'Z'), ('0'..'9')])
5
- o = ranges.map(&:to_a).flatten
6
- (0..(count - 1)).map { o[rand(o.length)] }.join
5
+ coll = ranges.map(&:to_a).flatten
6
+ (0..(count - 1)).map { coll[rand(coll.length)] }.join
7
7
  end
8
8
  end
9
9
 
@@ -1,7 +1,9 @@
1
+ # A more useful Time class
1
2
  class Time
2
3
  class << self
3
- def timestamp
4
- Time.now.strftime('%Y%m%d%H%M%S')
4
+ def timestamp(resolution = 3)
5
+ return Time.now.strftime('%Y%m%d%H%M%S') if resolution < 1
6
+ Time.now.strftime("%Y%m%d%H%M%S.%#{resolution}N")
5
7
  end
6
8
  end
7
9
  end
@@ -1,34 +1,42 @@
1
1
  module MidwireCommon
2
+ # A useful mixing for Time behavior
2
3
  class TimeTool
3
- # converts the given time (HH:MM:SS) to seconds
4
- #
5
- # +time+ the time-string
6
- def self.time_to_seconds(time)
7
- return -1 if time.nil? || time.strip.empty?
8
- times = time.split(/:/).reverse
9
- seconds = 0
10
- (0...times.length).each_with_index do |i|
11
- seconds += times[i].to_i * (60**i)
4
+ class << self
5
+ # converts the given time (HH:MM:SS) to seconds
6
+ #
7
+ # +time+ the time-string
8
+ def time_to_seconds(time)
9
+ return -1 if time.nil? || time.strip.empty?
10
+ times = time.split(/:/).reverse
11
+ seconds = 0
12
+ (0...times.length).each_with_index do |int|
13
+ seconds += times[int].to_i * (60**int)
14
+ end
15
+ seconds
12
16
  end
13
- seconds
14
- end
15
17
 
16
- # converts the given seconds into a time string (HH:MM:SS)
17
- #
18
- # +seconds+ the seconds to convert
19
- def self.seconds_to_time(seconds)
20
- return 'unknown' if seconds.nil?
21
- t = seconds
22
- time = ''
23
- 2.downto(0) do |i|
24
- tmp = t / (60**i)
25
- t -= tmp * 60**i
26
- time += ':' unless time.empty?
27
- # rubocop:disable Style/FormatString
28
- time += ('%02d' % tmp)
29
- # rubocop:enable Style/FormatString
18
+ # converts the given seconds into a time string (HH:MM:SS)
19
+ #
20
+ # +seconds+ the seconds to convert
21
+ def seconds_to_time(seconds)
22
+ return 'unknown' if seconds.nil?
23
+ tsec = seconds
24
+ time = ''
25
+ 2.downto(0) do |int|
26
+ power = to_the_power(60, int)
27
+ tmp = tsec / power
28
+ tsec -= tmp * power
29
+ time += ':' unless time.empty?
30
+ time += format('%02d', tmp)
31
+ end
32
+ time
33
+ end
34
+
35
+ private
36
+
37
+ def to_the_power(base, expon)
38
+ base**expon
30
39
  end
31
- time
32
40
  end
33
41
  end
34
42
  end
@@ -1,6 +1,6 @@
1
1
  original_verbosity = $VERBOSE
2
2
  $VERBOSE = nil
3
3
  module MidwireCommon
4
- VERSION = '0.3.0'.freeze
4
+ VERSION = '1.1.0'.freeze
5
5
  end
6
6
  $VERBOSE = original_verbosity
@@ -1,8 +1,9 @@
1
1
  require 'yaml'
2
2
 
3
3
  module MidwireCommon
4
+ # Simple class for YAML settings
4
5
  class YamlSetting
5
- attr_accessor :file
6
+ attr_reader :file
6
7
  attr_reader :config
7
8
 
8
9
  def initialize(file)
@@ -15,7 +16,7 @@ module MidwireCommon
15
16
  end
16
17
 
17
18
  def save
18
- File.open(file, 'w') { |f| f.write(YAML.dump(config)) }
19
+ File.open(file, 'w') { |thefile| thefile.write(YAML.dump(config)) }
19
20
  self
20
21
  end
21
22
 
@@ -1,6 +1,7 @@
1
1
  require 'pathname'
2
2
  require 'midwire_common/version'
3
3
 
4
+ # Main module namespace
4
5
  module MidwireCommon
5
6
  class << self
6
7
  def root
@@ -8,5 +9,6 @@ module MidwireCommon
8
9
  end
9
10
  end
10
11
 
11
- autoload :RakeHelper, 'midwire_common/rake_helper'
12
+ autoload :NumberBehavior, 'midwire_common/number_behavior'
13
+ autoload :RakeHelper, 'midwire_common/rake_helper'
12
14
  end
@@ -16,6 +16,8 @@ Gem::Specification.new do |spec|
16
16
  spec.test_files = spec.files.grep(%r{^spec/})
17
17
  spec.require_paths = ['lib']
18
18
 
19
+ spec.required_ruby_version = '~> 2'
20
+
19
21
  spec.add_development_dependency 'rspec', '~> 2.14'
20
22
  spec.add_development_dependency 'simplecov', '~> 0.7'
21
23
  spec.add_development_dependency 'guard', '~> 2.11'
@@ -25,6 +27,4 @@ Gem::Specification.new do |spec|
25
27
  spec.add_development_dependency 'pry-nav', '~> 0.2'
26
28
  spec.add_development_dependency 'rake', '~> 10.1'
27
29
  spec.add_development_dependency 'rubocop', '~> 0.36'
28
-
29
- spec.add_dependency 'thor', '~> 0.19'
30
30
  end
@@ -44,9 +44,9 @@ describe Array do
44
44
 
45
45
  it 'can binary search for elements' do
46
46
  a = %w(a b c)
47
- a.bsearch('a').should eq(0)
48
- a.bsearch('b').should eq(1)
49
- a.bsearch('c').should eq(2)
47
+ a.binsearch('a').should eq(0)
48
+ a.binsearch('b').should eq(1)
49
+ a.binsearch('c').should eq(2)
50
50
  end
51
51
 
52
52
  it 'can superjoin elements' do
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe File::Stat do
4
4
  it 'knows on which devise it resides' do
5
- s = File::Stat.device_name('/tmp')
6
- s.should_not be_nil
5
+ dev = File::Stat.device_name('/tmp')
6
+ dev.should_not be_nil
7
7
  end
8
8
  end
@@ -1,10 +1,47 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Time do
4
- it 'generates a timestamp, appropriate for a filename' do
5
- ts = Time.timestamp
6
- expect(ts.length).to eq(14)
7
- expect(ts.alpha_numeric?).to eq(true)
8
- expect(ts.numeric?).to eq(true)
4
+ context 'generates a timestamp' do
5
+ it 'appropriate for a filename' do
6
+ ts = Time.timestamp
7
+ expect(ts.length).to eq(18)
8
+ expect(ts.numeric?).to eq(true)
9
+ end
10
+
11
+ it 'with the only seconds' do
12
+ ts = Time.timestamp(0)
13
+ expect(ts.length).to eq(14)
14
+ expect(ts.numeric?).to eq(true)
15
+ end
16
+
17
+ it 'defaults to nanosecond resolution' do
18
+ ts = Time.timestamp
19
+ expect(ts.length).to eq(18)
20
+ expect(ts.numeric?).to eq(true)
21
+ end
22
+
23
+ it 'with the millisecond resolution' do
24
+ ts = Time.timestamp(3)
25
+ expect(ts.length).to eq(18)
26
+ expect(ts.numeric?).to eq(true)
27
+ end
28
+
29
+ it 'with the microsecond resolution' do
30
+ ts = Time.timestamp(6)
31
+ expect(ts.length).to eq(21)
32
+ expect(ts.numeric?).to eq(true)
33
+ end
34
+
35
+ it 'with the nanosecond resolution' do
36
+ ts = Time.timestamp(9)
37
+ expect(ts.length).to eq(24)
38
+ expect(ts.numeric?).to eq(true)
39
+ end
40
+
41
+ it 'with the picosecond resolution' do
42
+ ts = Time.timestamp(12)
43
+ expect(ts.length).to eq(27)
44
+ expect(ts.numeric?).to eq(true)
45
+ end
9
46
  end
10
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: midwire_common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Blackburn
@@ -136,20 +136,6 @@ dependencies:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0.36'
139
- - !ruby/object:Gem::Dependency
140
- name: thor
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - "~>"
144
- - !ruby/object:Gem::Version
145
- version: '0.19'
146
- type: :runtime
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - "~>"
151
- - !ruby/object:Gem::Version
152
- version: '0.19'
153
139
  description: A useful Ruby library
154
140
  email:
155
141
  - 87a1779b@opayq.com
@@ -176,6 +162,7 @@ files:
176
162
  - lib/midwire_common/fixnum.rb
177
163
  - lib/midwire_common/float.rb
178
164
  - lib/midwire_common/hash.rb
165
+ - lib/midwire_common/number_behavior.rb
179
166
  - lib/midwire_common/rake_helper.rb
180
167
  - lib/midwire_common/rake_tasks.rb
181
168
  - lib/midwire_common/string.rb
@@ -208,9 +195,9 @@ require_paths:
208
195
  - lib
209
196
  required_ruby_version: !ruby/object:Gem::Requirement
210
197
  requirements:
211
- - - ">="
198
+ - - "~>"
212
199
  - !ruby/object:Gem::Version
213
- version: '0'
200
+ version: '2'
214
201
  required_rubygems_version: !ruby/object:Gem::Requirement
215
202
  requirements:
216
203
  - - ">="
@@ -218,7 +205,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
218
205
  version: '0'
219
206
  requirements: []
220
207
  rubyforge_project:
221
- rubygems_version: 2.4.5
208
+ rubygems_version: 2.4.5.1
222
209
  signing_key:
223
210
  specification_version: 4
224
211
  summary: Midwire Tech Ruby Library