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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 610b3c6906cff1da83a54ea846a91053db886a99
|
4
|
+
data.tar.gz: cce85b69aa0979b7ba4d83099409a3cebf958ebc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f01eb7ce5297c9c1b99569b9864a136bd58e1614036f007ecc8f5be9f69fd3cdc4f3ebe8592a13eba5385faac9bc5d51cc900d0cf2d5260416e91f01b82581ae
|
7
|
+
data.tar.gz: 12e6d698c9c448d03d94ec9f052ae0eb62389f7701c7093bd80668898bbbb04b140838c740cb9eb82908ece1f49a342051034372d8c75fe31904a72538a2a2c1
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
Style/Documentation:
|
2
|
+
Enabled: false
|
3
|
+
|
4
|
+
Style/AlignParameters:
|
5
|
+
EnforcedStyle: with_fixed_indentation
|
6
|
+
|
7
|
+
Metrics/MethodLength:
|
8
|
+
CountComments: false # count full line comments?
|
9
|
+
Max: 16
|
10
|
+
|
11
|
+
Style/RegexpLiteral:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Encoding:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
# MethodLength:
|
18
|
+
# CountComments: false
|
19
|
+
# Max: 30
|
20
|
+
|
21
|
+
#AlignHash:
|
22
|
+
# EnforcedColonStyle: table
|
23
|
+
# EnforcedHashRocketStyle: table
|
24
|
+
|
25
|
+
HashSyntax:
|
26
|
+
Enabled: true
|
27
|
+
|
28
|
+
Lambda:
|
29
|
+
Description: 'Use the new lambda literal syntax for single-line blocks.'
|
30
|
+
Enabled: true
|
31
|
+
|
32
|
+
LambdaCall:
|
33
|
+
Description: 'Use lambda.call(...) instead of lambda.(...).'
|
34
|
+
Enabled: true
|
35
|
+
|
36
|
+
LineLength:
|
37
|
+
Max: 80
|
38
|
+
|
39
|
+
AlignParameters:
|
40
|
+
Description: >
|
41
|
+
Align the parameters of a method call if they span more
|
42
|
+
than one line.
|
43
|
+
Enabled: false
|
44
|
+
|
45
|
+
# Checks formatting of special comments
|
46
|
+
CommentAnnotation:
|
47
|
+
Keywords:
|
48
|
+
- TODO
|
49
|
+
- FIXME
|
50
|
+
- OPTIMIZE
|
51
|
+
- HACK
|
52
|
+
- REVIEW
|
53
|
+
|
54
|
+
AllCops:
|
55
|
+
# Include gemspec and Rakefile
|
56
|
+
Include:
|
57
|
+
- '**/*.gemspec'
|
58
|
+
- '**/*.podspec'
|
59
|
+
- '**/*.jbuilder'
|
60
|
+
- '**/*.rake'
|
61
|
+
- '**/Gemfile'
|
62
|
+
- '**/Rakefile'
|
63
|
+
- '**/Capfile'
|
64
|
+
- '**/Guardfile'
|
65
|
+
- '**/Podfile'
|
66
|
+
- '**/Thorfile'
|
67
|
+
- '**/Vagrantfile'
|
68
|
+
Exclude:
|
69
|
+
- 'vendor/**/*'
|
70
|
+
- 'stubs/**/*'
|
71
|
+
|
72
|
+
Lint/Eval:
|
73
|
+
Enabled: false
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.1.
|
1
|
+
2.1.5
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
*0.1.13* (January 07, 2015)
|
2
|
+
|
3
|
+
* Upgrade to Ruby 2.x idiosyncracies, i.e., Fixnum already provides 'odd?', 'even?' methods, so I removed those, etc.
|
4
|
+
* Fix lots of Rubocop issues.
|
5
|
+
|
1
6
|
*0.1.12* (December 22, 2014)
|
2
7
|
|
3
8
|
* Add diff, apply_diff and apply_diff! instance methods for Hash
|
data/Gemfile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
gemspec
|
data/Guardfile
CHANGED
@@ -1,11 +1,14 @@
|
|
1
|
-
|
1
|
+
CLI = 'bundle exec rspec --format doc --color'
|
2
|
+
|
3
|
+
guard 'rspec', cmd: CLI, all_on_start: false, all_after_pass: false do
|
2
4
|
watch(%r{^spec/.+_spec\.rb$})
|
3
5
|
watch('spec/spec_helper.rb') { 'spec' }
|
4
6
|
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
5
7
|
watch(%r{^lib/midwire_common/all.rb$}) { 'spec' }
|
6
8
|
end
|
7
9
|
|
8
|
-
guard :rubocop do
|
10
|
+
guard :rubocop, cli: ['-D'] do
|
9
11
|
watch(%r{.+\.rb$})
|
12
|
+
watch(%r{.+\.rake$})
|
10
13
|
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
|
11
14
|
end
|
data/README.md
CHANGED
data/Rakefile
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
|
-
require
|
3
|
-
Dir[
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
Dir['lib/tasks/**/*.rake'].sort.each { |ext| load ext }
|
4
4
|
|
5
5
|
# RSpec Tasks
|
6
6
|
require 'rspec/core/rake_task'
|
7
7
|
RSpec::Core::RakeTask.new('spec')
|
8
|
-
task :
|
8
|
+
task default: :spec
|
data/lib/midwire_common.rb
CHANGED
data/lib/midwire_common/array.rb
CHANGED
@@ -14,23 +14,26 @@ class Array
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def sort_case_insensitive
|
17
|
-
|
17
|
+
sort_by(&:downcase)
|
18
18
|
end
|
19
19
|
|
20
20
|
def each_with_first_last(first_code, main_code, last_code)
|
21
21
|
each_with_index do |item, i|
|
22
22
|
case i
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
when 0 then first_code.call(item)
|
24
|
+
when size - 1 then last_code.call(item)
|
25
|
+
else main_code.call(item)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
# Binary search returns index if found or nil
|
31
|
+
# rubocop:disable Metrics/LineLength
|
32
|
+
# rubocop:disable Style/SpaceAfterComma, Style/SpaceAroundOperators, Style/SpaceAfterColon, Style/SpaceAfterSemicolon, Style/Semicolon
|
31
33
|
def bsearch(e, l = 0, u = length - 1)
|
32
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)
|
33
35
|
end
|
36
|
+
# rubocop:enable Style/SpaceAfterComma, Style/SpaceAroundOperators, Style/SpaceAfterColon, Style/SpaceAfterSemicolon, Style/Semicolon
|
34
37
|
|
35
38
|
# Make a string from a multi-dimensional array :
|
36
39
|
# 1 dimension :
|
@@ -45,4 +48,5 @@ class Array
|
|
45
48
|
(a.respond_to?(:superjoin) && rest.length > 0) ? a.superjoin(*rest) : a.to_s
|
46
49
|
end.join(d[1]) + d[2]
|
47
50
|
end
|
51
|
+
# rubocop:enable Metrics/LineLength
|
48
52
|
end
|
@@ -1,7 +1,12 @@
|
|
1
1
|
module Enumerable
|
2
2
|
# Sort by frequency of occurrence
|
3
|
+
# rubocop:disable Style/EachWithObject
|
3
4
|
def sort_by_frequency
|
4
|
-
histogram = inject(Hash.new(0))
|
5
|
-
|
5
|
+
histogram = inject(Hash.new(0)) do |hash, x|
|
6
|
+
hash[x] += 1
|
7
|
+
hash
|
8
|
+
end
|
9
|
+
sort_by { |x| [histogram[x] * -1, x] }
|
6
10
|
end
|
11
|
+
# rubocop:enable Style/EachWithObject
|
7
12
|
end
|
data/lib/midwire_common/file.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
class File
|
2
2
|
class Stat
|
3
3
|
def self.device_name(file)
|
4
|
-
Dir['/dev/*'].inject({})
|
4
|
+
Dir['/dev/*'].inject({}) do |h, v|
|
5
5
|
h.update(File.stat(v).rdev => v)
|
6
|
-
|
6
|
+
end.values_at(File.stat(file).dev).first || nil
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
@@ -1,18 +1,22 @@
|
|
1
1
|
class Fixnum
|
2
2
|
# Format a number with commas and a decimal point
|
3
|
+
# rubocop:disable Style/PerlBackrefs
|
3
4
|
def commify
|
4
5
|
to_s =~ /([^\.]*)(\..*)?/
|
5
|
-
int, dec = $1.reverse, $2 ? $2 :
|
6
|
+
int, dec = $1.reverse, $2 ? $2 : ''
|
6
7
|
while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3')
|
7
8
|
end
|
8
9
|
int.reverse + dec
|
9
10
|
end
|
11
|
+
# rubocop:enable Style/PerlBackrefs
|
10
12
|
|
11
|
-
|
12
|
-
self % 2 == 0
|
13
|
-
end
|
13
|
+
# Ruby 2 has its own methods for these
|
14
14
|
|
15
|
-
def
|
16
|
-
|
17
|
-
end
|
15
|
+
# def is_even?
|
16
|
+
# self % 2 == 0
|
17
|
+
# end
|
18
|
+
|
19
|
+
# def is_odd?
|
20
|
+
# !is_even?
|
21
|
+
# end
|
18
22
|
end
|
data/lib/midwire_common/float.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
class Float
|
2
2
|
# Format a number with commas and a decimal point
|
3
|
+
# rubocop:disable Style/PerlBackrefs
|
3
4
|
def commify
|
4
5
|
to_s =~ /([^\.]*)(\..*)?/
|
5
|
-
int, dec = $1.reverse, $2 ? $2 :
|
6
|
+
int, dec = $1.reverse, $2 ? $2 : ''
|
6
7
|
while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3')
|
7
8
|
end
|
8
9
|
int.reverse + dec
|
9
10
|
end
|
11
|
+
# rubocop:enable Style/PerlBackrefs
|
10
12
|
end
|
data/lib/midwire_common/hash.rb
CHANGED
@@ -1,15 +1,18 @@
|
|
1
1
|
class Hash
|
2
|
+
# rubocop:disable Style/EachWithObject
|
2
3
|
def grep(pattern)
|
3
|
-
|
4
|
-
res << kv if kv[0] =~ pattern
|
4
|
+
reduce([]) do |res, kv|
|
5
|
+
res << kv if kv[0] =~ pattern || kv[1] =~ pattern
|
5
6
|
res
|
6
7
|
end
|
7
8
|
end
|
9
|
+
# rubocop:enable Style/EachWithObject
|
8
10
|
|
11
|
+
# rubocop:disable Metrics/AbcSize, Style/EachWithObject
|
9
12
|
def diff(other)
|
10
|
-
(
|
13
|
+
(keys + other.keys).uniq.inject({}) do |memo, key|
|
11
14
|
unless self[key] == other[key]
|
12
|
-
if self[key].
|
15
|
+
if self[key].is_a?(Hash) && other[key].is_a?(Hash)
|
13
16
|
memo[key] = self[key].diff(other[key])
|
14
17
|
else
|
15
18
|
memo[key] = [self[key], other[key]]
|
@@ -18,92 +21,91 @@ class Hash
|
|
18
21
|
memo
|
19
22
|
end
|
20
23
|
end
|
24
|
+
# rubocop:enable Metrics/AbcSize, Style/EachWithObject
|
21
25
|
|
26
|
+
# TODO: Spec/Test this method
|
22
27
|
def apply_diff!(changes, direction = :right)
|
23
28
|
path = [[self, changes]]
|
24
29
|
pos, local_changes = path.pop
|
25
30
|
while local_changes
|
26
|
-
local_changes.each_pair
|
27
|
-
if change.
|
31
|
+
local_changes.each_pair do |key, change|
|
32
|
+
if change.is_a?(Array)
|
28
33
|
pos[key] = (direction == :right) ? change[1] : change[0]
|
29
34
|
else
|
30
35
|
path.push([pos[key], change])
|
31
36
|
end
|
32
|
-
|
37
|
+
end
|
33
38
|
pos, local_changes = path.pop
|
34
39
|
end
|
35
40
|
self
|
36
41
|
end
|
37
42
|
|
43
|
+
# TODO: Spec/Test this method
|
44
|
+
# rubocop:disable Metrics/AbcSize
|
38
45
|
def apply_diff(changes, direction = :right)
|
39
|
-
cloned =
|
46
|
+
cloned = clone
|
40
47
|
path = [[cloned, changes]]
|
41
48
|
pos, local_changes = path.pop
|
42
49
|
while local_changes
|
43
|
-
local_changes.each_pair
|
44
|
-
if change.
|
50
|
+
local_changes.each_pair do |key, change|
|
51
|
+
if change.is_a?(Array)
|
45
52
|
pos[key] = (direction == :right) ? change[1] : change[0]
|
46
53
|
else
|
47
54
|
pos[key] = pos[key].clone
|
48
55
|
path.push([pos[key], change])
|
49
56
|
end
|
50
|
-
|
57
|
+
end
|
51
58
|
pos, local_changes = path.pop
|
52
59
|
end
|
53
60
|
cloned
|
54
61
|
end
|
62
|
+
# rubocop:enable Metrics/AbcSize
|
55
63
|
|
56
64
|
# Usage { :a => 1, :b => 2, :c => 3}.except(:a) -> { :b => 2, :c => 3}
|
57
65
|
def except(*keys)
|
58
|
-
|
59
|
-
keys.flatten.include? k.to_sym
|
60
|
-
}
|
66
|
+
reject { |k, _v| keys.flatten.include? k.to_sym }
|
61
67
|
end
|
62
68
|
|
63
69
|
# Usage { :a => 1, :b => 2, :c => 3}.only(:a) -> {:a => 1}
|
64
70
|
def only(*keys)
|
65
|
-
|
66
|
-
!keys.flatten.include? k.to_sym
|
67
|
-
}
|
71
|
+
dup.reject { |k, _v| !keys.flatten.include? k.to_sym }
|
68
72
|
end
|
69
73
|
|
70
74
|
# Usage h = { :a => 1, :b => 2, :c => 3}.pop(:a) -> {:a => 1}
|
71
75
|
# ... and now h == { :b => 2, :c => 3}
|
72
76
|
def pop(*keys)
|
73
|
-
r =
|
74
|
-
|
75
|
-
}
|
76
|
-
self.reject! { |k,v|
|
77
|
-
keys.flatten.include? k.to_sym
|
78
|
-
}
|
77
|
+
r = reject { |k, _v| !keys.flatten.include? k.to_sym }
|
78
|
+
self.reject! { |k, _v| keys.flatten.include? k.to_sym }
|
79
79
|
r
|
80
80
|
end
|
81
81
|
|
82
82
|
# Usage { :a => 1, :b => 2, :c => 3}.to_query_string #= a=1&b=2&c=3
|
83
83
|
def to_query_string
|
84
84
|
require 'uri'
|
85
|
-
|
85
|
+
map do |k, v|
|
86
86
|
"#{URI.encode(k.to_s)}=#{URI.encode(v.to_s)}"
|
87
|
-
end.join(
|
87
|
+
end.join('&')
|
88
88
|
end
|
89
89
|
|
90
|
+
# rubocop:disable Style/RescueModifier
|
90
91
|
def symbolize_keys!
|
91
92
|
keys.each do |key|
|
92
93
|
self[(key.to_sym rescue key) || key] = delete(key)
|
93
94
|
end
|
94
95
|
self
|
95
96
|
end
|
97
|
+
# rubocop:enable Style/RescueModifier
|
96
98
|
|
97
99
|
def symbolize_keys
|
98
100
|
dup.symbolize_keys!
|
99
101
|
end
|
100
102
|
|
103
|
+
# Recursively change keys to symbols
|
104
|
+
# Modifies the hash keys in place.
|
101
105
|
def recursively_symbolize_keys!
|
102
106
|
self.symbolize_keys!
|
103
|
-
|
104
|
-
if v.is_a? Hash
|
105
|
-
v.recursively_symbolize_keys!
|
106
|
-
end
|
107
|
+
values.each do |v|
|
108
|
+
v.recursively_symbolize_keys! if v.is_a? Hash
|
107
109
|
end
|
108
110
|
self
|
109
111
|
end
|
@@ -3,6 +3,7 @@ require 'midwire_common'
|
|
3
3
|
|
4
4
|
module MidwireCommon
|
5
5
|
# RakeHelper helps to automate gem release and versioning tasks
|
6
|
+
# rubocop:disable Metrics/ClassLength
|
6
7
|
class RakeHelper
|
7
8
|
include Rake::DSL if defined? Rake::DSL
|
8
9
|
|
@@ -118,7 +119,11 @@ module MidwireCommon
|
|
118
119
|
|Running `#{cmd}' failed.
|
119
120
|
| Run this command directly for more detailed output.
|
120
121
|
string
|
121
|
-
code == 0
|
122
|
+
if code == 0
|
123
|
+
out
|
124
|
+
else
|
125
|
+
fail(out.empty? ? msg : out)
|
126
|
+
end
|
122
127
|
end
|
123
128
|
|
124
129
|
def sh_with_code(cmd, &block)
|
@@ -127,11 +132,10 @@ module MidwireCommon
|
|
127
132
|
Bundler.ui.debug(cmd)
|
128
133
|
Dir.chdir(base) do
|
129
134
|
outbuf = `#{cmd}`
|
130
|
-
if
|
131
|
-
block.call(outbuf) if block
|
132
|
-
end
|
135
|
+
block.call(outbuf) if block if $CHILD_STATUS == 0
|
133
136
|
end
|
134
|
-
[outbuf,
|
137
|
+
[outbuf, $CHILD_STATUS]
|
135
138
|
end
|
136
139
|
end
|
140
|
+
# rubocop:enable Metrics/ClassLength
|
137
141
|
end
|