midwire_common 0.3.0 → 1.1.0
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/.ruby-version +1 -1
 - data/CHANGELOG +11 -0
 - data/README.md +3 -1
 - data/lib/midwire_common/array.rb +12 -11
 - data/lib/midwire_common/data_file_cache.rb +3 -3
 - data/lib/midwire_common/enumerable.rb +4 -3
 - data/lib/midwire_common/file/stat.rb +4 -2
 - data/lib/midwire_common/fixnum.rb +2 -20
 - data/lib/midwire_common/float.rb +2 -10
 - data/lib/midwire_common/hash.rb +14 -10
 - data/lib/midwire_common/number_behavior.rb +16 -0
 - data/lib/midwire_common/rake_helper.rb +0 -1
 - data/lib/midwire_common/string.rb +2 -2
 - data/lib/midwire_common/time.rb +4 -2
 - data/lib/midwire_common/time_tool.rb +34 -26
 - data/lib/midwire_common/version.rb +1 -1
 - data/lib/midwire_common/yaml_setting.rb +3 -2
 - data/lib/midwire_common.rb +3 -1
 - data/midwire_common.gemspec +2 -2
 - data/spec/lib/midwire_common/array_spec.rb +3 -3
 - data/spec/lib/midwire_common/file/stat_spec.rb +2 -2
 - data/spec/lib/midwire_common/time_spec.rb +42 -5
 - metadata +5 -18
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 3e7d213c8109f31ef945f647152122b7f8520363
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 71c130a18c5ca6ab637572a5c48bbb9f0598b54b
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: ed228a57311853ac1af82e122cd25b5d77f95280d9108355ab8164f6a6a1a8e6e42241578d58799ce1d1282f7269ab0112a2eddac2b7a4093b187760428f1c94
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 5650ead282aa147f27e5830ef02022b25d297395a0df6e20346def21b8fc8474e483329a180da5a411e62d100eb4aeceee2039363d1a247bf89aca3d3485ea32
         
     | 
    
        data/.ruby-version
    CHANGED
    
    | 
         @@ -1 +1 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            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
    
    
    
        data/lib/midwire_common/array.rb
    CHANGED
    
    | 
         @@ -1,8 +1,9 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # A light-weight super Array class
         
     | 
| 
       1 
2 
     | 
    
         
             
            class Array
         
     | 
| 
       2 
3 
     | 
    
         
             
              def count_occurrences
         
     | 
| 
       3 
     | 
    
         
            -
                 
     | 
