k_util 0.0.3 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +95 -8
- data/STORIES.md +14 -14
- data/USAGE.md +97 -6
- data/k_util.gemspec +1 -1
- data/lib/k_util.rb +5 -3
- data/lib/k_util/console_helper.rb +24 -0
- data/lib/k_util/data_helper.rb +58 -62
- data/lib/k_util/file_helper.rb +22 -24
- data/lib/k_util/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88c89c4c9e613a825a162a59b8b4ea546d747f223d4d93ce93f26b3c734d49c2
|
4
|
+
data.tar.gz: 94b16c31d2149012c947e85a677f36b33f559cd914636269f8b5ce9bb9bc56bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca99065afbe8883d234c30a4cef1b56c69d2be2fd48860918158f0f1df8bc40cd77cc23b0a72a459ab765a1bb1c7b944f1ef6bc67b1c85a840a6df8c8a13cf24
|
7
|
+
data.tar.gz: fef92543798f378b9aedb86f1db84619b2a82fb0246f13ac1e99932def8e37a4bd391aa24a5411c3726aaea807b37cb22f33115fc47cc40c0ae404b67d8e4b2b
|
data/README.md
CHANGED
@@ -26,7 +26,7 @@ gem install k_util
|
|
26
26
|
|
27
27
|
### Main Story
|
28
28
|
|
29
|
-
As a Developer, I need simple utility helpers, to solve cross cutting issues
|
29
|
+
As a Developer, I need simple utility helpers, to solve cross cutting issues and simplify common access methods
|
30
30
|
|
31
31
|
See all [stories](./STORIES.md)
|
32
32
|
|
@@ -36,12 +36,101 @@ See all [usage examples](./USAGE.md)
|
|
36
36
|
|
37
37
|
### Basic Example
|
38
38
|
|
39
|
-
####
|
39
|
+
#### Console Helpers
|
40
40
|
|
41
|
-
|
41
|
+
Generate encoded strings that have meaning in the console
|
42
42
|
|
43
43
|
```ruby
|
44
|
-
|
44
|
+
puts KUtil.console.hyperlink('Google', 'https://google.com')
|
45
|
+
|
46
|
+
# "Google"
|
47
|
+
# (clickable hyperlink to the google website)
|
48
|
+
|
49
|
+
puts KUtil.console.file_hyperlink('My File', '/somepath/my-file.txt')
|
50
|
+
|
51
|
+
# "My File"
|
52
|
+
# (clickable link to the a file in the file system)
|
53
|
+
```
|
54
|
+
|
55
|
+
#### Data Helpers
|
56
|
+
|
57
|
+
Data object helpers such as any object to open struct and any object to hash
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
ThunderBirds = Struct.new(:action)
|
61
|
+
|
62
|
+
virgil =
|
63
|
+
OpenStruct.new(
|
64
|
+
name: 'Virgil Tracy', age: 73, thunder_bird: ThunderBirds.new(:are_grounded)
|
65
|
+
)
|
66
|
+
penny =
|
67
|
+
OpenStruct.new(
|
68
|
+
name: 'Lady Penelope', age: 69, thunder_bird: ThunderBirds.new(:are_go)
|
69
|
+
)
|
70
|
+
|
71
|
+
data = {
|
72
|
+
key1: 'David',
|
73
|
+
key2: 333,
|
74
|
+
key3: ThunderBirds.new(:are_go),
|
75
|
+
people: [virgil, penny]
|
76
|
+
}
|
77
|
+
|
78
|
+
data_open = KUtil.data.to_open_struct(data)
|
79
|
+
data_hash = KUtil.data.to_hash(data_open)
|
80
|
+
|
81
|
+
puts JSON.pretty_generate(data_hash)
|
82
|
+
|
83
|
+
# {
|
84
|
+
# "key1": "David",
|
85
|
+
# "key2": 333,
|
86
|
+
# "key3": {
|
87
|
+
# "action": "are_go"
|
88
|
+
# },
|
89
|
+
# "people": [
|
90
|
+
# {
|
91
|
+
# "name": "Virgil Tracy",
|
92
|
+
# "age": 73,
|
93
|
+
# "thunder_bird": {
|
94
|
+
# "action": "are_grounded"
|
95
|
+
# }
|
96
|
+
# },
|
97
|
+
# {
|
98
|
+
# "name": "Lady Penelope",
|
99
|
+
# "age": 69,
|
100
|
+
# "thunder_bird": {
|
101
|
+
# "action": "are_go"
|
102
|
+
# }
|
103
|
+
# }
|
104
|
+
# ]
|
105
|
+
# }
|
106
|
+
```
|
107
|
+
|
108
|
+
#### File Helpers
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
puts KUtil.file.expand_path('file.rb')
|
112
|
+
|
113
|
+
# /current/folder/file.rb
|
114
|
+
|
115
|
+
puts KUtil.file.expand_path('/file.rb')
|
116
|
+
|
117
|
+
# /file.rb
|
118
|
+
|
119
|
+
puts KUtil.file.expand_path('~/file.rb')
|
120
|
+
|
121
|
+
# /Users/current-user/file.rb
|
122
|
+
|
123
|
+
puts KUtil.file.expand_path('file.rb', '/klue-less/xyz')
|
124
|
+
|
125
|
+
# /klue-less/xyz/file.rb
|
126
|
+
|
127
|
+
puts KUtil.file.pathname_absolute?('somepath/somefile.rb')
|
128
|
+
|
129
|
+
# false
|
130
|
+
|
131
|
+
puts KUtil.file.pathname_absolute?('/somepath/somefile.rb')
|
132
|
+
|
133
|
+
# true
|
45
134
|
```
|
46
135
|
|
47
136
|
## Development
|
@@ -68,10 +157,8 @@ Aaa::Bbb::Program.execute()
|
|
68
157
|
To release a new version, update the version number in `version.rb`, build the gem and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
69
158
|
|
70
159
|
```bash
|
71
|
-
|
72
|
-
|
73
|
-
# or push the latest gem
|
74
|
-
ls *.gem | sort -r | head -1 | xargs gem push
|
160
|
+
rake publish
|
161
|
+
rake clean
|
75
162
|
```
|
76
163
|
|
77
164
|
## Contributing
|
data/STORIES.md
CHANGED
@@ -2,32 +2,27 @@
|
|
2
2
|
|
3
3
|
> KUtil provides simple utility methods
|
4
4
|
|
5
|
-
As a Developer, I need simple utility helpers, to solve cross cutting issues
|
5
|
+
As a Developer, I need simple utility helpers, to solve cross cutting issues and simplify common access methods
|
6
6
|
|
7
7
|
## Development radar
|
8
8
|
|
9
|
-
|
9
|
+
## Stories and tasks
|
10
10
|
|
11
|
-
|
11
|
+
### Stories - completed
|
12
12
|
|
13
|
-
|
13
|
+
As a Developer, I need simple utility helpers, to solve cross cutting issues
|
14
14
|
|
15
|
-
|
15
|
+
- Console helpers
|
16
|
+
- Data helpers
|
17
|
+
- File helpers
|
18
|
+
|
19
|
+
### Tasks - completed
|
16
20
|
|
17
21
|
Setup RubyGems and RubyDoc
|
18
22
|
|
19
23
|
- Build and deploy gem to [rubygems.org](https://rubygems.org/gems/k_util)
|
20
24
|
- Attach documentation to [rubydoc.info](https://rubydoc.info/github/to-do-/k_util/master)
|
21
25
|
|
22
|
-
Setup GitHub Action (test and lint)
|
23
|
-
|
24
|
-
- Setup Rspec action
|
25
|
-
- Setup RuboCop action
|
26
|
-
|
27
|
-
## Stories and tasks
|
28
|
-
|
29
|
-
### Tasks - completed
|
30
|
-
|
31
26
|
Setup project management, requirement and SCRUM documents
|
32
27
|
|
33
28
|
- Setup readme file
|
@@ -35,6 +30,11 @@ Setup project management, requirement and SCRUM documents
|
|
35
30
|
- Setup a project backlog
|
36
31
|
- Setup an examples/usage document
|
37
32
|
|
33
|
+
Setup GitHub Action (test and lint)
|
34
|
+
|
35
|
+
- Setup Rspec action
|
36
|
+
- Setup RuboCop action
|
37
|
+
|
38
38
|
Setup new Ruby GEM
|
39
39
|
|
40
40
|
- Build out a standard GEM structure
|
data/USAGE.md
CHANGED
@@ -2,18 +2,109 @@
|
|
2
2
|
|
3
3
|
> KUtil provides simple utility methods
|
4
4
|
|
5
|
-
As a Developer, I need simple utility helpers, to solve cross cutting issues
|
5
|
+
As a Developer, I need simple utility helpers, to solve cross cutting issues and simplify common access methods
|
6
6
|
|
7
7
|
## Usage
|
8
8
|
|
9
9
|
### Sample Classes
|
10
10
|
|
11
|
-
####
|
11
|
+
#### Console Helpers
|
12
12
|
|
13
|
-
|
13
|
+
Generate encoded strings that have meaning in the console
|
14
14
|
|
15
15
|
```ruby
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
puts KUtil.console.hyperlink('Google', 'https://google.com')
|
17
|
+
|
18
|
+
# "Google"
|
19
|
+
# (clickable hyperlink to the google website)
|
20
|
+
|
21
|
+
puts KUtil.console.file_hyperlink('My File', '/somepath/my-file.txt')
|
22
|
+
|
23
|
+
# "My File"
|
24
|
+
# (clickable link to the a file in the file system)
|
25
|
+
```
|
26
|
+
|
27
|
+
#### File Helpers
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
# Expand Path
|
31
|
+
|
32
|
+
puts KUtil.file.expand_path('file.rb')
|
33
|
+
|
34
|
+
# /current/folder/file.rb
|
35
|
+
|
36
|
+
puts KUtil.file.expand_path('/file.rb')
|
37
|
+
|
38
|
+
# /file.rb
|
39
|
+
|
40
|
+
puts KUtil.file.expand_path('~/file.rb')
|
41
|
+
|
42
|
+
# /Users/current-user/file.rb
|
43
|
+
|
44
|
+
puts KUtil.file.expand_path('file.rb', '/klue-less/xyz')
|
45
|
+
|
46
|
+
# /klue-less/xyz/file.rb
|
47
|
+
|
48
|
+
# Absolute path/file name
|
49
|
+
|
50
|
+
puts KUtil.file.pathname_absolute?('somepath/somefile.rb')
|
51
|
+
|
52
|
+
# false
|
53
|
+
|
54
|
+
puts KUtil.file.pathname_absolute?('/somepath/somefile.rb')
|
55
|
+
|
56
|
+
# true
|
57
|
+
```
|
58
|
+
|
59
|
+
#### Data Helpers
|
60
|
+
|
61
|
+
Data object helpers such as any object to open struct and any object to hash
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
ThunderBirds = Struct.new(:action)
|
65
|
+
|
66
|
+
virgil =
|
67
|
+
OpenStruct.new(
|
68
|
+
name: 'Virgil Tracy', age: 73, thunder_bird: ThunderBirds.new(:are_grounded)
|
69
|
+
)
|
70
|
+
penny =
|
71
|
+
OpenStruct.new(
|
72
|
+
name: 'Lady Penelope', age: 69, thunder_bird: ThunderBirds.new(:are_go)
|
73
|
+
)
|
74
|
+
|
75
|
+
data = {
|
76
|
+
key1: 'David',
|
77
|
+
key2: 333,
|
78
|
+
key3: ThunderBirds.new(:are_go),
|
79
|
+
people: [virgil, penny]
|
80
|
+
}
|
81
|
+
|
82
|
+
data_open = KUtil.data.to_open_struct(data)
|
83
|
+
data_hash = KUtil.data.to_hash(data_open)
|
84
|
+
|
85
|
+
puts JSON.pretty_generate(data_hash)
|
86
|
+
|
87
|
+
# {
|
88
|
+
# "key1": "David",
|
89
|
+
# "key2": 333,
|
90
|
+
# "key3": {
|
91
|
+
# "action": "are_go"
|
92
|
+
# },
|
93
|
+
# "people": [
|
94
|
+
# {
|
95
|
+
# "name": "Virgil Tracy",
|
96
|
+
# "age": 73,
|
97
|
+
# "thunder_bird": {
|
98
|
+
# "action": "are_grounded"
|
99
|
+
# }
|
100
|
+
# },
|
101
|
+
# {
|
102
|
+
# "name": "Lady Penelope",
|
103
|
+
# "age": 69,
|
104
|
+
# "thunder_bird": {
|
105
|
+
# "action": "are_go"
|
106
|
+
# }
|
107
|
+
# }
|
108
|
+
# ]
|
109
|
+
# }
|
19
110
|
```
|
data/k_util.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
|
12
12
|
spec.summary = 'KUtil provides simple utility methods'
|
13
13
|
spec.description = <<-TEXT
|
14
|
-
KUtil provides simple utility methods
|
14
|
+
KUtil provides simple utility methods, such as file helpers, data object helpers and more.
|
15
15
|
TEXT
|
16
16
|
spec.homepage = 'http://appydave.com/gems/k-util'
|
17
17
|
spec.license = 'MIT'
|
data/lib/k_util.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'k_util/version'
|
4
|
+
require 'k_util/console_helper'
|
5
|
+
require 'k_util/data_helper'
|
6
|
+
require 'k_util/file_helper'
|
4
7
|
|
8
|
+
# Provide various helper functions
|
5
9
|
module KUtil
|
6
10
|
# raise KUtil::Error, 'Sample message'
|
7
|
-
class Error < StandardError; end
|
8
|
-
|
9
|
-
# Your code goes here...
|
11
|
+
# class Error < StandardError; end
|
10
12
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Provide console helper functions
|
4
|
+
module KUtil
|
5
|
+
class << self
|
6
|
+
attr_accessor :console
|
7
|
+
end
|
8
|
+
|
9
|
+
# Helper methods attached to the namespace for working with Console
|
10
|
+
class ConsoleHelper
|
11
|
+
# Convert a hash into a deep OpenStruct or array an array
|
12
|
+
# of objects into an array of OpenStruct
|
13
|
+
# Generate hyper link encoded string for the console
|
14
|
+
def self.file_hyperlink(text, file)
|
15
|
+
"\u001b]8;;file://#{file}\u0007#{text}\u001b]8;;\u0007"
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.hyperlink(text, link)
|
19
|
+
"\u001b]8;;#{link}\u0007#{text}\u001b]8;;\u0007"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
KUtil.console = KUtil::ConsoleHelper
|
data/lib/k_util/data_helper.rb
CHANGED
@@ -1,64 +1,60 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
#
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
#
|
12
|
-
#
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
#
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
#
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
#
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
# end
|
62
|
-
# end
|
63
|
-
|
64
|
-
# KDsl::Util.data = KDsl::Util::DataHelper
|
3
|
+
# Provide data object helper functions
|
4
|
+
module KUtil
|
5
|
+
class << self
|
6
|
+
attr_accessor :data
|
7
|
+
end
|
8
|
+
|
9
|
+
# Helper methods attached to the namespace for working with Data
|
10
|
+
class DataHelper
|
11
|
+
# Convert various data types (Hash, Array, Struct) into a deep nested OpenStruct
|
12
|
+
# or an array of deep nested OpenStruct
|
13
|
+
def self.to_open_struct(data)
|
14
|
+
case data
|
15
|
+
when Hash
|
16
|
+
OpenStruct.new(data.transform_values { |v| to_open_struct(v) })
|
17
|
+
|
18
|
+
when Array
|
19
|
+
data.map { |o| to_open_struct(o) }
|
20
|
+
|
21
|
+
when Struct, OpenStruct
|
22
|
+
to_open_struct(data.to_h)
|
23
|
+
|
24
|
+
else
|
25
|
+
# Some primitave type: String, True/False or an ObjectStruct
|
26
|
+
data
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
31
|
+
def self.to_hash(data)
|
32
|
+
# No test yet
|
33
|
+
if data.is_a?(Array)
|
34
|
+
return data.map { |v| v.is_a?(OpenStruct) ? to_hash(v) : v }
|
35
|
+
end
|
36
|
+
|
37
|
+
data.each_pair.with_object({}) do |(key, value), hash|
|
38
|
+
case value
|
39
|
+
when OpenStruct
|
40
|
+
hash[key] = to_hash(value)
|
41
|
+
when Array
|
42
|
+
# No test yet
|
43
|
+
values = value.map { |v| v.is_a?(OpenStruct) ? to_hash(v) : v }
|
44
|
+
hash[key] = values
|
45
|
+
else
|
46
|
+
hash[key] = value
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
51
|
+
|
52
|
+
def self.clean_symbol(value)
|
53
|
+
return value if value.nil?
|
54
|
+
|
55
|
+
value.is_a?(Symbol) ? value.to_s : value
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
KUtil.data = KUtil::DataHelper
|
data/lib/k_util/file_helper.rb
CHANGED
@@ -1,29 +1,27 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
#
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
# end
|
3
|
+
# Provide file helper functions
|
4
|
+
module KUtil
|
5
|
+
class << self
|
6
|
+
attr_accessor :file
|
7
|
+
end
|
9
8
|
|
10
|
-
#
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
9
|
+
# Helper methods attached to the namespace for working with Files
|
10
|
+
class FileHelper
|
11
|
+
def self.expand_path(filename, base_path = nil)
|
12
|
+
if pathname_absolute?(filename)
|
13
|
+
filename
|
14
|
+
elsif filename.start_with?('~/')
|
15
|
+
File.expand_path(filename)
|
16
|
+
else
|
17
|
+
File.expand_path(filename, base_path)
|
18
|
+
end
|
19
|
+
end
|
21
20
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
# end
|
21
|
+
def self.pathname_absolute?(pathname)
|
22
|
+
Pathname.new(pathname).absolute?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
28
26
|
|
29
|
-
|
27
|
+
KUtil.file = KUtil::FileHelper
|
data/lib/k_util/version.rb
CHANGED
metadata
CHANGED
@@ -1,16 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: k_util
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Cruwys
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-03-
|
11
|
+
date: 2021-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: " KUtil provides simple utility methods
|
13
|
+
description: " KUtil provides simple utility methods, such as file helpers, data
|
14
|
+
object helpers and more.\n"
|
14
15
|
email:
|
15
16
|
- david@ideasmen.com.au
|
16
17
|
executables: []
|
@@ -38,6 +39,7 @@ files:
|
|
38
39
|
- hooks/update-version
|
39
40
|
- k_util.gemspec
|
40
41
|
- lib/k_util.rb
|
42
|
+
- lib/k_util/console_helper.rb
|
41
43
|
- lib/k_util/data_helper.rb
|
42
44
|
- lib/k_util/file_helper.rb
|
43
45
|
- lib/k_util/version.rb
|