skn_utils 5.2.0 → 5.3.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: 6626371d3c94b3bc3907c2076960641a49019628
4
- data.tar.gz: 51fc2a420168d5d9d760cccc4dae8a2d7dbd4d13
3
+ metadata.gz: 5b812d7904cab21c79e37afec4bf5c9eb2f67336
4
+ data.tar.gz: 966367ddcb978a3bb2eb6eeea71a89c93b3ee347
5
5
  SHA512:
6
- metadata.gz: 5eee749ed9ad4ccc4f4fd7c5c9d464fd459650d197b3c30acbbeace7799b88ebff4e1774e3a75ad8ed24e3f0a0fdfd9f0f3ea213eca6b67ed0797610f4caac73
7
- data.tar.gz: dda45c33fa7e7d6509f6369fb84504fb0f146baa698fffe3b5c42c95bb73b02d9d168509f0b380d259d5118dd5cacd3424715556d6f09b195e4c3663daa38885
6
+ metadata.gz: 595bf74de49521fe61e6ec10dee7f1c56b97798096ff540e03e1bdc92e92c82e7b805018150bfb0495bca45b74c1c76ac6c9bc38aee19b774a8a081f2b6aa327
7
+ data.tar.gz: 1b57076cadc213712426727bcaac68a546f99f0cab187b15618b8799d106b8019d056742aa87ef092af9f271e7cf6a704bc43990ab6dd7d84fcdeb7ea7bd06d2
data/README.md CHANGED
@@ -30,8 +30,8 @@ Ruby's Hash object is already extremely flexible, even more so with the addition
30
30
  1. Command registries used to dispatch command requests to proper command handler. see example app [SknBase](https://github.com/skoona/skn_base/blob/master/strategy/services/content/command_handler.rb)
31
31
  ```ruby
32
32
  SknSettings.registry = {
33
- Services::Content::Commands::RetrieveAvailableResources => method(:resources_metadata_service),
34
- Services::Content::Commands::RetrieveResourceContent => method(:resource_content_service)
33
+ Commands::RetrieveAvailableResources => method(:resources_metadata_service),
34
+ Commands::RetrieveResourceContent => method(:resource_content_service)
35
35
  }
36
36
  ...
37
37
  SknSettings.registry[ cmd.class ].call( cmd )
@@ -60,6 +60,12 @@ There are many more use cases for Ruby's Hash that this gem just makes easier to
60
60
 
61
61
 
62
62
  ## History
63
+ 11/09/2018 V5.3.0
64
+ Added two utils to SknUtils class:
65
+ #catch_exceptions(&blk) #=> catch all exceptions and retry block x times
66
+ #as_human_size(12345) #=> 12 KB
67
+ - See related RSpecs
68
+
63
69
  10/17/2018 V5.1.3
64
70
  Enhanced SknUtils::Configurable to include a #registry method/value at its root, like Clas.registry or Class.root
65
71
  - Right now these are Class methods only, will update them to survive #new later.
@@ -153,8 +159,9 @@ There are many more use cases for Ruby's Hash that this gem just makes easier to
153
159
 
154
160
  SknContainer/SknRegistry # Basic Key/Value container which #registers and #resolves procs, classes, and/or object
155
161
 
156
- SknSuccess # Three attribute value containers for return codes -- #success, #message, #value
157
- SknFailure # Three attribute value containers for return codes -- #success, #message, #value
162
+ SknSuccess # Three attribute value containers for return codes -- #value, #message, #success
163
+ - Extra #payload method returns value as NestResult if value is_a Hash
164
+ SknFailure # Three attribute value containers for return codes -- #value, #message, #success
158
165
 
159
166
 
160
167
  ## Public Methods: SknUtils::Configurable module
data/docs/.gitkeep ADDED
@@ -0,0 +1 @@
1
+ keep
data/lib/skn_failure.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # ##
2
2
  # Bad Result
3
3
  #
4
- # Syntax: SknFailure.call(payload, message=nil, bool_code=false)
4
+ # Syntax: SknFailure.call(value, message=nil, bool_code=false)
5
5
  #
6
6
 
7
7
  class SknFailure
@@ -16,12 +16,15 @@ class SknFailure
16
16
  @value = val || "Failure"
17
17
  @message = msg || ''
18
18
  @success = rc.nil? ? false : rc
19
- @_payload = val.kind_of?(Hash) ? SknUtils::DottedHash.new(val) : nil
20
- # puts "#{self.class.name} => val:#{val}, rc:#{rc}, msg:#{msg}, args:#{args}"
21
- # puts "#{self.class.name} => @val:#{@value}, @rc:#{@success}, @msg:#{@message}"
22
19
  end
23
20
 
24
21
  def payload
25
- @_payload || @value
22
+ if defined?(@_payload)
23
+ @_payload
24
+ elsif value.kind_of?(Hash)
25
+ @_payload = SknUtils::DottedHash.new(value)
26
+ else
27
+ value
28
+ end
26
29
  end
27
30
  end
data/lib/skn_success.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # ##
2
2
  # Good Result
3
3
  #
4
- # Syntax: SknSuccess.call(payload, message=nil, bool_code=true)
4
+ # Syntax: SknSuccess.call(value, message=nil, bool_code=true)
5
5
  #
6
6
 
7
7
  class SknSuccess
@@ -16,12 +16,15 @@ class SknSuccess
16
16
  @value = val || "Success"
17
17
  @message = msg || ''
18
18
  @success = rc.nil? ? true : rc
19
- @_payload = val.kind_of?(Hash) ? SknUtils::DottedHash.new(val) : nil
20
- # puts "#{self.class.name} => val:#{val}, rc:#{rc}, msg:#{msg}, args:#{args}"
21
- # puts "#{self.class.name} => @val:#{@value}, @rc:#{@success}, @msg:#{@message}"
22
19
  end
23
20
 
24
21
  def payload
25
- @_payload || @value
22
+ if defined?(@_payload)
23
+ @_payload
24
+ elsif value.kind_of?(Hash)
25
+ @_payload = SknUtils::DottedHash.new(value.to_h)
26
+ else
27
+ value
28
+ end
26
29
  end
27
30
  end
data/lib/skn_utils.rb CHANGED
@@ -6,7 +6,6 @@ require 'erb'
6
6
  require 'date'
7
7
  require 'time'
8
8
  require 'concurrent'
9
- require 'colorize'
10
9
  unless defined?(Rails)
11
10
  begin
12
11
  require 'deep_merge'
@@ -38,6 +37,7 @@ module SknUtils
38
37
 
39
38
  # Random Utils
40
39
  # Retries block up to :retries times with a :pause_between, and returns Success/Failure object
40
+ # -- return SknSuccess | SknFailure response object
41
41
  #
42
42
  def self.catch_exceptions(retries=3, pause_between=3, &block)
43
43
  retry_count ||= 1
@@ -46,8 +46,8 @@ module SknUtils
46
46
 
47
47
  SknSuccess.( yield )
48
48
 
49
- rescue StandardError => error
50
- Kernel.puts "#{retry_count} - #{error.class.name}:#{error.message}".light_blue.italic
49
+ rescue StandardError, ScriptError => error
50
+ puts "#{retry_count} - #{error.class.name}:#{error.message}"
51
51
  if retry_count <= attempts
52
52
  retry_count+= 1
53
53
  sleep(pause_between)
@@ -58,4 +58,24 @@ module SknUtils
58
58
  end
59
59
  end # end method
60
60
 
61
+
62
+ # ##
63
+ # SknUtils.as_human_size(12345) #=> 12 KB
64
+ #
65
+ def self.as_human_size(number)
66
+ units = %W(Bytes KB MB GB TB PB EB)
67
+ num = number.to_f
68
+ if number < 1001
69
+ num = number
70
+ exp = 0
71
+ else
72
+ max_exp = units.size - 1
73
+ exp = ( Math.log( num ) / Math.log( 1024 ) ).round
74
+ exp = max_exp if exp > max_exp
75
+ num /= 1024 ** exp
76
+ end
77
+ ((num > 9 || num.modulo(1) < 0.1) ? '%d %s' : '%.1f %s') % [num, units[exp]]
78
+ end
79
+
80
+
61
81
  end
@@ -2,7 +2,7 @@
2
2
  module SknUtils
3
3
  class Version
4
4
  MAJOR = 5
5
- MINOR = 2
5
+ MINOR = 3
6
6
  PATCH = 0
7
7
 
8
8
  def self.to_s
data/skn_utils.gemspec CHANGED
@@ -33,7 +33,6 @@ EOF
33
33
  spec.add_runtime_dependency 'deep_merge', '~> 1'
34
34
  spec.add_runtime_dependency 'concurrent-ruby', '~> 1'
35
35
  spec.add_runtime_dependency 'thor', '~> 0'
36
- spec.add_runtime_dependency 'colorize', '~> 0'
37
36
 
38
37
  spec.add_development_dependency "bundler", "~> 1"
39
38
  spec.add_development_dependency "rake", "~> 10"
@@ -0,0 +1,35 @@
1
+ ##
2
+ # spec/lib/skn_utils/as_human_size_spec.rb
3
+ #
4
+
5
+
6
+ describe SknUtils, 'Number to Human Readable value' do
7
+
8
+ context "#as_human_size" do
9
+ it '#as_human_size(123) 123 Bytes' do
10
+ expect(described_class.as_human_size(123)).to eq "123 Bytes"
11
+ end
12
+ it '#as_human_size(358400) 0.3 MB' do
13
+ expect(described_class.as_human_size(358400)).to eq "0.3 MB"
14
+ end
15
+ it '#as_human_size(1234) 1.2 KB' do
16
+ expect(described_class.as_human_size(1234)).to eq "1.2 KB"
17
+ end
18
+ it '#as_human_size(12345) 12 KB' do
19
+ expect(described_class.as_human_size(12345)).to eq "12 KB"
20
+ end
21
+ it '#as_human_size(1234567890) 1.1 GB' do
22
+ expect(described_class.as_human_size(1234567890)).to eq "1.1 GB"
23
+ end
24
+ it '#as_human_size(1234567890123) 1.1 TB' do
25
+ expect(described_class.as_human_size(1234567890123)).to eq "1.1 TB"
26
+ end
27
+ it '#as_human_size(1234567) 1.2 MB' do
28
+ expect(described_class.as_human_size(1234567)).to eq "1.2 MB"
29
+ end
30
+ it '#as_human_size(483989) 0.5 MB' do
31
+ expect(described_class.as_human_size(483989 )).to eq "0.5 MB"
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,27 @@
1
+ ##
2
+ # spec/lib/skn_utils/catch_exceptions_spec.rb
3
+ #
4
+
5
+
6
+ describe SknUtils, 'Catch exceptions and retry block. ' do
7
+
8
+ context "#catch_exceptions" do
9
+ it '#catch_exceptions -- good block ' do
10
+ expect(
11
+ described_class.catch_exceptions do
12
+ "good"
13
+ end.value
14
+ ).to eq "good"
15
+ end
16
+
17
+ it '#catch_exceptions -- exception block ' do
18
+ expect(
19
+ described_class.catch_exceptions do
20
+ raise NotImplementedError, "Simulate Failures"
21
+ end.value
22
+ ).to include "Simulate Failures"
23
+ end
24
+
25
+ end
26
+
27
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skn_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.2.0
4
+ version: 5.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Scott Jr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-22 00:00:00.000000000 Z
11
+ date: 2018-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deep_merge
@@ -52,20 +52,6 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: colorize
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: bundler
71
57
  requirement: !ruby/object:Gem::Requirement
@@ -178,6 +164,7 @@ files:
178
164
  - bin/configs/settings/test.yml
179
165
  - bin/console
180
166
  - bin/install
167
+ - docs/.gitkeep
181
168
  - lib/skn_container.rb
182
169
  - lib/skn_failure.rb
183
170
  - lib/skn_hash.rb
@@ -206,6 +193,8 @@ files:
206
193
  - spec/factories/settings/test.local.yml
207
194
  - spec/factories/settings/test.yml
208
195
  - spec/lib/skn_settings_spec.rb
196
+ - spec/lib/skn_utils/as_human_size_spec.rb
197
+ - spec/lib/skn_utils/catch_exceptions_spec.rb
209
198
  - spec/lib/skn_utils/configurable_spec.rb
210
199
  - spec/lib/skn_utils/container_spec.rb
211
200
  - spec/lib/skn_utils/nested_result_spec.rb
@@ -252,6 +241,8 @@ test_files:
252
241
  - spec/factories/settings/test.local.yml
253
242
  - spec/factories/settings/test.yml
254
243
  - spec/lib/skn_settings_spec.rb
244
+ - spec/lib/skn_utils/as_human_size_spec.rb
245
+ - spec/lib/skn_utils/catch_exceptions_spec.rb
255
246
  - spec/lib/skn_utils/configurable_spec.rb
256
247
  - spec/lib/skn_utils/container_spec.rb
257
248
  - spec/lib/skn_utils/nested_result_spec.rb