toolrack 0.6.2 → 0.7.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
  SHA256:
3
- metadata.gz: 91dc7e7717dc84f13f1cf0e7f3da03a68b6ed891d3900bd1b23abd5e8148734c
4
- data.tar.gz: ff6f780710d0f7d7e21583e4a0c24e9e4508d02f089ca19d5f92f99e61773b7d
3
+ metadata.gz: 3b5c17ec1e90be20f1ec19300d51804183a44223b5b761bdb00475ed7d930063
4
+ data.tar.gz: b7776ecbd1cbad8a7541f32085d3db57a24867ef6ed2d0f9b98f5ad724e3d1a4
5
5
  SHA512:
6
- metadata.gz: c5ff196e18d97bdd54096490b5ab9450dd074ae31a6e59c15030772e16953e2e7f5b345bdd61a9caf51e7617b1727773edb244fa97cff1341c355644360b2769
7
- data.tar.gz: 3358f972376beff09830a084019b9b4cb5fe8ef2adb3e7f29c376d5097e858c9ab137496b41a6ea1a4316ffdcad4eebb4c10edfb90358a713e98616a3d63bfc8
6
+ metadata.gz: 835aa468defaf9c46aae59586c1ae1315c9112b5e116846be4f5f63661faff5d2cf1977082c313768434b6af2a8b82429db8b438a44aaf100e386dbfa2628e1b
7
+ data.tar.gz: 70ab89133d5088f7a04fd167a2dd9e402368323fc29bbbbeb55266bcf1d5abb84fc847bbd8ab470a0ec56afe6c83281fab174d4a2d0c98151d61c8ce0822fb8f
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/README.md CHANGED
@@ -20,48 +20,63 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- Toolrack is meant to be utilities hence it is prefixed with an entity.
24
- Therefore the two utilities are both packaged in namespace:
23
+ The utilities are those commonly being used that I think should be group together to save some effort in my other projects.
25
24
 
26
25
  ```ruby
27
- module Antrapol::MyToolRack::ConditionUtils
26
+ module ToolRack::ConditionUtils
28
27
  end
29
28
  ```
30
29
 
31
30
  ```ruby
32
- module Antrapol::MyToolRack::ExceptionUtils
31
+ module ToolRack::ExceptionUtils
33
32
  end
34
33
  ```
35
34
 
36
- Besides, all the utilities so far is a module by itself, which means it is meant to be included/extended into application classes.
35
+ All the utilities so far is a module by itself, which means it is meant to be included/extended into application classes.
37
36
 
38
37
  For example:
39
38
  ```ruby
40
39
  class EditController
41
- include Antrapol::MyToolRack::ConditionUtils
42
- include Antrapol::MyToolRack::ExceptionUtils
40
+ # enable instance method to access those uitilities
41
+ include ToolRack::ConditionUtils
42
+ include ToolRack::ExceptionUtils
43
43
  end
44
44
  ```
45
45
 
46
46
  Currently it has 3 modules:
47
47
  * Condition Utilities
