skn_utils 5.3.0 → 5.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b812d7904cab21c79e37afec4bf5c9eb2f67336
4
- data.tar.gz: 966367ddcb978a3bb2eb6eeea71a89c93b3ee347
3
+ metadata.gz: faaa010ea403a5bfbf20386ee86a9c68e8982da1
4
+ data.tar.gz: 909be6bef4b46a41bfd293d35a3059396d4adba5
5
5
  SHA512:
6
- metadata.gz: 595bf74de49521fe61e6ec10dee7f1c56b97798096ff540e03e1bdc92e92c82e7b805018150bfb0495bca45b74c1c76ac6c9bc38aee19b774a8a081f2b6aa327
7
- data.tar.gz: 1b57076cadc213712426727bcaac68a546f99f0cab187b15618b8799d106b8019d056742aa87ef092af9f271e7cf6a704bc43990ab6dd7d84fcdeb7ea7bd06d2
6
+ metadata.gz: 46f92868aaecee7ece92b3d1c4b5bc33b5643e8c2de3a5f442e5141edb2b45d5a4cb8d4bd29e24241a34001b253abec38fe381e8ca8d798dc8eb6d662fdc12c2
7
+ data.tar.gz: 81c41c8dda9b50797a76005a29cb329b2d8bc9da0af585d1848124a54606ac1f3a9d8bb8bb1d6ec5b2806df0002d673849425ca68290121e4ac327c7b94f784d
data/README.md CHANGED
@@ -2,9 +2,7 @@
2
2
  [![Gem Version](https://badge.fury.io/rb/skn_utils.svg)](http://badge.fury.io/rb/skn_utils)
3
3
 
4
4
  # SknUtils
5
- #### SknUtils::NestedResult class; dynamic key/value container
6
- The intent of this gem is to be a container of nestable data results or key/value pairs, with easy access to its contents and on-demand transformation back to the creating hash (#to_hash).
7
-
5
+ ## SknUtils::NestedResult class; dynamic key/value container
8
6
  A Ruby Gem containing a Ruby PORO (Plain Old Ruby Object) that can be instantiated at runtime with an input hash. This library creates
9
7
  an Object with Dot and Hash notational accessors to each key's value. Additional key/value pairs can be added post-create
10
8
  by simply assigning it; `obj.my_new_var = "some value"`
@@ -29,20 +27,20 @@ Ruby's Hash object is already extremely flexible, even more so with the addition
29
27
  1. In-Memory Key-Store, use it to cache active user objects, or active Integration passwords, and/or objects that are not serializable.
30
28
  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
29
  ```ruby
32
- SknSettings.registry = {
30
+ SknSettings.command_handler = {
33
31
  Commands::RetrieveAvailableResources => method(:resources_metadata_service),
34
32
  Commands::RetrieveResourceContent => method(:resource_content_service)
35
33
  }
36
34
  ...
37
- SknSettings.registry[ cmd.class ].call( cmd )
35
+ SknSettings.command_handler[ cmd.class ].call( cmd )
38
36
  -- or --
39
- SknSettings.registry.key?( cmd.class ) && cmd.valid? ?
40
- SknSettings.registry[ cmd.class ].call( cmd ) :
41
- command_not_found_action()
37
+ SknSettings.command_handler.key?( cmd.class ) && cmd.valid? ?
38
+ SknSettings.command_handler[ cmd.class ].call( cmd ) : command_not_found_action()
42
39
  ```
43
40
  There are many more use cases for Ruby's Hash that this gem just makes easier to implement.
44
41
 
45
- #### Available Classes
42
+
43
+ ## Available Classes
46
44
  * SknSuccess
47
45
  * SknFailure
48
46
  * SknSettings
@@ -58,10 +56,21 @@ There are many more use cases for Ruby's Hash that this gem just makes easier to
58
56
  * SknUtils::CoreObjectExtensions
59
57
  * SknUtils::Configurable
60
58
 
59
+ ## Available Class.Methods
60
+ * SknUtils.catch_exceptions()
61
+ * SknUtils.as_human_size()
62
+ * SknUtils.duration(start_time=nil)
63
+
64
+
61
65
 
62
66
  ## History
67
+ 12/16/2018 V5.4.0
68
+ Added :duration() utils to SknUtils module:
69
+ #duration() #=> returns start_time value
70
+ #duration(start_time) #=> returns elapsed-time as string, "%3.3f seconds"
71
+
63
72
  11/09/2018 V5.3.0
64
- Added two utils to SknUtils class:
73
+ Added two utils to SknUtils module:
65
74
  #catch_exceptions(&blk) #=> catch all exceptions and retry block x times
66
75
  #as_human_size(12345) #=> 12 KB
67
76
  - See related RSpecs
@@ -165,18 +174,20 @@ There are many more use cases for Ruby's Hash that this gem just makes easier to
165
174
 
166
175
 
167
176
  ## Public Methods: SknUtils::Configurable module
168
- For making an arbitrary class configurable and then specifying those configuration values using a clean and simple DSL.
177
+ For making an arbitrary class configurable and then specifying those configuration values.
169
178
  ```ruby
179
+ # (1) First establish the method names of class values desired.
170
180
  ################
171
- Inside Target component
181
+ # Inside Target component
172
182
  ################
173
183
  class MyApp
174
184
  include SknUtils::Configurable.with(:app_id, :title, :cookie_name) # or {root_enable: false})
175
185
  # ... default=true for root|env|logger
176
186
  end
177
187
 
188
+ # (2) Set initial values for those class values in a Rails Initializer, or before use.
178
189
  ################
179
- Inside Initializer
190
+ # Inside Initializer
180
191
  ################
181
192
  MyApp.configure do
182
193
  app_id "my_app"
@@ -184,8 +195,9 @@ There are many more use cases for Ruby's Hash that this gem just makes easier to
184
195
  cookie_name { "#{app_id}_session" }
185
196
  end
186
197
 
198
+ # (2) Or set initial values for those class values when defining the class.
187
199
  ################
188
- During Definition
200
+ # During Definition
189
201
  ################
190
202
  class MyApp
191
203
  include SknUtils::Configurable.with(:app_id, :title, :cookie_name, {root_enable: true})
@@ -201,8 +213,9 @@ There are many more use cases for Ruby's Hash that this gem just makes easier to
201
213
  self.root = Dir.pwd
202
214
  end
203
215
 
216
+ # (3) Remember they are class values, use as needed.
204
217
  ################
205
- Usage:
218
+ # Usage:
206
219
  ################
207
220
  MyApp.config.app_id # ==> "my_app"
208
221
  MyApp.logger # ==> <Logger.class>
@@ -210,10 +223,10 @@ There are many more use cases for Ruby's Hash that this gem just makes easier to
210
223
  MyApp.env.test? # ==> true
211
224
 
212
225
  ###############
213
- Syntax
226
+ # Syntax
214
227
  ###############
215
- Main Class Attrs
216
- - root = application rood directory as Pathname
228
+ # Main Class Attrs -- defaults
229
+ - root = application root directory as Pathname
217
230
  - env = string value from RACK_ENV
218
231
  - registry = SknRegistry instance
219
232
  - logger = Assigned Logger instance
@@ -227,16 +240,17 @@ There are many more use cases for Ruby's Hash that this gem just makes easier to
227
240
 
228
241
 
229
242
  ## Public Methods: SknContainer ONLY
230
- SknContainer is global constant containing an initialized Object of Concurrent::Hash using defaults with additional methods.
243
+ SknContainer is global constant assigned to an instantiated instance of SknRegistry.
231
244
  Returns the keyed value as the original instance/value or if provided a proc the result of calling that proc.
232
245
  To register a class or object for global retrieval, use the following API. Also review the RSpecs for additional useage info.
246
+
233
247
  #register(key, contents = nil, options = {})
234
248
  - example:
235
249
  SknContainer.register(:some_klass, MyClass) -- class as value
236
250
  SknContainer.register(:the_instance, MyClass.new) -- Object Instance as value
237
251
  SknContainer.register(:unique_instance, -> {MyClass.new}) -- New Object Instance for each #resolve
238
252
 
239
- SknContainer -- #register return self to enable chaining
253
+ SknContainer -- #register returns self to enable chaining
240
254
  .register(:unique_instance, -> {MyClass.new})
241
255
  .register(:the_instance, MyClass.new)
242
256
  .register(:some_klass, MyClass)
@@ -2,7 +2,7 @@
2
2
  module SknUtils
3
3
  class Version
4
4
  MAJOR = 5
5
- MINOR = 3
5
+ MINOR = 4
6
6
  PATCH = 0
7
7
 
8
8
  def self.to_s
data/lib/skn_utils.rb CHANGED
@@ -44,7 +44,8 @@ module SknUtils
44
44
  attempts = retries
45
45
  begin
46
46
 
47
- SknSuccess.( yield )
47
+ res = yield
48
+ [SknFailure, SknFailure].any? {|o| res.kind_of?(o) } ? res : SknSuccess.( res )
48
49
 
49
50
  rescue StandardError, ScriptError => error
50
51
  puts "#{retry_count} - #{error.class.name}:#{error.message}"
@@ -53,7 +54,7 @@ module SknUtils
53
54
  sleep(pause_between)
54
55
  retry
55
56
  else
56
- SknFailure.( "RETRY ATTEMPTS FAILED - #{error.class.name}:#{error.message}" )
57
+ SknFailure.( "RETRY ATTEMPTS FAILED - #{error.class.name}:#{error.message}", error.backtrace[0..5].to_s )
57
58
  end
58
59
  end
59
60
  end # end method
@@ -77,5 +78,11 @@ module SknUtils
77
78
  ((num > 9 || num.modulo(1) < 0.1) ? '%d %s' : '%.1f %s') % [num, units[exp]]
78
79
  end
79
80
 
81
+ # call without to get start time
82
+ # call with start_time to get duration string
83
+ def self.duration(start_time=nil)
84
+ start_time.nil? ? Process.clock_gettime(Process::CLOCK_MONOTONIC) :
85
+ "%3.3f seconds" % (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time)
86
+ end
80
87
 
81
88
  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.3.0
4
+ version: 5.4.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-11-10 00:00:00.000000000 Z
11
+ date: 2018-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deep_merge