kutils 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d1a0dbd2603aa516690550052fbf03ca008593c92e73f93e5eef44996b4dce20
4
+ data.tar.gz: c78052b564ced2d8dca8fd04ad0e84620a11f187d477ca312a7d24191a8cc667
5
+ SHA512:
6
+ metadata.gz: 18dc7a53a371f61801d9cd3708fede1ec8c199f991787ab86ef43d4984809ebe8cac581189d066688d7c7e6b95cba637cad1ae712d876078bc4364cdd1321167
7
+ data.tar.gz: 8d3722ff06a964b27f008f232d3a7967f3de0a56b71d921837b2f05a7bb82c94779ec241e03ffdd9deb1efc62c91005e7656a15cc0f9c6e63b82826dfc3b01d6
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 kk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # kutils
2
+
3
+ > Ruby 通用工具库 —— 统一命名空间、极简高效、自动化工程最佳实践
4
+
5
+ ---
6
+
7
+ ## 项目简介
8
+
9
+ Kutils 是一个现代 Ruby 通用工具库,涵盖字符串、时间、文件、加解密、数组、验证、调试、DSL、格式化等常用场景,全部模块统一挂载于 `Kutils` 命名空间,极易集成与扩展。支持自动化测试、文档、CI/CD、安全扫描与 Gem 发布。
10
+
11
+ ---
12
+
13
+ ## 特性
14
+
15
+ - 统一命名空间:`Kutils::模块名.方法`
16
+ - 覆盖常用开发场景(字符串、时间、文件、加密、数组、验证等)
17
+ - 代码与文档,支持黑白主题切换
18
+ - 自动化测试、代码规范、CI/CD、文档部署、安全扫描
19
+ - 支持 Ruby 2.7+,兼容 Ruby 3.x
20
+ - 可作为 Gem 直接集成
21
+
22
+ ---
23
+
24
+ ## 安装
25
+
26
+ ```sh
27
+ git clone https://github.com/kk/kutils.git
28
+ cd kutils
29
+ bundle install
30
+ ```
31
+
32
+ 或在你的 Gemfile 中添加(发布后):
33
+
34
+ ```ruby
35
+ gem 'kutils', git: 'https://github.com/kk/kutils.git'
36
+ ```
37
+
38
+ ---
39
+
40
+ ## 用法示例
41
+
42
+ ```ruby
43
+ require 'kutils'
44
+
45
+ puts Kutils::StringUtils.camelize('hello_world') # => HelloWorld
46
+ puts Kutils::TimeUtils.now_iso8601
47
+ puts Kutils::FileUtils.safe_read('foo.txt')
48
+ puts Kutils::CryptoUtils.md5('abc')
49
+ puts Kutils::ArrayUtils.deep_flatten([[1], [2]])
50
+ puts Kutils::Validator.email?('foo@bar.com')
51
+ Kutils::DebugUtils.log({foo: 1})
52
+ Kutils::DSLBuilder.new { task('deploy') { puts 'Deploying...' } }
53
+ puts Kutils::FormatUtils.markdown_title('Hello')
54
+ ```
55
+
56
+ 更多用法见 [docs/USAGE.md](docs/USAGE.md)
57
+
58
+ ---
59
+
60
+ ## API 文档
61
+
62
+ - [API 参考 (Markdown)](docs/API.md)
63
+ - [自动化 YARD API 文档(支持黑白主题切换)](https://your-gh-pages-url/yardoc/index.html)
64
+
65
+ ---
66
+
67
+ ## 测试与质量保障
68
+
69
+ - RSpec 单元测试覆盖所有模块
70
+ - RuboCop 代码规范
71
+ - GitHub Actions 自动化测试、文档部署、安全扫描
72
+ - Bundler Audit & Dependabot 依赖安全
73
+
74
+ 本地运行:
75
+
76
+ ```sh
77
+ bundle exec rspec # 运行所有测试
78
+ bundle exec rubocop # 代码规范检查
79
+ rake yard # 生成 API 文档
80
+ ```
81
+
82
+ ---
83
+
84
+ ## 贡献指南
85
+
86
+ 欢迎 PR、Issue 及建议!
87
+
88
+ 1. Fork & 新建分支
89
+ 2. 保持代码风格与注释一致
90
+ 3. 补充/完善测试
91
+ 4. 提交 PR 前确保 CI 通过
92
+
93
+ ---
94
+
95
+ ## 许可证
96
+
97
+ MIT License. © 2025 kk
data/lib/kutils.rb ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2025 kk
4
+ #
5
+ # This software is released under the MIT License.
6
+ # https://opensource.org/licenses/MIT
7
+
8
+ require 'zeitwerk'
9
+ require_relative 'utils/version'
10
+
11
+ module Kutils
12
+ VERSION = ::Kutils::VERSION
13
+ end
14
+
15
+ loader = Zeitwerk::Loader.for_gem
16
+ loader.push_dir(File.expand_path('utils', __dir__))
17
+ loader.setup
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+ # Copyright (c) 2025 kk
3
+ #
4
+ # This software is released under the MIT License.
5
+ # https://opensource.org/licenses/MIT
6
+
7
+ module Kutils
8
+ # ArrayUtils provides array flattening and safe mapping utilities.
9
+ module ArrayUtils
10
+ # Deeply flatten an array (1 level at a time)
11
+ # @param arr [Array]
12
+ # @return [Array]
13
+ def self.deep_flatten(arr)
14
+ arr.flatten(1) while arr.any? { |a| a.is_a?(Array) }
15
+ arr
16
+ end
17
+
18
+ # Map array safely (nil returns empty array)
19
+ # @param arr [Array, nil]
20
+ # @yield [item]
21
+ # @return [Array]
22
+ def self.safe_map(arr, &block)
23
+ (arr || []).map(&block)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+ # Copyright (c) 2025 kk
3
+ #
4
+ # This software is released under the MIT License.
5
+ # https://opensource.org/licenses/MIT
6
+
7
+ require 'digest'
8
+ require 'base64'
9
+ require 'openssl'
10
+
11
+ module Kutils
12
+ # CryptoUtils provides common hash and encoding utilities.
13
+ module CryptoUtils
14
+ # Calculate MD5 hex digest
15
+ # @param str [String]
16
+ # @return [String]
17
+ def self.md5(str)
18
+ Digest::MD5.hexdigest(str)
19
+ end
20
+
21
+ # Calculate HMAC-SHA256 hex digest
22
+ # @param data [String]
23
+ # @param key [String]
24
+ # @return [String]
25
+ def self.hmac_sha256(data, key)
26
+ OpenSSL::HMAC.hexdigest('SHA256', key, data)
27
+ end
28
+
29
+ # Base64 encode string
30
+ # @param str [String]
31
+ # @return [String]
32
+ def self.base64_encode(str)
33
+ Base64.strict_encode64(str)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+ # Copyright (c) 2025 kk
3
+ #
4
+ # This software is released under the MIT License.
5
+ # https://opensource.org/licenses/MIT
6
+
7
+ require 'pp'
8
+
9
+ module Kutils
10
+ # DebugUtils provides quick debug print utilities.
11
+ module DebugUtils
12
+ # Pretty print object with optional label
13
+ # @param obj [Object]
14
+ # @param label [String, nil]
15
+ # @return [void]
16
+ def self.log(obj, label = nil)
17
+ puts "==== #{label || 'DEBUG'} ===="
18
+ pp obj
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+ # Copyright (c) 2025 kk
3
+ #
4
+ # This software is released under the MIT License.
5
+ # https://opensource.org/licenses/MIT
6
+
7
+ module Kutils
8
+ # DSLBuilder provides a simple DSL for configuration or scripting.
9
+ class DSLBuilder
10
+ # @yield DSL block
11
+ def initialize(&block)
12
+ instance_eval(&block) if block_given?
13
+ end
14
+
15
+ # Define a task with a name and block
16
+ # @param name [String]
17
+ # @yield Block to execute for the task
18
+ def task(name, &block)
19
+ puts "Define task: #{name}"
20
+ block.call if block_given?
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+ # Copyright (c) 2025 kk
3
+ #
4
+ # This software is released under the MIT License.
5
+ # https://opensource.org/licenses/MIT
6
+
7
+ module Kutils
8
+ # FileUtils provides safe file and directory operations.
9
+ # ⚠️ 注意:如与 Ruby 标准库 FileUtils 冲突,可改名为 SafeFileUtils。
10
+ module FileUtils
11
+ # Safely read file content if file exists
12
+ # @param path [String]
13
+ # @return [String, nil]
14
+ # @example
15
+ # Kutils::FileUtils.safe_read('foo.txt')
16
+ def self.safe_read(path)
17
+ File.exist?(path) ? File.read(path) : nil
18
+ end
19
+
20
+ # Create directory if not exists
21
+ # @param path [String]
22
+ # @return [void]
23
+ # @example
24
+ # Kutils::FileUtils.mkdir_p('tmp/data')
25
+ def self.mkdir_p(path)
26
+ Dir.mkdir(path) unless Dir.exist?(path)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+ # Copyright (c) 2025 kk
3
+ #
4
+ # This software is released under the MIT License.
5
+ # https://opensource.org/licenses/MIT
6
+
7
+ module Kutils
8
+ # FormatUtils provides formatting helpers for markdown and colored text.
9
+ module FormatUtils
10
+ # Format a markdown title
11
+ # @param title [String]
12
+ # @return [String]
13
+ def self.markdown_title(title)
14
+ "# #{title}\n\n"
15
+ end
16
+
17
+ # Colorize text in terminal
18
+ # @param text [String]
19
+ # @param color [Symbol]
20
+ # @return [String]
21
+ def self.color_text(text, color)
22
+ "\e[#{color_code(color)}m#{text}\e[0m"
23
+ end
24
+
25
+ # Get ANSI color code
26
+ # @param color [Symbol]
27
+ # @return [Integer]
28
+ def self.color_code(color)
29
+ { red: 31, green: 32, yellow: 33, blue: 34 }[color] || 0
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+ # Copyright (c) 2025 kk
3
+ #
4
+ # This software is released under the MIT License.
5
+ # https://opensource.org/licenses/MIT
6
+
7
+ module Kutils
8
+ # StringUtils provides string format conversion and cleaning utilities.
9
+ module StringUtils
10
+ # Convert snake_case to CamelCase
11
+ # @param str [String]
12
+ # @return [String]
13
+ # @example
14
+ # Kutils::StringUtils.camelize('hello_world') #=> 'HelloWorld'
15
+ def self.camelize(str)
16
+ str.split('_').map(&:capitalize).join
17
+ end
18
+
19
+ # Convert CamelCase or kebab-case to snake_case
20
+ # @param str [String]
21
+ # @return [String]
22
+ # @example
23
+ # Kutils::StringUtils.underscore('HelloWorld') #=> 'hello_world'
24
+ def self.underscore(str)
25
+ str.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\\1_\\2').gsub(/([a-z\d])([A-Z])/,'\\1_\\2').tr("-", "_").downcase
26
+ end
27
+
28
+ # Convert string to URL slug (lowercase, dash-separated, alphanumeric)
29
+ # @param str [String]
30
+ # @return [String]
31
+ # @example
32
+ # Kutils::StringUtils.slugify('Hello Ruby!') #=> 'hello-ruby'
33
+ def self.slugify(str)
34
+ str.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+ # Copyright (c) 2025 kk
3
+ #
4
+ # This software is released under the MIT License.
5
+ # https://opensource.org/licenses/MIT
6
+
7
+ require 'time'
8
+
9
+ module Kutils
10
+ # TimeUtils provides time formatting and duration utilities.
11
+ module TimeUtils
12
+ # Get current UTC time in ISO8601 format
13
+ # @return [String]
14
+ # @example
15
+ # Kutils::TimeUtils.now_iso8601 #=> '2025-01-01T12:00:00Z'
16
+ def self.now_iso8601
17
+ Time.now.utc.iso8601
18
+ end
19
+
20
+ # Format seconds as HH:MM:SS
21
+ # @param seconds [Integer]
22
+ # @return [String]
23
+ # @example
24
+ # Kutils::TimeUtils.human_duration(3661) #=> '01:01:01'
25
+ def self.human_duration(seconds)
26
+ mm, ss = seconds.divmod(60)
27
+ hh, mm = mm.divmod(60)
28
+ format('%02d:%02d:%02d', hh, mm, ss)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+ # Copyright (c) 2025 kk
3
+ #
4
+ # This software is released under the MIT License.
5
+ # https://opensource.org/licenses/MIT
6
+
7
+ module Kutils
8
+ # Validator provides simple parameter validation utilities.
9
+ module Validator
10
+ # Check if value is present (not nil or empty string)
11
+ # @param val [Object]
12
+ # @return [Boolean]
13
+ def self.required?(val)
14
+ !val.nil? && val != ''
15
+ end
16
+
17
+ # Check if value is a valid email address
18
+ # @param val [String]
19
+ # @return [Boolean]
20
+ def self.email?(val)
21
+ !!(val =~ /\A[^@\s]+@[^@\s]+\z/)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+ # Copyright (c) 2025 kk
3
+ #
4
+ # This software is released under the MIT License.
5
+ # https://opensource.org/licenses/MIT
6
+
7
+ module Kutils
8
+ VERSION = '0.1.0'
9
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kutils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-07-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kramdown
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rouge
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.60'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.60'
69
+ description: String, time, file, crypto, array, validation, debug, DSL, formatting
70
+ and more.
71
+ email:
72
+ - kk@example.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - LICENSE
78
+ - README.md
79
+ - lib/kutils.rb
80
+ - lib/utils/array_utils.rb
81
+ - lib/utils/crypto_utils.rb
82
+ - lib/utils/debug_utils.rb
83
+ - lib/utils/dsl_builder.rb
84
+ - lib/utils/file_utils.rb
85
+ - lib/utils/format_utils.rb
86
+ - lib/utils/string_utils.rb
87
+ - lib/utils/time_utils.rb
88
+ - lib/utils/validator.rb
89
+ - lib/utils/version.rb
90
+ homepage: https://github.com/kk/kutils
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 2.7.0
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubygems_version: 3.4.19
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A collection of geek-style Ruby utility modules.
113
+ test_files: []