48
- * is_empty?(obj) - I find rather effort intensive to call if not (obj.nil? or obj.empty?) for items that I want to check for validity before process. Hence this method shall test for .nil?. If the object also respond to :empty? it shall be called again. For example integer type does not support .empty?
49
- * not_empty?(obj) - Reverse of the is\_empty?(obj) above
48
+ * is\_empty?(obj) - I found that I've type the condition if not (x.nil? and x.empty?) too frequent that I think this is very best to turn this into a function. The empty test shall take the following test in sequence:
49
+ * First test is x.nil?. If it is return true
50
+ * Then test if x respond\_to? :empty?. If it is call it.
51
+ * Then test if x respond\_to? :length. If it is call it to see if the value is it 0 and return its comparison result
52
+ * Then test if x respond\_to? :size. If it is call it to see if the value is it 0 and return its comparison result
53
+ Once any of the test is successful, it will not proceed to test the subseqent tests and return its' result.
54
+ * not\_empty?(obj) - Reverse of the is\_empty?(obj) above
55
+ * is\_boolean?(obj) - Test given value is it a boolean value. Note this does not test if the obj is a string of 'true' or 'false'. Those shall return false by this API. If you want to test for string, use is\_string\_boolean?(str) or is\_str\_bool?(str)
56
+ * is\_bool?(obj) - Shorcut to is\_boolean?()
57
+ * is\_string\_boolean?(str) - Test given string is it "true" or "false". Note the comparison is case insensitive. Note that both "true" and "false" shall return with value true from this API. This API does not convert the string value it to boolean. Just a test if the string carry the boolean value. Refers data conversion section if you want to convert string to boolean
58
+
59
+
60
+ * Data Conversion Utilities
61
+ This is meant to convert data from a format to another format
62
+ * to\_b64(bin) - convert given binary to typical RFC 2045 Base64 output which includes line break in specific position
63
+ * to\_b64\_strict(bin) - Convert binary to RFC 4648 Base64 format, means there is no line break in between.
64
+ * from\_b64(str) - Convert given Base64 input into binary back. Tested this with both RFC2045 and RFC4648 Base64 output seems no issue.
65
+ * from\_b64\_strict(str) - Although seems the from\_b64 able to cater the RFC4648 output, prepare this in case...
66
+ * to\_hex(bin) - Convert binary into hex output. Note this function also convert integer into hex.
67
+ * from\_hex(str) - Convert hex string into binary
68
+ * hex\_to\_int(str) - Convert given hex string into integer. Alias method: hex\_to\_num, hex\_to\_integer, hex\_to\_number
69
+ * string\_to\_boolean(str) - Convert the string into appropriate boolean true/false. Note if the given string is not "true" or "false", nil shall be returned and can test it with is\_empty?
70
+
50
71
 
51
72
  * Exception Utilities
52
- * raise_if_empty(obj, message, error) - Extension from the is_empty?() above, usually if it is empty, an exception shall be raised. It is just combined the conditions with raise of exception.
53
- * obj is the object to test for is_empty
73
+ * raise\_if\_empty(obj, message, error) - Extension from the is\_empty?() above, usually if it is empty, an exception shall be raised. It is just combined the conditions with raise of exception.
74
+ * obj is the object to test for is\_empty?()
54
75
  * message is the one that shall be thrown with the exception.
55
76
  * error is the exception type