| 
       4 
     | 
    
         
            -
                each { | 
     | 
| 
       5 
     | 
    
         
            -
                 
     | 
| 
      
 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,  
     | 
| 
       22 
     | 
    
         
            -
                  case  
     | 
| 
      
 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  
     | 
| 
       34 
     | 
    
         
            -
                return if l>u;m=(l+u)/2;e<self[m]?u=m-1:l=m+1;e==self[m]?m: 
     | 
| 
      
 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 
     | 
    
         
            -
                 
     | 
| 
      
 47 
     | 
    
         
            +
                dim = ldescr[0]
         
     | 
| 
       47 
48 
     | 
    
         
             
                rest = ldescr[1..-1]
         
     | 
| 
       48 
     | 
    
         
            -
                 
     | 
| 
       49 
     | 
    
         
            -
                  ( 
     | 
| 
       50 
     | 
    
         
            -
                end.join( 
     | 
| 
      
 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 
     | 
    
         
            -
                   
     | 
| 
       12 
     | 
    
         
            -
                   
     | 
| 
       13 
     | 
    
         
            -
                  File.open(@cache_file, 'w') { |file| file.write( 
     | 
| 
      
 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 | 
     | 
| 
       5 
     | 
    
         
            -
                  hash[ 
     | 
| 
      
 5 
     | 
    
         
            +
                histogram = each_with_object(Hash.new(0)) do |elem, hash|
         
     | 
| 
      
 6 
     | 
    
         
            +
                  hash[elem] += 1
         
     | 
| 
       6 
7 
     | 
    
         
             
                  hash
         
     | 
| 
       7 
8 
     | 
    
         
             
                end
         
     | 
| 
       8 
     | 
    
         
            -
                sort_by { | 
     | 
| 
      
 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 | 
     | 
| 
       5 
     | 
    
         
            -
                     
     | 
| 
      
 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 
     | 
    
         
            -
               
     | 
| 
       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
         
     | 
    
        data/lib/midwire_common/float.rb
    CHANGED
    
    | 
         @@ -1,12 +1,4 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # A more useful Float class
         
     | 
| 
       1 
2 
     | 
    
         
             
            class Float
         
     | 
| 
       2 
     | 
    
         
            -
               
     | 
| 
       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
         
     | 
    
        data/lib/midwire_common/hash.rb
    CHANGED
    
    | 
         @@ -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(&-> ( 
     | 
| 
      
 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 { | 
     | 
| 
      
 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 { | 
     | 
| 
      
 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 
     | 
    
         
            -
                 
     | 
| 
       90 
     | 
    
         
            -
                reject! { | 
     | 
| 
       91 
     | 
    
         
            -
                 
     | 
| 
      
 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 | 
     | 
| 
       98 
     | 
    
         
            -
                  "#{URI.encode( 
     | 
| 
      
 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 | 
     | 
| 
       120 
     | 
    
         
            -
                   
     | 
| 
      
 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
         
     | 
| 
         @@ -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 
     | 
    
         
            -
                   
     | 
| 
       6 
     | 
    
         
            -
                  (0..(count - 1)).map {  
     | 
| 
      
 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 
     | 
    
         | 
    
        data/lib/midwire_common/time.rb
    CHANGED
    
    | 
         @@ -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 
     | 
    
         
            -
                 
     | 
| 
       4 
     | 
    
         
            -
             
     | 
| 
       5 
     | 
    
         
            -
             
     | 
| 
       6 
     | 
    
         
            -
             
     | 
| 
       7 
     | 
    
         
            -
                   
     | 
| 
       8 
     | 
    
         
            -
             
     | 
| 
       9 
     | 
    
         
            -
             
     | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
     | 
    
         
            -
                     
     | 
| 
      
 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 
     | 
    
         
            -
             
     | 
| 
       17 
     | 
    
         
            -
             
     | 
| 
       18 
     | 
    
         
            -
             
     | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
       20 
     | 
    
         
            -
             
     | 
| 
       21 
     | 
    
         
            -
             
     | 
| 
       22 
     | 
    
         
            -
             
     | 
| 
       23 
     | 
    
         
            -
             
     | 
| 
       24 
     | 
    
         
            -
             
     | 
| 
       25 
     | 
    
         
            -
             
     | 
| 
       26 
     | 
    
         
            -
             
     | 
| 
       27 
     | 
    
         
            -
             
     | 
| 
       28 
     | 
    
         
            -
             
     | 
| 
       29 
     | 
    
         
            -
                     
     | 
| 
      
 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,8 +1,9 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            require 'yaml'
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            module MidwireCommon
         
     | 
| 
      
 4 
     | 
    
         
            +
              # Simple class for YAML settings
         
     | 
| 
       4 
5 
     | 
    
         
             
              class YamlSetting
         
     | 
| 
       5 
     | 
    
         
            -
                 
     | 
| 
      
 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') { | 
     | 
| 
      
 19 
     | 
    
         
            +
                  File.open(file, 'w') { |thefile| thefile.write(YAML.dump(config)) }
         
     | 
| 
       19 
20 
     | 
    
         
             
                  self
         
     | 
| 
       20 
21 
     | 
    
         
             
                end
         
     | 
| 
       21 
22 
     | 
    
         | 
    
        data/lib/midwire_common.rb
    CHANGED
    
    | 
         @@ -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 : 
     | 
| 
      
 12 
     | 
    
         
            +
              autoload :NumberBehavior, 'midwire_common/number_behavior'
         
     | 
| 
      
 13 
     | 
    
         
            +
              autoload :RakeHelper,     'midwire_common/rake_helper'
         
     | 
| 
       12 
14 
     | 
    
         
             
            end
         
     | 
    
        data/midwire_common.gemspec
    CHANGED
    
    | 
         @@ -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. 
     | 
| 
       48 
     | 
    
         
            -
                a. 
     | 
| 
       49 
     | 
    
         
            -
                a. 
     | 
| 
      
 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
         
     | 
| 
         @@ -1,10 +1,47 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            require 'spec_helper'
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            describe Time do
         
     | 
| 
       4 
     | 
    
         
            -
               
     | 
| 
       5 
     | 
    
         
            -
                 
     | 
| 
       6 
     | 
    
         
            -
             
     | 
| 
       7 
     | 
    
         
            -
             
     | 
| 
       8 
     | 
    
         
            -
             
     | 
| 
      
 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:  
     | 
| 
      
 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: ' 
     | 
| 
      
 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
         
     |