sumi 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.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +1 -0
  4. data/lib/sumi/version.rb +4 -0
  5. data/lib/sumi.rb +119 -0
  6. metadata +50 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7b5126797070f46a0b06f9b6124a187db5f46038f218996826b70aba710987ed
4
+ data.tar.gz: 58ac0f18996e1651634d4232d9467b0256be2854fb7c75889324d88ec92bc0e3
5
+ SHA512:
6
+ metadata.gz: 1f531452d46c679ebe6215bad01c75bfbc094f19401d78902c2454a2c0ed385df67b1312b2a853f511b6cf3120d559342e1a65ca51f66189b1e36872a3bb66ed
7
+ data.tar.gz: 3deb211ebb558a284146393de650979e509fd576572c0859c55357ce2a22ec0593fbd5efcc8fe9ff807311200c0359934a513fcba3b91925e29c269b06efda8f
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Joel Drapper
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # Sumi
@@ -0,0 +1,4 @@
1
+
2
+ module Sumi
3
+ VERSION = "0.1.0"
4
+ end
data/lib/sumi.rb ADDED
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sumi/version"
4
+
5
+ module Sumi
6
+ def self.inspect(object, indent: 0, tab_width: 2, max_width: 60, max_depth: 5, max_instance_variables: 10, original_object: nil)
7
+ return "self" if object && object == original_object
8
+
9
+ original_object ||= object
10
+
11
+ case object
12
+ when Hash
13
+ return "{}" if object.empty?
14
+
15
+ buffer = +"{\n"
16
+ indent += 1
17
+ object.each do |key, value|
18
+ buffer << ("\t" * indent)
19
+ case key
20
+ when Symbol
21
+ buffer << "#{key.name}: "
22
+ else
23
+ buffer << inspect(key, indent:, original_object:)
24
+ buffer << " => "
25
+ end
26
+ buffer << inspect(value, indent:, original_object:)
27
+ buffer << ",\n"
28
+ end
29
+ indent -= 1
30
+ buffer << ("\t" * indent)
31
+ buffer << "}"
32
+ when Array
33
+ new_lines = false
34
+ length = 0
35
+ items = object.map do |item|
36
+ pretty_item = inspect(item, indent: indent + 1, original_object:)
37
+ new_lines = true if pretty_item.include?("\n")
38
+ length += pretty_item.bytesize
39
+ pretty_item
40
+ end
41
+
42
+ if new_lines || length > max_width - (indent * tab_width)
43
+ "[\n#{"\t" * (indent + 1)}#{items.join(",\n#{"\t" * (indent + 1)}")},\n#{"\t" * indent}]"
44
+ else
45
+ "[#{items.join(', ')}]"
46
+ end
47
+ when Set
48
+ new_lines = false
49
+ length = 0
50
+ items = object.to_a.sort!.map do |item|
51
+ pretty_item = inspect(item, indent: indent + 1, original_object:)
52
+ new_lines = true if pretty_item.include?("\n")
53
+ length += pretty_item.bytesize
54
+ pretty_item
55
+ end
56
+
57
+ if new_lines || length > max_width - (indent * tab_width)
58
+ "Set[\n#{"\t" * (indent + 1)}#{items.join(",\n#{"\t" * (indent + 1)}")},\n#{"\t" * indent}]"
59
+ else
60
+ "Set[#{items.join(', ')}]"
61
+ end
62
+ when Module
63
+ object.name
64
+ when Pathname
65
+ %(Pathname("#{object.to_path}"))
66
+ when Date, DateTime, Time
67
+ %(#{object.class.name}("#{object}"))
68
+ when Symbol, String, Integer, Float, Regexp, Range, Rational, Complex, true, false, nil
69
+ object.inspect
70
+ when Data
71
+ buffer = +""
72
+ members = object.members.take(max_instance_variables) # TODO: either rename max_instance_variables to max_properties or define a max_members specifcally for data objects
73
+ total_count = object.members.length
74
+ items = members.map { |key| [key, object.__send__(key)] }
75
+
76
+ inspect_object(object:, original_object:, buffer:, items:, total_count:, indent:, max_depth:, max_instance_variables:, separator: ": ")
77
+ else
78
+ buffer = +""
79
+ instance_variables = object.instance_variables.take(max_instance_variables)
80
+ total_count = object.instance_variables.length
81
+ items = instance_variables.map { |name| [name, object.instance_variable_get(name)] }
82
+
83
+ inspect_object(object:, original_object:, buffer:, items:, total_count:, indent:, max_depth:, max_instance_variables:, separator: " = ")
84
+ end
85
+ end
86
+
87
+ def self.inspect_object(object:, original_object:, buffer:, items:, total_count:, indent:, max_depth:, max_instance_variables:, separator:)
88
+ if total_count > 0 && indent < max_depth
89
+ buffer << "#{object.class.name}(\n"
90
+ indent += 1
91
+
92
+ if indent < max_depth
93
+ items.take(max_instance_variables).each do |key, value|
94
+ buffer << ("\t" * indent)
95
+ buffer << "#{key}#{separator}"
96
+
97
+ buffer << inspect(value, indent:, original_object:)
98
+ buffer << ",\n"
99
+ end
100
+
101
+ if total_count > max_instance_variables
102
+ buffer << ("\t" * indent)
103
+ buffer << "...\n"
104
+ end
105
+ else
106
+ buffer << ("\t" * indent)
107
+ buffer << "...\n"
108
+ end
109
+
110
+ indent -= 1
111
+ buffer << ("\t" * indent)
112
+ buffer << ")"
113
+ elsif indent >= max_depth
114
+ buffer << "#{object.class.name}(...)"
115
+ else
116
+ buffer << "#{object.class.name}()"
117
+ end
118
+ end
119
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sumi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joel Drapper
8
+ - Marco Roth
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-01-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Print Ruby objects as Ruby
14
+ email:
15
+ - joel@drapper.me
16
+ - marco.roth@intergga.ch
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE.txt
22
+ - README.md
23
+ - lib/sumi.rb
24
+ - lib/sumi/version.rb
25
+ homepage: https://github.com/joeldrapper/sumi
26
+ licenses:
27
+ - MIT
28
+ metadata:
29
+ homepage_uri: https://github.com/joeldrapper/sumi
30
+ source_code_uri: https://github.com/joeldrapper/sumi
31
+ funding_uri: https://github.com/sponsors/joeldrapper
32
+ rubygems_mfa_required: 'true'
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.6.2
48
+ specification_version: 4
49
+ summary: Print Ruby objects as Ruby
50
+ test_files: []