56
- * raise_if_false(obj, message, error) - As the name implied
57
- * raise_if_true(obj, message, error) - As the name implied
58
-
59
- * Process Utilities
60
- * exec(command, options = { ), &block) - Passing in the command into the method and result is returned to caller. If options is not given shall go to basic execution which is the backtick (\`). If block empty, result of the command is returned to caller. If block is given, result (true or false of the execution of command and any result from STDOUT is call to block provider(e.g. block.call($?, result)
61
-
62
- # Note on Process Utilities
77
+ * raise\_if\_false(obj, message, error) - As the name implied
78
+ * raise\_if\_true(obj, message, error) - As the name implied
63
79
 
64
- The command is passed as it is to the operating system which includes fatal operation such as "rm -rf". Library at current stage made no filtering on system harmful command hence the caller of the library has to take extra precaution on what command is being passed into the library
65
80
 
66
81
 
67
82
 
data/lib/toolrack.rb CHANGED
@@ -10,6 +10,7 @@ require_relative 'toolrack/exception_utils'
10
10
  require_relative 'toolrack/condition_utils'
11
11
  require_relative 'toolrack/process_utils'
12
12
  require_relative 'toolrack/runtime_utils'
13
+ require_relative 'toolrack/data_conversion_utils'
13
14
 
14
15
  module Antrapol
15
16
  module ToolRack
@@ -20,5 +21,6 @@ module Antrapol
20
21
  end
21
22
 
22
23
  ToolRack = Antrapol::ToolRack
24
+ ToolRack::DataConv = Antrapol::ToolRack::DataConversionUtils
23
25
 
24
26
 
@@ -15,6 +15,10 @@ module Antrapol
15
15
  else
16
16
  obj.empty?
17
17
  end
18
+ elsif obj.respond_to?(:length)
19
+ obj.length == 0
20
+ elsif obj.respond_to?(:size)
21
+ obj.size == 0
18
22
  else
19
23
  false
20
24
  end
@@ -29,6 +33,21 @@ module Antrapol
29
33
  end
30
34
  alias_method :is_bool?, :is_boolean?
31
35
 
36
+ def is_string_boolean?(str)
37
+ if not_empty?(str)
38
+ s = str.to_s.strip.downcase
39
+ case s
40
+ when "true", "false"
41
+ true
42
+ else
43
+ false
44
+ end
45
+ else
46
+ false
47
+ end
48
+ end
49
+ alias_method :is_str_bool?, :is_string_boolean?
50
+
32
51
  end # ConditionUtils
33
52
  end # MyToolRack
34
53
  end # Antrapol
@@ -0,0 +1,96 @@
1
+
2
+ require 'base64'
3
+
4
+ module Antrapol
5
+ module ToolRack
6
+ module DataConversionUtils
7
+ include ConditionUtils
8
+
9
+ def from_b64(str)
10
+ if not_empty?(str)
11
+ # decode operation supports both strict or non strict output
12
+ Base64.decode64(str)
13
+ else
14
+ str
15
+ end
16
+ end
17
+
18
+ def to_b64(bin)
19
+ if not_empty?(bin)
20
+ # Base64 standard output which has new line in the middle of the output
21
+ Base64.encode64(bin)
22
+ else
23
+ bin
24
+ end
25
+ end
26
+
27
+ def to_b64_strict(bin)
28
+ if not_empty?(bin)
29
+ # strict base64 shall have NO new line in the middle of the output
30
+ Base64.strict_encode64(bin)
31
+ else
32
+ bin
33
+ end
34
+ end
35
+
36
+ def from_b64_strict(str)
37
+ if not_empty?(str)
38
+ Base64.strict_decode64(str)
39
+ else
40
+ str
41
+ end
42
+ end
43
+
44
+ def from_hex(str)
45
+ if not_empty?(str)
46
+ str.scan(/../).map { |x| x.hex.chr }.join
47
+ else
48
+ str
49
+ end
50
+ end
51
+ alias_method :from_hex_string, :from_hex
52
+
53
+ def hex_to_number(str)
54
+ if not_empty?(str)
55
+ str.to_i(16)
56
+ else
57
+ str
58
+ end
59
+ end
60
+ alias_method :hex_to_num, :hex_to_number
61
+ alias_method :hex_to_int, :hex_to_number
62
+
63
+ def to_hex(bin)
64
+ if not_empty?(bin)
65
+ case bin
66
+ when String
67
+ bin.each_byte.map { |b| b.to_s(16).rjust(2,'0') }.join
68
+ when Integer
69
+ bin.to_s(16)
70
+ else
71
+ bin
72
+ end
73
+ else
74
+ bin
75
+ end
76
+ end
77
+
78
+ def string_to_bool(str)
79
+ if not_empty?(str) and is_str_bool?(str)
80
+ s = str.to_s.strip.downcase
81
+ case s
82
+ when "true"
83
+ true
84
+ when "false"
85
+ false
86
+ else
87
+ nil
88
+ end
89
+ else
90
+ nil
91
+ end
92
+ end
93
+
94
+ end
95
+ end
96
+ end
@@ -1,6 +1,6 @@
1
1
  module Antrapol
2
2
  module ToolRack
3
- VERSION = "0.6.2"
3
+ VERSION = "0.7.0"
4
4
  end
5
5
  end
6
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toolrack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris
@@ -46,6 +46,7 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
+ - ".rspec"
49
50
  - ".travis.yml"
50
51
  - CODE_OF_CONDUCT.md
51
52
  - Gemfile
@@ -55,6 +56,7 @@ files:
55
56
  - bin/setup
56
57
  - lib/toolrack.rb
57
58
  - lib/toolrack/condition_utils.rb
59
+ - lib/toolrack/data_conversion_utils.rb
58
60
  - lib/toolrack/exception_utils.rb
59
61
  - lib/toolrack/global.rb
60
62
  - lib/toolrack/process_utils.rb